1 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AnnotationManager class.
12 //===----------------------------------------------------------------------===//
15 #include "Support/Annotation.h"
18 typedef std::map<const std::string, unsigned> IDMapType;
19 static unsigned IDCounter = 0; // Unique ID counter
21 // Static member to ensure initialiation on demand.
22 static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
24 // On demand annotation creation support...
25 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
26 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
28 static FactMapType *TheFactMap = 0;
29 static FactMapType &getFactMap() {
31 TheFactMap = new FactMapType();
35 static void eraseFromFactMap(unsigned ID) {
36 assert(TheFactMap && "No entries found!");
37 TheFactMap->erase(ID);
38 if (TheFactMap->empty()) { // Delete when empty
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
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,
58 AnnotationID Result(getID(Name));
59 registerAnnotationFactory(Result, Fact, Data);
64 // getName - This function is especially slow, but that's okay because it should
65 // only be used for debugging.
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;
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.
80 void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
84 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
86 eraseFromFactMap(ID.ID);
89 // createAnnotation - Create an annotation of the specified ID for the
90 // specified object, using a register annotation creation function.
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);