1 //===-- Support/Annotation.h - Annotation classes ---------------*- C++ -*-===//
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.
7 // Annotations are designed to be easily attachable to various classes.
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef SUPPORT_ANNOTATION_H
16 #define SUPPORT_ANNOTATION_H
22 class AnnotationManager;
24 //===----------------------------------------------------------------------===//
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.
31 friend class AnnotationManager;
34 AnnotationID(); // Default ctor is disabled
35 inline AnnotationID(unsigned i) : ID(i) {} // Only creatable from AnnMgr
37 inline AnnotationID(const AnnotationID &A) : ID(A.ID) {}
39 inline bool operator==(const AnnotationID &A) const {
42 inline bool operator<(const AnnotationID &A) const {
48 //===----------------------------------------------------------------------===//
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.
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
59 inline Annotation(AnnotationID id) : ID(id), Next(0) {}
60 virtual ~Annotation() {} // Designed to be subclassed
62 // getID - Return the unique ID# of this annotation
63 inline AnnotationID getID() const { return ID; }
65 // getNext - Return the next annotation in the list...
66 inline Annotation *getNext() const { return Next; }
70 //===----------------------------------------------------------------------===//
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.
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.
81 mutable Annotation *AnnotationList;
83 Annotable(const Annotable &); // Do not implement
84 void operator=(const Annotable &); // Do not implement
86 Annotable() : AnnotationList(0) {}
87 virtual ~Annotable() { // Virtual because it's designed to be subclassed...
88 Annotation *A = AnnotationList;
90 Annotation *Next = A->getNext();
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.
101 Annotation *getAnnotation(AnnotationID ID) const {
102 for (Annotation *A = AnnotationList; A; A = A->getNext())
103 if (A->getID() == ID) return A;
107 // getOrCreateAnnotation - Search through the annotation list, if there is
108 // no annotation with the specified ID, then use the AnnotationManager to
111 inline Annotation *getOrCreateAnnotation(AnnotationID ID) const;
113 // addAnnotation - Insert the annotation into the list in a sorted location.
115 void addAnnotation(Annotation *A) const {
116 assert(A->Next == 0 && "Annotation already in list?!?");
118 Annotation **AL = &AnnotationList;
119 while (*AL && (*AL)->ID < A->getID()) // Find where to insert annotation
121 A->Next = *AL; // Link the annotation in
125 // unlinkAnnotation - Remove the first annotation of the specified ID... and
126 // then return the unlinked annotation. The annotation object is not deleted.
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;
139 // deleteAnnotation - Delete the first annotation of the specified ID in the
140 // list. Unlink unlinkAnnotation, this actually deletes the annotation object
142 bool deleteAnnotation(AnnotationID ID) const {
143 Annotation *A = unlinkAnnotation(ID);
150 //===----------------------------------------------------------------------===//
152 // AnnotationManager - This class is primarily responsible for maintaining a
153 // one-to-one mapping between string Annotation names and Annotation ID numbers.
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.
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
164 struct AnnotationManager {
165 typedef Annotation *(*Factory)(AnnotationID, const Annotable *, void*);
167 //===--------------------------------------------------------------------===//
168 // Basic ID <-> Name map functionality
170 static AnnotationID getID(const std::string &Name); // Name -> ID
171 static const std::string &getName(AnnotationID ID); // ID -> Name
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,
178 //===--------------------------------------------------------------------===//
179 // Annotation creation on demand support...
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.
185 static void registerAnnotationFactory(AnnotationID ID, Factory Func,
186 void *ExtraData = 0);
188 // createAnnotation - Create an annotation of the specified ID for the
189 // specified object, using a register annotation creation function.
191 static Annotation *createAnnotation(AnnotationID ID, const Annotable *Obj);
196 // getOrCreateAnnotation - Search through the annotation list, if there is
197 // no annotation with the specified ID, then use the AnnotationManager to
200 inline Annotation *Annotable::getOrCreateAnnotation(AnnotationID ID) const {
201 Annotation *A = getAnnotation(ID); // Fast path, check for preexisting ann
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!");