Provide support for detecting if the Win32 imaghlp and psapi libraries
[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
122
123 /// PATypeHolder - Holder class for a potentially abstract type.  This uses
124 /// efficient union-find techniques to handle dynamic type resolution.  Unless
125 /// you need to do custom processing when types are resolved, you should always
126 /// use PATypeHolders in preference to PATypeHandles.
127 ///
128 class PATypeHolder {
129   mutable const Type *Ty;
130 public:
131   PATypeHolder(const Type *ty) : Ty(ty) {
132     addRef();
133   }
134   PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) {
135     addRef();
136   }
137
138   ~PATypeHolder() { dropRef(); }
139
140   operator Type *() const { return get(); }
141   Type *get() const;
142
143   // operator-> - Allow user to dereference handle naturally...
144   Type *operator->() const { return get(); }
145
146   // operator= - Allow assignment to handle
147   Type *operator=(const Type *ty) {
148     if (Ty != ty) {   // Don't accidentally drop last ref to Ty.
149       dropRef();
150       Ty = ty;
151       addRef();
152     }
153     return get();
154   }
155   Type *operator=(const PATypeHolder &H) {
156     return operator=(H.Ty);
157   }
158
159   /// getRawType - This should only be used to implement the vmcore library.
160   ///
161   const Type *getRawType() const { return Ty; }
162
163 private:
164   void addRef();
165   void dropRef();
166 };
167
168 } // End llvm namespace
169
170 #endif