1 //===-- Annotation.cpp - Implement the Annotation Classes --------*- C++ -*--=//
3 // This file implements the AnnotationManager class.
5 //===----------------------------------------------------------------------===//
8 #include "Support/Annotation.h"
10 typedef std::map<const std::string, unsigned> IDMapType;
11 static unsigned IDCounter = 0; // Unique ID counter
13 // Static member to ensure initialiation on demand.
14 static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
16 // On demand annotation creation support...
17 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
18 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
20 static FactMapType *TheFactMap = 0;
21 static FactMapType &getFactMap() {
23 TheFactMap = new FactMapType();
27 static void eraseFromFactMap(unsigned ID) {
28 assert(TheFactMap && "No entries found!");
29 TheFactMap->erase(ID);
30 if (TheFactMap->empty()) { // Delete when empty
37 AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID
38 IDMapType::iterator I = getIDMap().find(Name);
39 if (I == getIDMap().end()) {
40 getIDMap()[Name] = IDCounter++; // Add a new element
46 // getID - Name -> ID + registration of a factory function for demand driven
47 // annotation support.
48 AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
50 AnnotationID Result(getID(Name));
51 registerAnnotationFactory(Result, Fact, Data);
56 // getName - This function is especially slow, but that's okay because it should
57 // only be used for debugging.
59 const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
60 IDMapType &TheMap = getIDMap();
61 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
62 assert(I != TheMap.end() && "Annotation ID is unknown!");
63 if (I->second == ID.ID) return I->first;
68 // registerAnnotationFactory - This method is used to register a callback
69 // function used to create an annotation on demand if it is needed by the
70 // Annotable::findOrCreateAnnotation method.
72 void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
76 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
78 eraseFromFactMap(ID.ID);
81 // createAnnotation - Create an annotation of the specified ID for the
82 // specified object, using a register annotation creation function.
84 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
85 const Annotable *Obj) {
86 FactMapType::iterator I = getFactMap().find(ID.ID);
87 if (I == getFactMap().end()) return 0;
88 return I->second.first(ID, Obj, I->second.second);