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