1 //===-- llvm/Support/Annotation.h - Annotation classes ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 // Annotations are designed to be easily attachable to various classes.
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.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_SUPPORT_ANNOTATION_H
23 #define LLVM_SUPPORT_ANNOTATION_H
33 struct AnnotationManager;
35 //===----------------------------------------------------------------------===//
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.
42 friend struct AnnotationManager;
45 AnnotationID(); // Default ctor is disabled
47 // AnnotationID is only creatable from AnnMgr.
48 explicit inline AnnotationID(unsigned i) : ID(i) {}
50 inline AnnotationID(const AnnotationID &A) : ID(A.ID) {}
52 inline bool operator==(const AnnotationID &A) const {
55 inline bool operator<(const AnnotationID &A) const {
61 //===----------------------------------------------------------------------===//
63 // Annotation Class - This class serves as a base class for any specific
64 // annotations that you might need. Simply subclass this to add extra
65 // information to the annotations.
68 friend class Annotable; // Annotable manipulates Next list
69 AnnotationID ID; // ID number, as obtained from AnnotationManager
70 Annotation *Next; // The next annotation in the linked list
72 explicit inline Annotation(AnnotationID id) : ID(id), Next(0) {}
73 virtual ~Annotation(); // Designed to be subclassed
75 // getID - Return the unique ID# of this annotation
76 inline AnnotationID getID() const { return ID; }
78 // getNext - Return the next annotation in the list...
79 inline Annotation *getNext() const { return Next; }
83 //===----------------------------------------------------------------------===//
85 // Annotable - This class is used as a base class for all objects that would
86 // like to have annotation capability.
88 // Annotable objects keep their annotation list sorted as annotations are
89 // inserted and deleted. This is used to ensure that annotations with identical
90 // ID#'s are stored sequentially.
93 mutable Annotation *AnnotationList;
95 Annotable(const Annotable &); // Do not implement
96 void operator=(const Annotable &); // Do not implement
98 Annotable() : AnnotationList(0) {}
101 // getAnnotation - Search the list for annotations of the specified ID. The
102 // pointer returned is either null (if no annotations of the specified ID
103 // exist), or it points to the first element of a potentially list of elements
104 // with identical ID #'s.
106 Annotation *getAnnotation(AnnotationID ID) const {
107 for (Annotation *A = AnnotationList; A; A = A->getNext())
108 if (A->getID() == ID) return A;
112 // getOrCreateAnnotation - Search through the annotation list, if there is
113 // no annotation with the specified ID, then use the AnnotationManager to
116 inline Annotation *getOrCreateAnnotation(AnnotationID ID) const;
118 // addAnnotation - Insert the annotation into the list in a sorted location.
120 void addAnnotation(Annotation *A) const {
121 assert(A->Next == 0 && "Annotation already in list?!?");
123 Annotation **AL = &AnnotationList;
124 while (*AL && (*AL)->ID < A->getID()) // Find where to insert annotation
126 A->Next = *AL; // Link the annotation in
130 // unlinkAnnotation - Remove the first annotation of the specified ID... and
131 // then return the unlinked annotation. The annotation object is not deleted.
133 inline Annotation *unlinkAnnotation(AnnotationID ID) const {
134 for (Annotation **A = &AnnotationList; *A; A = &((*A)->Next))
135 if ((*A)->getID() == ID) {
136 Annotation *Ret = *A;
144 // deleteAnnotation - Delete the first annotation of the specified ID in the
145 // list. Unlink unlinkAnnotation, this actually deletes the annotation object
147 bool deleteAnnotation(AnnotationID ID) const {
148 Annotation *A = unlinkAnnotation(ID);
155 //===----------------------------------------------------------------------===//
157 // AnnotationManager - This class is primarily responsible for maintaining a
158 // one-to-one mapping between string Annotation names and Annotation ID numbers.
160 // Compared to the rest of the Annotation system, these mapping methods are
161 // relatively slow, so they should be avoided by locally caching Annotation
162 // ID #'s. These methods are safe to call at any time, even by static ctors, so
163 // they should be used by static ctors most of the time.
165 // This class also provides support for annotations that are created on demand
166 // by the Annotable::getOrCreateAnnotation method. To get this to work, simply
167 // register an annotation handler
169 struct AnnotationManager {
170 typedef Annotation *(*Factory)(AnnotationID, const Annotable *, void*);
172 //===--------------------------------------------------------------------===//
173 // Basic ID <-> Name map functionality
175 static AnnotationID getID(const std::string &Name); // Name -> ID
176 static const std::string &getName(AnnotationID ID); // ID -> Name
178 // getID - Name -> ID + registration of a factory function for demand driven
179 // annotation support.
180 static AnnotationID getID(const std::string &Name, Factory Fact,
183 //===--------------------------------------------------------------------===//
184 // Annotation creation on demand support...
186 // registerAnnotationFactory - This method is used to register a callback
187 // function used to create an annotation on demand if it is needed by the
188 // Annotable::getOrCreateAnnotation method.
190 static void registerAnnotationFactory(AnnotationID ID, Factory Func,
191 void *ExtraData = 0);
193 // createAnnotation - Create an annotation of the specified ID for the
194 // specified object, using a register annotation creation function.
196 static Annotation *createAnnotation(AnnotationID ID, const Annotable *Obj);
201 // getOrCreateAnnotation - Search through the annotation list, if there is
202 // no annotation with the specified ID, then use the AnnotationManager to
205 inline Annotation *Annotable::getOrCreateAnnotation(AnnotationID ID) const {
206 Annotation *A = getAnnotation(ID); // Fast path, check for preexisting ann
209 // No annotation found, ask the annotation manager to create an annotation...
210 A = AnnotationManager::createAnnotation(ID, this);
211 assert(A && "AnnotationManager could not create annotation!");
216 } // End namespace llvm