Propagation in TargetLowering. Includes passing a DL
[oota-llvm.git] / include / llvm / Support / Annotation.h
1 //===-- llvm/Support/Annotation.h - Annotation classes ----------*- 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 contains the declarations for two classes: Annotation & Annotable.
11 // Using these two simple classes, anything that derives from Annotable can have
12 // Annotation subclasses attached to them, ready for easy retrieval.
13 //
14 // Annotations are designed to be easily attachable to various classes.
15 //
16 // The AnnotationManager class is essential for using these classes.  It is
17 // responsible for turning Annotation name strings into tokens [unique id #'s]
18 // that may be used to search for and create annotations.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_SUPPORT_ANNOTATION_H
23 #define LLVM_SUPPORT_ANNOTATION_H
24
25 #include <cassert>
26
27 namespace llvm {
28
29 class AnnotationID;
30 class Annotation;
31 class Annotable;
32 struct AnnotationManager;
33
34 //===----------------------------------------------------------------------===//
35 //
36 // AnnotationID - This class is a thin wrapper around an unsigned integer that
37 // is used to hopefully prevent errors using AnnotationID's.  They may be copied
38 // freely around and passed byvalue with little or no overhead.
39 //
40 class AnnotationID {
41   friend struct AnnotationManager;
42   unsigned ID;
43
44   AnnotationID();                             // Default ctor is disabled
45
46   // AnnotationID is only creatable from AnnMgr.
47   explicit inline AnnotationID(unsigned i) : ID(i) {}
48 public:
49   inline AnnotationID(const AnnotationID &A) : ID(A.ID) {}
50
51   inline bool operator==(const AnnotationID &A) const {
52     return A.ID == ID;
53   }
54   inline bool operator<(const AnnotationID &A) const {
55     return ID < A.ID;
56   }
57 };
58
59
60 //===----------------------------------------------------------------------===//
61 //
62 // Annotation Class - This class serves as a base class for any specific
63 // annotations that you might need.  Simply subclass this to add extra
64 // information to the annotations.
65 //
66 class Annotation {
67   friend class Annotable;  // Annotable manipulates Next list
68   AnnotationID ID;         // ID number, as obtained from AnnotationManager
69   Annotation *Next;        // The next annotation in the linked list
70 public:
71   explicit inline Annotation(AnnotationID id) : ID(id), Next(0) {}
72   virtual ~Annotation();  // Designed to be subclassed
73
74   // getID - Return the unique ID# of this annotation
75   inline AnnotationID getID() const { return ID; }
76
77   // getNext - Return the next annotation in the list...
78   inline Annotation *getNext() const { return Next; }
79 };
80
81
82 //===----------------------------------------------------------------------===//
83 //
84 // Annotable - This class is used as a base class for all objects that would
85 // like to have annotation capability.
86 //
87 // Annotable objects keep their annotation list sorted as annotations are
88 // inserted and deleted.  This is used to ensure that annotations with identical
89 // ID#'s are stored sequentially.
90 //
91 class Annotable {
92   mutable Annotation *AnnotationList;
93
94   Annotable(const Annotable &);        // Do not implement
95   void operator=(const Annotable &);   // Do not implement
96 public:
97   Annotable() : AnnotationList(0) {}
98   ~Annotable();
99
100   // getAnnotation - Search the list for annotations of the specified ID.  The
101   // pointer returned is either null (if no annotations of the specified ID
102   // exist), or it points to the first element of a potentially list of elements
103   // with identical ID #'s.
104   //
105   Annotation *getAnnotation(AnnotationID ID) const {
106     for (Annotation *A = AnnotationList; A; A = A->getNext())
107       if (A->getID() == ID) return A;
108     return 0;
109   }
110
111   // getOrCreateAnnotation - Search through the annotation list, if there is
112   // no annotation with the specified ID, then use the AnnotationManager to
113   // create one.
114   //
115   inline Annotation *getOrCreateAnnotation(AnnotationID ID) const;
116
117   // addAnnotation - Insert the annotation into the list in a sorted location.
118   //
119   void addAnnotation(Annotation *A) const {
120     assert(A->Next == 0 && "Annotation already in list?!?");
121
122     Annotation **AL = &AnnotationList;
123     while (*AL && (*AL)->ID < A->getID())  // Find where to insert annotation
124       AL = &((*AL)->Next);
125     A->Next = *AL;                         // Link the annotation in
126     *AL = A;
127   }
128
129   // unlinkAnnotation - Remove the first annotation of the specified ID... and
130   // then return the unlinked annotation.  The annotation object is not deleted.
131   //
132   inline Annotation *unlinkAnnotation(AnnotationID ID) const {
133     for (Annotation **A = &AnnotationList; *A; A = &((*A)->Next))
134       if ((*A)->getID() == ID) {
135         Annotation *Ret = *A;
136         *A = Ret->Next;
137         Ret->Next = 0;
138         return Ret;
139       }
140     return 0;
141   }
142
143   // deleteAnnotation - Delete the first annotation of the specified ID in the
144   // list.  Unlink unlinkAnnotation, this actually deletes the annotation object
145   //
146   bool deleteAnnotation(AnnotationID ID) const {
147     Annotation *A = unlinkAnnotation(ID);
148     delete A;
149     return A != 0;
150   }
151 };
152
153
154 //===----------------------------------------------------------------------===//
155 //
156 // AnnotationManager - This class is primarily responsible for maintaining a
157 // one-to-one mapping between string Annotation names and Annotation ID numbers.
158 //
159 // Compared to the rest of the Annotation system, these mapping methods are
160 // relatively slow, so they should be avoided by locally caching Annotation
161 // ID #'s.  These methods are safe to call at any time, even by static ctors, so
162 // they should be used by static ctors most of the time.
163 //
164 // This class also provides support for annotations that are created on demand
165 // by the Annotable::getOrCreateAnnotation method.  To get this to work, simply
166 // register an annotation handler
167 //
168 struct AnnotationManager {
169   typedef Annotation *(*Factory)(AnnotationID, const Annotable *, void*);
170
171   //===--------------------------------------------------------------------===//
172   // Basic ID <-> Name map functionality
173
174   static AnnotationID  getID(const char *Name);  // Name -> ID
175   static const char *getName(AnnotationID ID);   // ID -> Name
176
177   // getID - Name -> ID + registration of a factory function for demand driven
178   // annotation support.
179   static AnnotationID getID(const char *Name, Factory Fact, void *Data = 0);
180
181   //===--------------------------------------------------------------------===//
182   // Annotation creation on demand support...
183
184   // registerAnnotationFactory - This method is used to register a callback
185   // function used to create an annotation on demand if it is needed by the
186   // Annotable::getOrCreateAnnotation method.
187   //
188   static void registerAnnotationFactory(AnnotationID ID, Factory Func,
189                                         void *ExtraData = 0);
190
191   // createAnnotation - Create an annotation of the specified ID for the
192   // specified object, using a register annotation creation function.
193   //
194   static Annotation *createAnnotation(AnnotationID ID, const Annotable *Obj);
195 };
196
197
198
199 // getOrCreateAnnotation - Search through the annotation list, if there is
200 // no annotation with the specified ID, then use the AnnotationManager to
201 // create one.
202 //
203 inline Annotation *Annotable::getOrCreateAnnotation(AnnotationID ID) const {
204   Annotation *A = getAnnotation(ID);   // Fast path, check for preexisting ann
205   if (A) return A;
206
207   // No annotation found, ask the annotation manager to create an annotation...
208   A = AnnotationManager::createAnnotation(ID, this);
209   assert(A && "AnnotationManager could not create annotation!");
210   addAnnotation(A);
211   return A;
212 }
213
214 } // End namespace llvm
215
216 #endif