Use the C++ <cassert> header, not the C <assert.h> header
[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 // This is the "master" include for <cassert> Whether this file needs it or not,
25 // it must always include <cassert> for the files which include
26 // llvm/AbstractTypeUser.h
27 //
28 // In this way, most every LLVM source file will have access to the assert()
29 // macro without having to #include <cassert> directly.
30 //
31 #include <cassert>
32
33 class Type;
34 class DerivedType;
35
36 class AbstractTypeUser {
37 protected:
38   virtual ~AbstractTypeUser() {}                        // Derive from me
39 public:
40
41   // refineAbstractType - The callback method invoked when an abstract type
42   // has been found to be more concrete.  A class must override this method to
43   // update its internal state to reference NewType instead of OldType.  Soon
44   // after this method is invoked, OldType shall be deleted, so referencing it
45   // is quite unwise.
46   //
47   // Another case that is important to consider is when a type is refined, but
48   // stays in the same place in memory.  In this case OldTy will equal NewTy.
49   // This callback just notifies ATU's that the underlying structure of the type
50   // has changed... but any previously used properties are still valid.
51   //
52   // Note that it is possible to refine a type with parameters OldTy==NewTy, and
53   // OldTy is no longer abstract.  In this case, abstract type users should
54   // release their hold on a type, because it went from being abstract to
55   // concrete.
56   //
57   virtual void refineAbstractType(const DerivedType *OldTy,
58                                   const Type *NewTy) = 0;
59   // for debugging...
60   virtual void dump() const = 0;
61 };
62
63
64 // PATypeHandle - Handle to a Type subclass.  This class is parameterized so
65 // that users can have handles to FunctionType's that are still specialized, for
66 // example.  This class is a simple class used to keep the use list of abstract
67 // types up-to-date.
68 //
69 class PATypeHandle {
70   const Type *Ty;
71   AbstractTypeUser * const User;
72
73   // These functions are defined at the bottom of Type.h.  See the comment there
74   // for justification.
75   inline void addUser();
76   inline void removeUser();
77 public:
78   // ctor - Add use to type if abstract.  Note that Ty must not be null
79   inline PATypeHandle(const Type *ty, AbstractTypeUser *user) 
80     : Ty(ty), User(user) {
81     addUser();
82   }
83
84   // ctor - Add use to type if abstract.
85   inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
86     addUser();
87   }
88
89   // dtor - Remove reference to type...
90   inline ~PATypeHandle() { removeUser(); }
91
92   // Automatic casting operator so that the handle may be used naturally
93   inline operator const Type *() const { return Ty; }
94   inline const Type *get() const { return Ty; }
95
96   // operator= - Allow assignment to handle
97   inline const Type *operator=(const Type *ty) {
98     if (Ty != ty) {   // Ensure we don't accidentally drop last ref to Ty
99       removeUser();
100       Ty = ty;
101       addUser();
102     }
103     return Ty;
104   }
105
106   // operator= - Allow assignment to handle
107   inline const Type *operator=(const PATypeHandle &T) {
108     return operator=(T.Ty);
109   }
110
111   inline bool operator==(const Type *ty) {
112     return Ty == ty;
113   }
114
115   // operator-> - Allow user to dereference handle naturally...
116   inline const Type *operator->() const { return Ty; }
117
118   // removeUserFromConcrete - This function should be called when the User is
119   // notified that our type is refined... and the type is being refined to
120   // itself, which is now a concrete type.  When a type becomes concrete like
121   // this, we MUST remove ourself from the AbstractTypeUser list, even though
122   // the type is apparently concrete.
123   //
124   inline void removeUserFromConcrete();
125 };
126
127
128 // PATypeHolder - Holder class for a potentially abstract type.  This functions
129 // as both a handle (as above) and an AbstractTypeUser.  It uses the callback to
130 // keep its pointer member updated to the current version of the type.
131 //
132 struct PATypeHolder : public AbstractTypeUser, public PATypeHandle {
133   inline PATypeHolder(const Type *ty) : PATypeHandle(ty, this) {}
134   inline PATypeHolder(const PATypeHolder &T)
135     : AbstractTypeUser(T), PATypeHandle(T, this) {}
136
137   // refineAbstractType - All we do is update our PATypeHandle member to point
138   // to the new type.
139   //
140   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
141     assert(get() == (const Type*)OldTy && "Can't refine to unknown value!");
142
143     // Check to see if the type just became concrete.  If so, we have to
144     // removeUser to get off its AbstractTypeUser list
145     removeUserFromConcrete();
146
147     if ((const Type*)OldTy != NewTy)
148       PATypeHandle::operator=(NewTy);
149   }
150
151   // operator= - Allow assignment to handle
152   inline const Type *operator=(const Type *ty) {
153     return PATypeHandle::operator=(ty);
154   }
155
156   // operator= - Allow assignment to handle
157   inline const Type *operator=(const PATypeHandle &T) {
158     return PATypeHandle::operator=(T);
159   }
160   inline const Type *operator=(const PATypeHolder &H) {
161     return PATypeHandle::operator=(H);
162   }
163
164   void dump() const;
165 };
166
167 #endif