blob: 9f24607af7c2e5ca54710847f08b8512d81aef81 [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>
Chris Lattnercb09cc22003-01-14 21:29:58 +00008#include "Support/Annotation.h"
Chris Lattner697954c2002-01-20 22:54:45 +00009using std::string;
10using std::map;
11using std::pair;
12using std::make_pair;
Chris Lattner8dc89a32001-08-23 17:07:56 +000013
14typedef map<const string, unsigned> IDMapType;
15static unsigned IDCounter = 0; // Unique ID counter
16
17// Static member to ensure initialiation on demand.
18static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
19
20// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000021typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattnerda8f0042001-08-27 05:19:10 +000022typedef map<unsigned, pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000023
24static FactMapType *TheFactMap = 0;
25static FactMapType &getFactMap() {
26 if (TheFactMap == 0)
27 TheFactMap = new FactMapType();
28 return *TheFactMap;
29}
30
31static void eraseFromFactMap(unsigned ID) {
32 assert(TheFactMap && "No entries found!");
33 TheFactMap->erase(ID);
34 if (TheFactMap->empty()) { // Delete when empty
35 delete TheFactMap;
36 TheFactMap = 0;
37 }
38}
Chris Lattner8dc89a32001-08-23 17:07:56 +000039
40
41AnnotationID AnnotationManager::getID(const string &Name) { // Name -> ID
42 IDMapType::iterator I = getIDMap().find(Name);
43 if (I == getIDMap().end()) {
44 getIDMap()[Name] = IDCounter++; // Add a new element
45 return IDCounter-1;
46 }
47 return I->second;
48}
49
Chris Lattner82072d42001-09-09 21:02:38 +000050// getID - Name -> ID + registration of a factory function for demand driven
51// annotation support.
52AnnotationID AnnotationManager::getID(const string &Name, Factory Fact,
Chris Lattner60bfeb82002-07-24 20:17:22 +000053 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000054 AnnotationID Result(getID(Name));
55 registerAnnotationFactory(Result, Fact, Data);
56 return Result;
57}
58
59
Chris Lattner8dc89a32001-08-23 17:07:56 +000060// getName - This function is especially slow, but that's okay because it should
61// only be used for debugging.
62//
63const string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
64 IDMapType &TheMap = getIDMap();
65 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
66 assert(I != TheMap.end() && "Annotation ID is unknown!");
67 if (I->second == ID.ID) return I->first;
68 }
69}
70
71
72// registerAnnotationFactory - This method is used to register a callback
73// function used to create an annotation on demand if it is needed by the
74// Annotable::findOrCreateAnnotation method.
75//
76void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
Chris Lattnerda8f0042001-08-27 05:19:10 +000077 AnnFactory F,
78 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000079 if (F)
Chris Lattnerda8f0042001-08-27 05:19:10 +000080 getFactMap()[ID.ID] = make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +000081 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +000082 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +000083}
84
85// createAnnotation - Create an annotation of the specified ID for the
86// specified object, using a register annotation creation function.
87//
88Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Chris Lattnerc0f483d2001-09-07 16:44:01 +000089 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000090 FactMapType::iterator I = getFactMap().find(ID.ID);
91 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +000092 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000093}