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.
45 class AbstractTypeUser {
47 virtual ~AbstractTypeUser(); // Derive from me
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.
54 virtual void refineAbstractType(const DerivedType *OldTy,
55 const Type *NewTy) = 0;
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.
62 virtual void typeBecameConcrete(const DerivedType *AbsTy) = 0;
65 virtual void dump() const = 0;
69 /// PATypeHandle - Handle to a Type subclass. This class is used to keep the
70 /// use list of abstract types up-to-date.
74 AbstractTypeUser * const User;
76 // These functions are defined at the bottom of Type.h. See the comment there
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) {
87 // ctor - Add use to type if abstract.
88 inline PATypeHandle(const PATypeHandle &T) : Ty(T.Ty), User(T.User) {
92 // dtor - Remove reference to type...
93 inline ~PATypeHandle() { removeUser(); }
95 // Automatic casting operator so that the handle may be used naturally
96 inline operator const Type *() const { return Ty; }
97 inline const Type *get() const { return Ty; }
99 // operator= - Allow assignment to handle
100 inline const Type *operator=(const Type *ty) {
101 if (Ty != ty) { // Ensure we don't accidentally drop last ref to Ty
109 // operator= - Allow assignment to handle
110 inline const Type *operator=(const PATypeHandle &T) {
111 return operator=(T.Ty);
114 inline bool operator==(const Type *ty) {
118 // operator-> - Allow user to dereference handle naturally...
119 inline const Type *operator->() const { return Ty; }
121 // removeUserFromConcrete - This function should be called when the User is
122 // notified that our type is refined... and the type is being refined to
123 // itself, which is now a concrete type. When a type becomes concrete like
124 // this, we MUST remove ourself from the AbstractTypeUser list, even though
125 // the type is apparently concrete.
127 void removeUserFromConcrete();
131 /// PATypeHolder - Holder class for a potentially abstract type. This uses
132 /// efficient union-find techniques to handle dynamic type resolution. Unless
133 /// you need to do custom processing when types are resolved, you should always
134 /// use PATypeHolders in preference to PATypeHandles.
137 mutable const Type *Ty;
139 PATypeHolder(const Type *ty) : Ty(ty) {
142 PATypeHolder(const PATypeHolder &T) : Ty(T.Ty) {
146 ~PATypeHolder() { dropRef(); }
148 operator const Type *() const { return get(); }
149 const Type *get() const;
151 // operator-> - Allow user to dereference handle naturally...
152 const Type *operator->() const { return get(); }
154 // operator= - Allow assignment to handle
155 const Type *operator=(const Type *ty) {
156 if (Ty != ty) { // Don't accidentally drop last ref to Ty.
163 const Type *operator=(const PATypeHolder &H) {
164 return operator=(H.Ty);
172 } // End llvm namespace