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