de9565f723aa6191d7928cdebe551d8e806d3af0
[oota-llvm.git] / include / llvm / AbstractTypeUser.h
1 //===-- llvm/AbstractTypeUser.h - AbstractTypeUser Interface ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The AbstractTypeUser class is an interface to be implemented by classes who
11 // could possible use an abstract type.  Abstract types are denoted by the
12 // isAbstract flag set to true in the Type class.  These are classes that
13 // contain an Opaque type in their structure somehow.
14 //
15 // Classes must implement this interface so that they may be notified when an
16 // abstract type is resolved.  Abstract types may be resolved into more concrete
17 // types through: linking, parsing, and bytecode reading.  When this happens,
18 // all of the users of the type must be updated to reference the new, more
19 // concrete type.  They are notified through the AbstractTypeUser interface.
20 //
21 // In addition to this, AbstractTypeUsers must keep the use list of the
22 // potentially abstract type that they reference up-to-date.  To do this in a
23 // nice, transparent way, the PATypeHandle class is used to hold "Potentially
24 // Abstract Types", and keep the use list of the abstract types up-to-date.
25 //
26 //===----------------------------------------------------------------------===//
27
28 #ifndef LLVM_ABSTRACT_TYPE_USER_H
29 #define LLVM_ABSTRACT_TYPE_USER_H
30
31 // This is the "master" include for <cassert> Whether this file needs it or not,
32 // it must always include <cassert> for the files which include
33 // llvm/AbstractTypeUser.h
34 //
35 // In this way, most every LLVM source file will have access to the assert()
36 // macro without having to #include <cassert> directly.
37 //
38 #include <cassert>
39
40 namespace llvm {
41
42 class Type;
43 class DerivedType;
44
45 class AbstractTypeUser {
46 protected:
47   virtual ~AbstractTypeUser();                        // Derive from me
48 public:
49
50   /// refineAbstractType - The callback method invoked when an abstract type is
51   /// resolved to another type.  An object must override this method to update
52   /// its internal state to reference NewType instead of OldType.
53   ///
54   virtual void refineAbstractType(const DerivedType *OldTy,
55                                   const Type *NewTy) = 0;
56
57   /// The other case which AbstractTypeUsers must be aware of is when a type
58   /// makes the transition from being abstract (where it has clients on it's
59   /// AbstractTypeUsers list) to concrete (where it does not).  This method
60   /// notifies ATU's when this occurs for a type.
61   ///
62   virtual void typeBecameConcrete(const DerivedType *AbsTy) = 0;
63
64   // for debugging...
65   virtual void dump() const = 0;
66 };
67
68
69 /// PATypeHandle - Handle to a Type subclass.  This class is used to keep the
70 /// use list of abstract types up-to-date.
71 ///
72 class PATypeHandle {
73   const Type *Ty;
74   AbstractTypeUser * const User;
75
76   // These functions are defined at the bottom of Type.h.  See the comment there
77   // for justification.
78   void addUser();
79   void removeUser();
80 public:
81   // ctor - Add use to type if abstract.  Note that Ty must not be null
82   inline PATypeHandle(const Type *ty, AbstractTypeUser *user)
83     : Ty(ty), User(user) {
84     addUser();
85   }
86
87   // ctor - Add use to type if abstract.
88   inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
89     addUser();
90   }
91
92   // dtor - Remove reference to type...
93   inline ~PATypeHandle() { removeUser(); }
94
95   // Automatic casting operator so that the handle may be used naturally
96   inline operator Type *() const { return const_cast<Type*>(Ty); }
97   inline Type *get() const { return const_cast<Type*>(Ty); }
98
99   // operator= - Allow assignment to handle
100   inline Type *operator=(const Type *ty) {
101     if (Ty != ty) {   // Ensure we don't accidentally drop last ref to Ty
102       removeUser();
103       Ty = ty;
104       addUser();
105     }
106     return get();
107   }
108
109   // operator= - Allow assignment to handle
110   inline const Type *operator=(const PATypeHandle &T) {
111     return operator=(T.Ty);
112   }
113
114   inline bool operator==(const Type *ty) {
115     return Ty == ty;
116   }
117
118   // operator-> - Allow user to dereference handle naturally...
119   inline const Type *operator->() const { return Ty; }
120
121   // removeUserFromConcrete - This function should be called when the User is
122   // notified that our type is refined... and the type is being refined to
123   // itself, which is now a concrete type.  When a type becomes concrete like
124   // this, we MUST remove ourself from the AbstractTypeUser list, even though
125   // the type is apparently concrete.
126   //
127   void removeUserFromConcrete();
128 };
129
130
131 /// PATypeHolder - Holder class for a potentially abstract type.  This uses
132 /// efficient union-find techniques to handle dynamic type resolution.  Unless
133 /// you need to do custom processing when types are resolved, you should always
134 /// use PATypeHolders in preference to PATypeHandles.
135 ///
136 class PATypeHolder {
137   mutable const Type *Ty;
138 public:
139   PATypeHolder(const Type *ty) : Ty(ty) {
140     addRef();
141   }
142   PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) {
143     addRef();
144   }
145
146   ~PATypeHolder() { dropRef(); }
147
148   operator Type *() const { return get(); }
149   Type *get() const;
150
151   // operator-> - Allow user to dereference handle naturally...
152   Type *operator->() const { return get(); }
153
154   // operator= - Allow assignment to handle
155   Type *operator=(const Type *ty) {
156     if (Ty != ty) {   // Don't accidentally drop last ref to Ty.
157       dropRef();
158       Ty = ty;
159       addRef();
160     }
161     return get();
162   }
163   Type *operator=(const PATypeHolder &H) {
164     return operator=(H.Ty);
165   }
166
167   /// getRawType - This should only be used to implement the vmcore library.
168   ///
169   const Type *getRawType() const { return Ty; }
170
171 private:
172   void addRef();
173   void dropRef();
174 };
175
176 } // End llvm namespace
177
178 #endif