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