Revert back to keeping Burg and TableGen in the utils directory
[oota-llvm.git] / support / 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 "Support/Annotation.h"
9
10 typedef std::map<const std::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 std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
19
20 static FactMapType *TheFactMap = 0;
21 static FactMapType &getFactMap() {
22   if (TheFactMap == 0)
23     TheFactMap = new FactMapType();
24   return *TheFactMap;
25 }
26
27 static void eraseFromFactMap(unsigned ID) {
28   assert(TheFactMap && "No entries found!");
29   TheFactMap->erase(ID);
30   if (TheFactMap->empty()) {   // Delete when empty
31     delete TheFactMap;
32     TheFactMap = 0;
33   }
34 }
35
36
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
41     return IDCounter-1;
42   }
43   return I->second;
44 }
45
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,
49                                       void *Data) {
50   AnnotationID Result(getID(Name));
51   registerAnnotationFactory(Result, Fact, Data);
52   return Result;                      
53 }
54
55
56 // getName - This function is especially slow, but that's okay because it should
57 // only be used for debugging.
58 //
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;
64   }
65 }
66
67
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.
71 //
72 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, 
73                                                   AnnFactory F,
74                                                   void *ExtraData) {
75   if (F)
76     getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
77   else
78     eraseFromFactMap(ID.ID);
79 }
80
81 // createAnnotation - Create an annotation of the specified ID for the
82 // specified object, using a register annotation creation function.
83 //
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);
89 }