Annotations are const objects now
[oota-llvm.git] / lib / Support / Annotation.cpp
1 //===-- Annotation.cpp - Implement the Annotation Classes --------*- C++ -*--=//
2 //
3 // This file implements the AnnotationManager class.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include <map>
8 #include "llvm/Annotation.h"
9
10 typedef map<const string, unsigned> IDMapType;
11 static unsigned IDCounter = 0;  // Unique ID counter
12
13 // Static member to ensure initialiation on demand.
14 static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
15
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; }
20
21
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
26     return IDCounter-1;
27   }
28   return I->second;
29 }
30
31 // getName - This function is especially slow, but that's okay because it should
32 // only be used for debugging.
33 //
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;
39   }
40 }
41
42
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.
46 //
47 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, 
48                                                   AnnFactory F,
49                                                   void *ExtraData) {
50   if (F)
51     getFactMap()[ID.ID] = make_pair(F, ExtraData);
52   else
53     getFactMap().erase(ID.ID);
54 }
55
56 // createAnnotation - Create an annotation of the specified ID for the
57 // specified object, using a register annotation creation function.
58 //
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);
64 }