1 //===-- Annotation.cpp - Implement the Annotation Classes --------*- C++ -*--=//
3 // This file implements the AnnotationManager class.
5 //===----------------------------------------------------------------------===//
8 #include "llvm/Annotation.h"
10 typedef map<const 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 map<unsigned, pair<AnnFactory,void*> > FactMapType;
19 static FactMapType &getFactMap() { static FactMapType FactMap; return FactMap; }
22 AnnotationID AnnotationManager::getID(const string &Name) { // Name -> ID
23 IDMapType::iterator I = getIDMap().find(Name);
24 if (I == getIDMap().end()) {
25 getIDMap()[Name] = IDCounter++; // Add a new element
31 // getName - This function is especially slow, but that's okay because it should
32 // only be used for debugging.
34 const string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
35 IDMapType &TheMap = getIDMap();
36 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
37 assert(I != TheMap.end() && "Annotation ID is unknown!");
38 if (I->second == ID.ID) return I->first;
43 // registerAnnotationFactory - This method is used to register a callback
44 // function used to create an annotation on demand if it is needed by the
45 // Annotable::findOrCreateAnnotation method.
47 void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
51 getFactMap()[ID.ID] = make_pair(F, ExtraData);
53 getFactMap().erase(ID.ID);
56 // createAnnotation - Create an annotation of the specified ID for the
57 // specified object, using a register annotation creation function.
59 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
60 const Annotable *Obj) {
61 FactMapType::iterator I = getFactMap().find(ID.ID);
62 if (I == getFactMap().end()) return 0;
63 return I->second.first(ID, Obj, I->second.second);