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 // 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.
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.
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.
26 //===----------------------------------------------------------------------===//
28 #ifndef LLVM_ABSTRACT_TYPE_USER_H
29 #define LLVM_ABSTRACT_TYPE_USER_H
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
35 // In this way, most every LLVM source file will have access to the assert()
36 // macro without having to #include <cassert> directly.
43 class AbstractTypeUser {
45 virtual ~AbstractTypeUser() {} // Derive from me
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.
52 virtual void refineAbstractType(const DerivedType *OldTy,
53 const Type *NewTy) = 0;
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.
60 virtual void typeBecameConcrete(const DerivedType *AbsTy) = 0;
63 virtual void dump() const = 0;
67 /// PATypeHandle - Handle to a Type subclass. This class is used to keep the
68 /// use list of abstract types up-to-date.
72 AbstractTypeUser * const User;
74 // These functions are defined at the bottom of Type.h. See the comment there
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) {
85 // ctor - Add use to type if abstract.
86 inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
90 // dtor - Remove reference to type...
91 inline ~PATypeHandle() { removeUser(); }
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; }
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
107 // operator= - Allow assignment to handle
108 inline const Type *operator=(const PATypeHandle &T) {
109 return operator=(T.Ty);
112 inline bool operator==(const Type *ty) {
116 // operator-> - Allow user to dereference handle naturally...
117 inline const Type *operator->() const { return Ty; }
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.
125 void removeUserFromConcrete();
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.
135 mutable const Type *Ty;
137 PATypeHolder(const Type *ty) : Ty(ty) {
140 PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) {
144 operator const Type *() const { return get(); }
145 const Type *get() const;
147 // operator-> - Allow user to dereference handle naturally...
148 const Type *operator->() const { return get(); }
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.
159 const Type *operator=(const PATypeHolder &H) {
160 return operator=(H.Ty);