1 //===-- llvm/AbstractTypeUser.h - AbstractTypeUser Interface ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file declares the AbstractTypeUser class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ABSTRACT_TYPE_USER_H
15 #define LLVM_ABSTRACT_TYPE_USER_H
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
21 // In this way, most every LLVM source file will have access to the assert()
22 // macro without having to #include <cassert> directly.
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.
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
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
{
50 virtual ~AbstractTypeUser(); // Derive from me
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.
57 virtual void refineAbstractType(const DerivedType
*OldTy
,
58 const Type
*NewTy
) = 0;
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.
65 virtual void typeBecameConcrete(const DerivedType
*AbsTy
) = 0;
68 virtual void dump() const = 0;
72 /// PATypeHandle - Handle to a Type subclass. This class is used to keep the
73 /// use list of abstract types up-to-date.
77 AbstractTypeUser
* const User
;
79 // These functions are defined at the bottom of Type.h. See the comment there
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
) {
90 // ctor - Add use to type if abstract.
91 inline PATypeHandle(const PATypeHandle
&T
) : Ty(T
.Ty
), User(T
.User
) {
95 // dtor - Remove reference to type...
96 inline ~PATypeHandle() { removeUser(); }
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
); }
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
112 // operator= - Allow assignment to handle
113 inline const Type
*operator=(const PATypeHandle
&T
) {
114 return operator=(T
.Ty
);
117 inline bool operator==(const Type
*ty
) {
121 // operator-> - Allow user to dereference handle naturally...
122 inline const Type
*operator->() const { return Ty
; }
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.
132 mutable const Type
*Ty
;
134 PATypeHolder(const Type
*ty
) : Ty(ty
) {
137 PATypeHolder(const PATypeHolder
&T
) : Ty(T
.Ty
) {
141 ~PATypeHolder() { dropRef(); }
143 operator Type
*() const { return get(); }
146 // operator-> - Allow user to dereference handle naturally...
147 Type
*operator->() const { return get(); }
149 // operator= - Allow assignment to handle
150 Type
*operator=(const Type
*ty
) {
151 if (Ty
!= ty
) { // Don't accidentally drop last ref to Ty.
158 Type
*operator=(const PATypeHolder
&H
) {
159 return operator=(H
.Ty
);
162 /// getRawType - This should only be used to implement the vmcore library.
164 const Type
*getRawType() const { return Ty
; }
171 } // End llvm namespace