Simplify implementation, and probably speed things up too.
[oota-llvm.git] / lib / Support / Annotation.cpp
1 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the AnnotationManager class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include <map>
15 #include "Support/Annotation.h"
16 using namespace llvm;
17
18 typedef std::map<const std::string, unsigned> IDMapType;
19 static unsigned IDCounter = 0;  // Unique ID counter
20
21 // Static member to ensure initialiation on demand.
22 static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
23
24 // On demand annotation creation support...
25 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
26 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
27
28 static FactMapType *TheFactMap = 0;
29 static FactMapType &getFactMap() {
30   if (TheFactMap == 0)
31     TheFactMap = new FactMapType();
32   return *TheFactMap;
33 }
34
35 static void eraseFromFactMap(unsigned ID) {
36   assert(TheFactMap && "No entries found!");
37   TheFactMap->erase(ID);
38   if (TheFactMap->empty()) {   // Delete when empty
39     delete TheFactMap;
40     TheFactMap = 0;
41   }
42 }
43
44
45 AnnotationID AnnotationManager::getID(const std::string &Name) {  // Name -> ID
46   IDMapType::iterator I = getIDMap().find(Name);
47   if (I == getIDMap().end()) {
48     getIDMap()[Name] = IDCounter++;   // Add a new element
49     return IDCounter-1;
50   }
51   return I->second;
52 }
53
54 // getID - Name -> ID + registration of a factory function for demand driven
55 // annotation support.
56 AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
57                                       void *Data) {
58   AnnotationID Result(getID(Name));
59   registerAnnotationFactory(Result, Fact, Data);
60   return Result;                      
61 }
62
63
64 // getName - This function is especially slow, but that's okay because it should
65 // only be used for debugging.
66 //
67 const std::string &AnnotationManager::getName(AnnotationID ID) {  // ID -> Name
68   IDMapType &TheMap = getIDMap();
69   for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
70     assert(I != TheMap.end() && "Annotation ID is unknown!");
71     if (I->second == ID.ID) return I->first;
72   }
73 }
74
75
76 // registerAnnotationFactory - This method is used to register a callback
77 // function used to create an annotation on demand if it is needed by the 
78 // Annotable::findOrCreateAnnotation method.
79 //
80 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, 
81                                                   AnnFactory F,
82                                                   void *ExtraData) {
83   if (F)
84     getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
85   else
86     eraseFromFactMap(ID.ID);
87 }
88
89 // createAnnotation - Create an annotation of the specified ID for the
90 // specified object, using a register annotation creation function.
91 //
92 Annotation *AnnotationManager::createAnnotation(AnnotationID ID, 
93                                                 const Annotable *Obj) {
94   FactMapType::iterator I = getFactMap().find(ID.ID);
95   if (I == getFactMap().end()) return 0;
96   return I->second.first(ID, Obj, I->second.second);
97 }