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