Add option to print per module instead of per method, so that
[oota-llvm.git] / include / llvm / AbstractTypeUser.h
1 //===-- llvm/AbstractTypeUser.h - AbstractTypeUser Interface -----*- C++ -*--=//
2 //
3 // The AbstractTypeUser class is an interface to be implemented by classes who
4 // could possible use an abstract type.  Abstract types are denoted by the
5 // isAbstract flag set to true in the Type class.  These are classes that
6 // contain an Opaque type in their structure somehow.
7 //
8 // Classes must implement this interface so that they may be notified when an
9 // abstract type is resolved.  Abstract types may be resolved into more concrete
10 // types through: linking, parsing, and bytecode reading.  When this happens,
11 // all of the users of the type must be updated to reference the new, more
12 // concrete type.  They are notified through the AbstractTypeUser interface.
13 //
14 // In addition to this, AbstractTypeUsers must keep the use list of the
15 // potentially abstract type that they reference up-to-date.  To do this in a
16 // nice, transparent way, the PATypeHandle class is used to hold "Potentially
17 // Abstract Types", and keep the use list of the abstract types up-to-date.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_ABSTRACT_TYPE_USER_H
22 #define LLVM_ABSTRACT_TYPE_USER_H
23
24 class Type;
25 class DerivedType;
26
27 class AbstractTypeUser {
28 protected:
29   virtual ~AbstractTypeUser() {}                        // Derive from me
30 public:
31
32   // refineAbstractType - The callback method invoked when an abstract type
33   // has been found to be more concrete.  A class must override this method to
34   // update its internal state to reference NewType instead of OldType.  Soon
35   // after this method is invoked, OldType shall be deleted, so referencing it
36   // is quite unwise.
37   //
38   virtual void refineAbstractType(const DerivedType *OldTy,
39                                   const Type *NewTy) = 0;
40 };
41
42
43 // PATypeHandle - Handle to a Type subclass.  This class is parameterized so
44 // that users can have handles to MethodType's that are still specialized, for
45 // example.  This class is a simple class used to keep the use list of abstract
46 // types up-to-date.
47 //
48 template <class TypeSubClass>
49 class PATypeHandle {
50   const TypeSubClass *Ty;
51   AbstractTypeUser * const User;
52
53   // These functions are defined at the bottom of Type.h.  See the comment there
54   // for justification.
55   inline void addUser();
56   inline void removeUser();
57 public:
58   // ctor - Add use to type if abstract.  Note that Ty must not be null
59   inline PATypeHandle(const TypeSubClass *ty, AbstractTypeUser *user) 
60     : Ty(ty), User(user) {
61     addUser();
62   }
63
64   // ctor - Add use to type if abstract.
65   inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
66     addUser();
67   }
68
69   // dtor - Remove reference to type...
70   inline ~PATypeHandle() { removeUser(); }
71
72   // Automatic casting operator so that the handle may be used naturally
73   inline operator const TypeSubClass *() const { return Ty; }
74   inline const TypeSubClass *get() const { return Ty; }
75
76   // operator= - Allow assignment to handle
77   inline const TypeSubClass *operator=(const TypeSubClass *ty) {
78     if (Ty != ty) {   // Ensure we don't accidentally drop last ref to Ty
79       removeUser();
80       Ty = ty;
81       addUser();
82     }
83     return Ty;
84   }
85
86   // operator= - Allow assignment to handle
87   inline const TypeSubClass *operator=(const PATypeHandle &T) {
88     return operator=(T.Ty);
89   }
90
91   inline bool operator==(const TypeSubClass *ty) {
92     return Ty == ty;
93   }
94
95   // operator-> - Allow user to dereference handle naturally...
96   inline const TypeSubClass *operator->() const { return Ty; }
97 };
98
99
100 // PATypeHolder - Holder class for a potentially abstract type.  This functions
101 // as both a handle (as above) and an AbstractTypeUser.  It uses the callback to
102 // keep its pointer member updated to the current version of the type.
103 //
104 template <class TypeSC>
105 class PATypeHolder : public AbstractTypeUser, public PATypeHandle<TypeSC> {
106 public:
107   inline PATypeHolder(const TypeSC *ty) : PATypeHandle<TypeSC>(ty, this) {}
108   inline PATypeHolder(const PATypeHolder &T)
109     : AbstractTypeUser(T), PATypeHandle<TypeSC>(T, this) {}
110
111   // refineAbstractType - All we do is update our PATypeHandle member to point
112   // to the new type.
113   //
114   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
115     assert(get() == OldTy && "Can't refine to unknown value!");
116     PATypeHandle<TypeSC>::operator=((const TypeSC*)NewTy);
117   }
118
119   // operator= - Allow assignment to handle
120   inline const TypeSC *operator=(const TypeSC *ty) {
121     return PATypeHandle<TypeSC>::operator=(ty);
122   }
123
124   // operator= - Allow assignment to handle
125   inline const TypeSC *operator=(const PATypeHandle<TypeSC> &T) {
126     return PATypeHandle<TypeSC>::operator=(T);
127   }
128   inline const TypeSC *operator=(const PATypeHolder<TypeSC> &H) {
129     return PATypeHandle<TypeSC>::operator=(H);
130   }
131 };
132
133
134 #endif