ExecutionEngine::create no longer takes a TraceMode argument.
[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 class Type;
41 class DerivedType;
42
43 class AbstractTypeUser {
44 protected:
45   virtual ~AbstractTypeUser() {}                        // Derive from me
46 public:
47
48   /// refineAbstractType - The callback method invoked when an abstract type is
49   /// resolved to another type.  An object must override this method to update
50   /// its internal state to reference NewType instead of OldType.
51   ///
52   virtual void refineAbstractType(const DerivedType *OldTy,
53                                   const Type *NewTy) = 0;
54
55   /// The other case which AbstractTypeUsers must be aware of is when a type
56   /// makes the transition from being abstract (where it has clients on it's
57   /// AbstractTypeUsers list) to concrete (where it does not).  This method
58   /// notifies ATU's when this occurs for a type.
59   ///
60   virtual void typeBecameConcrete(const DerivedType *AbsTy) = 0;
61
62   // for debugging...
63   virtual void dump() const = 0;
64 };
65
66
67 /// PATypeHandle - Handle to a Type subclass.  This class is used to keep the
68 /// use list of abstract types up-to-date.
69 ///
70 class PATypeHandle {
71   const Type *Ty;
72   AbstractTypeUser * const User;
73
74   // These functions are defined at the bottom of Type.h.  See the comment there
75   // for justification.
76   void addUser();
77   void removeUser();
78 public:
79   // ctor - Add use to type if abstract.  Note that Ty must not be null
80   inline PATypeHandle(const Type *ty, AbstractTypeUser *user) 
81     : Ty(ty), User(user) {
82     addUser();
83   }
84
85   // ctor - Add use to type if abstract.
86   inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
87     addUser();
88   }
89
90   // dtor - Remove reference to type...
91   inline ~PATypeHandle() { removeUser(); }
92
93   // Automatic casting operator so that the handle may be used naturally
94   inline operator const Type *() const { return Ty; }
95   inline const Type *get() const { return Ty; }
96
97   // operator= - Allow assignment to handle
98   inline const Type *operator=(const Type *ty) {
99     if (Ty != ty) {   // Ensure we don't accidentally drop last ref to Ty
100       removeUser();
101       Ty = ty;
102       addUser();
103     }
104     return Ty;
105   }
106
107   // operator= - Allow assignment to handle
108   inline const Type *operator=(const PATypeHandle &T) {
109     return operator=(T.Ty);
110   }
111
112   inline bool operator==(const Type *ty) {
113     return Ty == ty;
114   }
115
116   // operator-> - Allow user to dereference handle naturally...
117   inline const Type *operator->() const { return Ty; }
118
119   // removeUserFromConcrete - This function should be called when the User is
120   // notified that our type is refined... and the type is being refined to
121   // itself, which is now a concrete type.  When a type becomes concrete like
122   // this, we MUST remove ourself from the AbstractTypeUser list, even though
123   // the type is apparently concrete.
124   //
125   void removeUserFromConcrete();
126 };
127
128
129 /// PATypeHolder - Holder class for a potentially abstract type.  This uses
130 /// efficient union-find techniques to handle dynamic type resolution.  Unless
131 /// you need to do custom processing when types are resolved, you should always
132 /// use PATypeHolders in preference to PATypeHandles.
133 ///
134 class PATypeHolder {
135   mutable const Type *Ty;
136 public:
137   PATypeHolder(const Type *ty) : Ty(ty) {
138     addRef();
139   }
140   PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) {
141     addRef();
142   }
143
144   operator const Type *() const { return get(); }
145   const Type *get() const;
146
147   // operator-> - Allow user to dereference handle naturally...
148   const Type *operator->() const { return get(); }
149
150   // operator= - Allow assignment to handle
151   const Type *operator=(const Type *ty) {
152     if (Ty != ty) {   // Don't accidentally drop last ref to Ty.
153       dropRef();
154       Ty = ty;
155       addRef();
156     }
157     return get();
158   }
159   const Type *operator=(const PATypeHolder &H) {
160     return operator=(H.Ty);
161   }
162
163 private:
164   void addRef();
165   void dropRef();
166 };
167
168 #endif