LLVM API Documentation
00001 //===-- llvm/Support/Annotation.h - Annotation classes ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the declarations for two classes: Annotation & Annotable. 00011 // Using these two simple classes, anything that derives from Annotable can have 00012 // Annotation subclasses attached to them, ready for easy retrieval. 00013 // 00014 // Annotations are designed to be easily attachable to various classes. 00015 // 00016 // The AnnotationManager class is essential for using these classes. It is 00017 // responsible for turning Annotation name strings into tokens [unique id #'s] 00018 // that may be used to search for and create annotations. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #ifndef LLVM_SUPPORT_ANNOTATION_H 00023 #define LLVM_SUPPORT_ANNOTATION_H 00024 00025 #include <string> 00026 #include <cassert> 00027 00028 namespace llvm { 00029 00030 class AnnotationID; 00031 class Annotation; 00032 class Annotable; 00033 struct AnnotationManager; 00034 00035 //===----------------------------------------------------------------------===// 00036 // 00037 // AnnotationID - This class is a thin wrapper around an unsigned integer that 00038 // is used to hopefully prevent errors using AnnotationID's. They may be copied 00039 // freely around and passed byvalue with little or no overhead. 00040 // 00041 class AnnotationID { 00042 friend struct AnnotationManager; 00043 unsigned ID; 00044 00045 AnnotationID(); // Default ctor is disabled 00046 00047 // AnnotationID is only creatable from AnnMgr. 00048 explicit inline AnnotationID(unsigned i) : ID(i) {} 00049 public: 00050 inline AnnotationID(const AnnotationID &A) : ID(A.ID) {} 00051 00052 inline bool operator==(const AnnotationID &A) const { 00053 return A.ID == ID; 00054 } 00055 inline bool operator<(const AnnotationID &A) const { 00056 return ID < A.ID; 00057 } 00058 }; 00059 00060 00061 //===----------------------------------------------------------------------===// 00062 // 00063 // Annotation Class - This class serves as a base class for any specific 00064 // annotations that you might need. Simply subclass this to add extra 00065 // information to the annotations. 00066 // 00067 class Annotation { 00068 friend class Annotable; // Annotable manipulates Next list 00069 AnnotationID ID; // ID number, as obtained from AnnotationManager 00070 Annotation *Next; // The next annotation in the linked list 00071 public: 00072 explicit inline Annotation(AnnotationID id) : ID(id), Next(0) {} 00073 virtual ~Annotation(); // Designed to be subclassed 00074 00075 // getID - Return the unique ID# of this annotation 00076 inline AnnotationID getID() const { return ID; } 00077 00078 // getNext - Return the next annotation in the list... 00079 inline Annotation *getNext() const { return Next; } 00080 }; 00081 00082 00083 //===----------------------------------------------------------------------===// 00084 // 00085 // Annotable - This class is used as a base class for all objects that would 00086 // like to have annotation capability. 00087 // 00088 // Annotable objects keep their annotation list sorted as annotations are 00089 // inserted and deleted. This is used to ensure that annotations with identical 00090 // ID#'s are stored sequentially. 00091 // 00092 class Annotable { 00093 mutable Annotation *AnnotationList; 00094 00095 Annotable(const Annotable &); // Do not implement 00096 void operator=(const Annotable &); // Do not implement 00097 public: 00098 Annotable() : AnnotationList(0) {} 00099 ~Annotable(); 00100 00101 // getAnnotation - Search the list for annotations of the specified ID. The 00102 // pointer returned is either null (if no annotations of the specified ID 00103 // exist), or it points to the first element of a potentially list of elements 00104 // with identical ID #'s. 00105 // 00106 Annotation *getAnnotation(AnnotationID ID) const { 00107 for (Annotation *A = AnnotationList; A; A = A->getNext()) 00108 if (A->getID() == ID) return A; 00109 return 0; 00110 } 00111 00112 // getOrCreateAnnotation - Search through the annotation list, if there is 00113 // no annotation with the specified ID, then use the AnnotationManager to 00114 // create one. 00115 // 00116 inline Annotation *getOrCreateAnnotation(AnnotationID ID) const; 00117 00118 // addAnnotation - Insert the annotation into the list in a sorted location. 00119 // 00120 void addAnnotation(Annotation *A) const { 00121 assert(A->Next == 0 && "Annotation already in list?!?"); 00122 00123 Annotation **AL = &AnnotationList; 00124 while (*AL && (*AL)->ID < A->getID()) // Find where to insert annotation 00125 AL = &((*AL)->Next); 00126 A->Next = *AL; // Link the annotation in 00127 *AL = A; 00128 } 00129 00130 // unlinkAnnotation - Remove the first annotation of the specified ID... and 00131 // then return the unlinked annotation. The annotation object is not deleted. 00132 // 00133 inline Annotation *unlinkAnnotation(AnnotationID ID) const { 00134 for (Annotation **A = &AnnotationList; *A; A = &((*A)->Next)) 00135 if ((*A)->getID() == ID) { 00136 Annotation *Ret = *A; 00137 *A = Ret->Next; 00138 Ret->Next = 0; 00139 return Ret; 00140 } 00141 return 0; 00142 } 00143 00144 // deleteAnnotation - Delete the first annotation of the specified ID in the 00145 // list. Unlink unlinkAnnotation, this actually deletes the annotation object 00146 // 00147 bool deleteAnnotation(AnnotationID ID) const { 00148 Annotation *A = unlinkAnnotation(ID); 00149 delete A; 00150 return A != 0; 00151 } 00152 }; 00153 00154 00155 //===----------------------------------------------------------------------===// 00156 // 00157 // AnnotationManager - This class is primarily responsible for maintaining a 00158 // one-to-one mapping between string Annotation names and Annotation ID numbers. 00159 // 00160 // Compared to the rest of the Annotation system, these mapping methods are 00161 // relatively slow, so they should be avoided by locally caching Annotation 00162 // ID #'s. These methods are safe to call at any time, even by static ctors, so 00163 // they should be used by static ctors most of the time. 00164 // 00165 // This class also provides support for annotations that are created on demand 00166 // by the Annotable::getOrCreateAnnotation method. To get this to work, simply 00167 // register an annotation handler 00168 // 00169 struct AnnotationManager { 00170 typedef Annotation *(*Factory)(AnnotationID, const Annotable *, void*); 00171 00172 //===--------------------------------------------------------------------===// 00173 // Basic ID <-> Name map functionality 00174 00175 static AnnotationID getID(const std::string &Name); // Name -> ID 00176 static const std::string &getName(AnnotationID ID); // ID -> Name 00177 00178 // getID - Name -> ID + registration of a factory function for demand driven 00179 // annotation support. 00180 static AnnotationID getID(const std::string &Name, Factory Fact, 00181 void *Data = 0); 00182 00183 //===--------------------------------------------------------------------===// 00184 // Annotation creation on demand support... 00185 00186 // registerAnnotationFactory - This method is used to register a callback 00187 // function used to create an annotation on demand if it is needed by the 00188 // Annotable::getOrCreateAnnotation method. 00189 // 00190 static void registerAnnotationFactory(AnnotationID ID, Factory Func, 00191 void *ExtraData = 0); 00192 00193 // createAnnotation - Create an annotation of the specified ID for the 00194 // specified object, using a register annotation creation function. 00195 // 00196 static Annotation *createAnnotation(AnnotationID ID, const Annotable *Obj); 00197 }; 00198 00199 00200 00201 // getOrCreateAnnotation - Search through the annotation list, if there is 00202 // no annotation with the specified ID, then use the AnnotationManager to 00203 // create one. 00204 // 00205 inline Annotation *Annotable::getOrCreateAnnotation(AnnotationID ID) const { 00206 Annotation *A = getAnnotation(ID); // Fast path, check for preexisting ann 00207 if (A) return A; 00208 00209 // No annotation found, ask the annotation manager to create an annotation... 00210 A = AnnotationManager::createAnnotation(ID, this); 00211 assert(A && "AnnotationManager could not create annotation!"); 00212 addAnnotation(A); 00213 return A; 00214 } 00215 00216 } // End namespace llvm 00217 00218 #endif