Clean up Type class by removing mutable ConstRules member and use annotations insead
[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 // getID - Name -> ID + registration of a factory function for demand driven
32 // annotation support.
33 AnnotationID AnnotationManager::getID(const string &Name, Factory Fact,
34                                       void *Data=0) {
35   AnnotationID Result(getID(Name));
36   registerAnnotationFactory(Result, Fact, Data);
37   return Result;                      
38 }
39
40
41 // getName - This function is especially slow, but that's okay because it should
42 // only be used for debugging.
43 //
44 const string &AnnotationManager::getName(AnnotationID ID) {        // ID -> Name
45   IDMapType &TheMap = getIDMap();
46   for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
47     assert(I != TheMap.end() && "Annotation ID is unknown!");
48     if (I->second == ID.ID) return I->first;
49   }
50 }
51
52
53 // registerAnnotationFactory - This method is used to register a callback
54 // function used to create an annotation on demand if it is needed by the 
55 // Annotable::findOrCreateAnnotation method.
56 //
57 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, 
58                                                   AnnFactory F,
59                                                   void *ExtraData) {
60   if (F)
61     getFactMap()[ID.ID] = make_pair(F, ExtraData);
62   else
63     getFactMap().erase(ID.ID);
64 }
65
66 // createAnnotation - Create an annotation of the specified ID for the
67 // specified object, using a register annotation creation function.
68 //
69 Annotation *AnnotationManager::createAnnotation(AnnotationID ID, 
70                                                 const Annotable *Obj) {
71   FactMapType::iterator I = getFactMap().find(ID.ID);
72   if (I == getFactMap().end()) return 0;
73   return I->second.first(ID, Obj, I->second.second);
74 }