blob: 65a049d42dcc19a18988e7e4abca291e918bbdca [file] [log] [blame]
Chris Lattner8dc89a32001-08-23 17:07:56 +00001//===-- 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
10typedef map<const string, unsigned> IDMapType;
11static unsigned IDCounter = 0; // Unique ID counter
12
13// Static member to ensure initialiation on demand.
14static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
15
16// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000017typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattnerda8f0042001-08-27 05:19:10 +000018typedef map<unsigned, pair<AnnFactory,void*> > FactMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000019static FactMapType &getFactMap() { static FactMapType FactMap; return FactMap; }
20
21
22AnnotationID 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
Chris Lattner82072d42001-09-09 21:02:38 +000031// getID - Name -> ID + registration of a factory function for demand driven
32// annotation support.
33AnnotationID 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
Chris Lattner8dc89a32001-08-23 17:07:56 +000041// getName - This function is especially slow, but that's okay because it should
42// only be used for debugging.
43//
44const 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//
57void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
Chris Lattnerda8f0042001-08-27 05:19:10 +000058 AnnFactory F,
59 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000060 if (F)
Chris Lattnerda8f0042001-08-27 05:19:10 +000061 getFactMap()[ID.ID] = make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +000062 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//
69Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Chris Lattnerc0f483d2001-09-07 16:44:01 +000070 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000071 FactMapType::iterator I = getFactMap().find(ID.ID);
72 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +000073 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000074}