Improve support for type-generic vector intrinsics by teaching TableGen how
[oota-llvm.git] / include / llvm / AbstractTypeUser.h
1 //===-- llvm/AbstractTypeUser.h - AbstractTypeUser Interface ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 template<typename T> struct simplify_type;
37
38 /// The AbstractTypeUser class is an interface to be implemented by classes who
39 /// could possibly use an abstract type.  Abstract types are denoted by the
40 /// isAbstract flag set to true in the Type class.  These are classes that
41 /// contain an Opaque type in their structure somewhere.
42 ///
43 /// Classes must implement this interface so that they may be notified when an
44 /// abstract type is resolved.  Abstract types may be resolved into more 
45 /// concrete types through: linking, parsing, and bitcode reading.  When this 
46 /// happens, all of the users of the type must be updated to reference the new,
47 /// more concrete type.  They are notified through the AbstractTypeUser 
48 /// interface.
49 ///
50 /// In addition to this, AbstractTypeUsers must keep the use list of the
51 /// potentially abstract type that they reference up-to-date.  To do this in a
52 /// nice, transparent way, the PATypeHandle class is used to hold "Potentially
53 /// Abstract Types", and keep the use list of the abstract types up-to-date.
54 /// @brief LLVM Abstract Type User Representation
55 class AbstractTypeUser {
56 protected:
57   virtual ~AbstractTypeUser();                        // Derive from me
58 public:
59
60   /// refineAbstractType - The callback method invoked when an abstract type is
61   /// resolved to another type.  An object must override this method to update
62   /// its internal state to reference NewType instead of OldType.
63   ///
64   virtual void refineAbstractType(const DerivedType *OldTy,
65                                   const Type *NewTy) = 0;
66
67   /// The other case which AbstractTypeUsers must be aware of is when a type
68   /// makes the transition from being abstract (where it has clients on it's
69   /// AbstractTypeUsers list) to concrete (where it does not).  This method
70   /// notifies ATU's when this occurs for a type.
71   ///
72   virtual void typeBecameConcrete(const DerivedType *AbsTy) = 0;
73
74   // for debugging...
75   virtual void dump() const = 0;
76 };
77
78
79 /// PATypeHandle - Handle to a Type subclass.  This class is used to keep the
80 /// use list of abstract types up-to-date.
81 ///
82 class PATypeHandle {
83   const Type *Ty;
84   AbstractTypeUser * const User;
85
86   // These functions are defined at the bottom of Type.h.  See the comment there
87   // for justification.
88   void addUser();
89   void removeUser();
90 public:
91   // ctor - Add use to type if abstract.  Note that Ty must not be null
92   inline PATypeHandle(const Type *ty, AbstractTypeUser *user)
93     : Ty(ty), User(user) {
94     addUser();
95   }
96
97   // ctor - Add use to type if abstract.
98   inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
99     addUser();
100   }
101
102   // dtor - Remove reference to type...
103   inline ~PATypeHandle() { removeUser(); }
104
105   // Automatic casting operator so that the handle may be used naturally
106   inline operator Type *() const { return const_cast<Type*>(Ty); }
107   inline Type *get() const { return const_cast<Type*>(Ty); }
108
109   // operator= - Allow assignment to handle
110   inline Type *operator=(const Type *ty) {
111     if (Ty != ty) {   // Ensure we don't accidentally drop last ref to Ty
112       removeUser();
113       Ty = ty;
114       addUser();
115     }
116     return get();
117   }
118
119   // operator= - Allow assignment to handle
120   inline const Type *operator=(const PATypeHandle &T) {
121     return operator=(T.Ty);
122   }
123
124   inline bool operator==(const Type *ty) {
125     return Ty == ty;
126   }
127
128   // operator-> - Allow user to dereference handle naturally...
129   inline const Type *operator->() const { return Ty; }
130 };
131
132
133 /// PATypeHolder - Holder class for a potentially abstract type.  This uses
134 /// efficient union-find techniques to handle dynamic type resolution.  Unless
135 /// you need to do custom processing when types are resolved, you should always
136 /// use PATypeHolders in preference to PATypeHandles.
137 ///
138 class PATypeHolder {
139   mutable const Type *Ty;
140 public:
141   PATypeHolder(const Type *ty) : Ty(ty) {
142     addRef();
143   }
144   PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) {
145     addRef();
146   }
147
148   ~PATypeHolder() { dropRef(); }
149
150   operator Type *() const { return get(); }
151   Type *get() const;
152
153   // operator-> - Allow user to dereference handle naturally...
154   Type *operator->() const { return get(); }
155
156   // operator= - Allow assignment to handle
157   Type *operator=(const Type *ty) {
158     if (Ty != ty) {   // Don't accidentally drop last ref to Ty.
159       dropRef();
160       Ty = ty;
161       addRef();
162     }
163     return get();
164   }
165   Type *operator=(const PATypeHolder &H) {
166     return operator=(H.Ty);
167   }
168
169   /// getRawType - This should only be used to implement the vmcore library.
170   ///
171   const Type *getRawType() const { return Ty; }
172
173 private:
174   void addRef();
175   void dropRef();
176 };
177
178 // simplify_type - Allow clients to treat uses just like values when using
179 // casting operators.
180 template<> struct simplify_type<PATypeHolder> {
181   typedef const Type* SimpleType;
182   static SimpleType getSimplifiedValue(const PATypeHolder &Val) {
183     return static_cast<SimpleType>(Val.get());
184   }
185 };
186 template<> struct simplify_type<const PATypeHolder> {
187   typedef const Type* SimpleType;
188   static SimpleType getSimplifiedValue(const PATypeHolder &Val) {
189     return static_cast<SimpleType>(Val.get());
190   }
191 };
192   
193 } // End llvm namespace
194
195 #endif