1 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
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 implements the AnnotationManager class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Annotation.h"
15 #include "llvm/Support/ManagedStatic.h"
19 Annotation::~Annotation() {} // Designed to be subclassed
21 Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
22 Annotation *A = AnnotationList;
24 Annotation *Next = A->getNext();
33 bool operator()(const char *a, const char *b) {
34 return strcmp(a, b) < 0;
39 typedef std::map<const char*, unsigned, StrCmp> IDMapType;
40 static unsigned IDCounter = 0; // Unique ID counter
42 // Static member to ensure initialiation on demand.
43 static ManagedStatic<IDMapType> IDMap;
45 // On demand annotation creation support...
46 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
47 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
49 static FactMapType *TheFactMap = 0;
50 static FactMapType &getFactMap() {
52 TheFactMap = new FactMapType();
56 static void eraseFromFactMap(unsigned ID) {
57 assert(TheFactMap && "No entries found!");
58 TheFactMap->erase(ID);
59 if (TheFactMap->empty()) { // Delete when empty
65 AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
66 IDMapType::iterator I = IDMap->find(Name);
67 if (I == IDMap->end()) {
68 (*IDMap)[Name] = IDCounter++; // Add a new element
69 return AnnotationID(IDCounter-1);
71 return AnnotationID(I->second);
74 // getID - Name -> ID + registration of a factory function for demand driven
75 // annotation support.
76 AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
78 AnnotationID Result(getID(Name));
79 registerAnnotationFactory(Result, Fact, Data);
83 // getName - This function is especially slow, but that's okay because it should
84 // only be used for debugging.
86 const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
87 IDMapType &TheMap = *IDMap;
88 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
89 assert(I != TheMap.end() && "Annotation ID is unknown!");
90 if (I->second == ID.ID) return I->first;
94 // registerAnnotationFactory - This method is used to register a callback
95 // function used to create an annotation on demand if it is needed by the
96 // Annotable::findOrCreateAnnotation method.
98 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
101 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
103 eraseFromFactMap(ID.ID);
106 // createAnnotation - Create an annotation of the specified ID for the
107 // specified object, using a register annotation creation function.
109 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
110 const Annotable *Obj) {
111 FactMapType::iterator I = getFactMap().find(ID.ID);
112 if (I == getFactMap().end()) return 0;
113 return I->second.first(ID, Obj, I->second.second);