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