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