96ead521f50aeab14549d0929d07b732d8d7aa50
[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 using std::string;
10 using std::map;
11 using std::pair;
12 using std::make_pair;
13
14 typedef map<const string, unsigned> IDMapType;
15 static unsigned IDCounter = 0;  // Unique ID counter
16
17 // Static member to ensure initialiation on demand.
18 static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
19
20 // On demand annotation creation support...
21 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
22 typedef map<unsigned, pair<AnnFactory,void*> > FactMapType;
23
24 static FactMapType *TheFactMap = 0;
25 static FactMapType &getFactMap() {
26   if (TheFactMap == 0)
27     TheFactMap = new FactMapType();
28   return *TheFactMap;
29 }
30
31 static void eraseFromFactMap(unsigned ID) {
32   assert(TheFactMap && "No entries found!");
33   TheFactMap->erase(ID);
34   if (TheFactMap->empty()) {   // Delete when empty
35     delete TheFactMap;
36     TheFactMap = 0;
37   }
38 }
39
40
41 AnnotationID AnnotationManager::getID(const string &Name) {  // Name -> ID
42   IDMapType::iterator I = getIDMap().find(Name);
43   if (I == getIDMap().end()) {
44     getIDMap()[Name] = IDCounter++;   // Add a new element
45     return IDCounter-1;
46   }
47   return I->second;
48 }
49
50 // getID - Name -> ID + registration of a factory function for demand driven
51 // annotation support.
52 AnnotationID AnnotationManager::getID(const string &Name, Factory Fact,
53                                       void *Data) {
54   AnnotationID Result(getID(Name));
55   registerAnnotationFactory(Result, Fact, Data);
56   return Result;                      
57 }
58
59
60 // getName - This function is especially slow, but that's okay because it should
61 // only be used for debugging.
62 //
63 const string &AnnotationManager::getName(AnnotationID ID) {        // ID -> Name
64   IDMapType &TheMap = getIDMap();
65   for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
66     assert(I != TheMap.end() && "Annotation ID is unknown!");
67     if (I->second == ID.ID) return I->first;
68   }
69 }
70
71
72 // registerAnnotationFactory - This method is used to register a callback
73 // function used to create an annotation on demand if it is needed by the 
74 // Annotable::findOrCreateAnnotation method.
75 //
76 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, 
77                                                   AnnFactory F,
78                                                   void *ExtraData) {
79   if (F)
80     getFactMap()[ID.ID] = make_pair(F, ExtraData);
81   else
82     eraseFromFactMap(ID.ID);
83 }
84
85 // createAnnotation - Create an annotation of the specified ID for the
86 // specified object, using a register annotation creation function.
87 //
88 Annotation *AnnotationManager::createAnnotation(AnnotationID ID, 
89                                                 const Annotable *Obj) {
90   FactMapType::iterator I = getFactMap().find(ID.ID);
91   if (I == getFactMap().end()) return 0;
92   return I->second.first(ID, Obj, I->second.second);
93 }