blob: 9166240b824a043ccbb33f2ba8801b9fdb20e140 [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 Lattner8dc89a32001-08-23 17:07:56 +00009
Chris Lattner01e770a2003-05-22 21:59:35 +000010typedef std::map<const std::string, unsigned> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000011static 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 Lattner01e770a2003-05-22 21:59:35 +000018typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000019
20static FactMapType *TheFactMap = 0;
21static FactMapType &getFactMap() {
22 if (TheFactMap == 0)
23 TheFactMap = new FactMapType();
24 return *TheFactMap;
25}
26
27static void eraseFromFactMap(unsigned ID) {
28 assert(TheFactMap && "No entries found!");
29 TheFactMap->erase(ID);
30 if (TheFactMap->empty()) { // Delete when empty
31 delete TheFactMap;
32 TheFactMap = 0;
33 }
34}
Chris Lattner8dc89a32001-08-23 17:07:56 +000035
36
Chris Lattner01e770a2003-05-22 21:59:35 +000037AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID
Chris Lattner8dc89a32001-08-23 17:07:56 +000038 IDMapType::iterator I = getIDMap().find(Name);
39 if (I == getIDMap().end()) {
40 getIDMap()[Name] = IDCounter++; // Add a new element
41 return IDCounter-1;
42 }
43 return I->second;
44}
45
Chris Lattner82072d42001-09-09 21:02:38 +000046// getID - Name -> ID + registration of a factory function for demand driven
47// annotation support.
Chris Lattner01e770a2003-05-22 21:59:35 +000048AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
Chris Lattner60bfeb82002-07-24 20:17:22 +000049 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000050 AnnotationID Result(getID(Name));
51 registerAnnotationFactory(Result, Fact, Data);
52 return Result;
53}
54
55
Chris Lattner8dc89a32001-08-23 17:07:56 +000056// getName - This function is especially slow, but that's okay because it should
57// only be used for debugging.
58//
Chris Lattner01e770a2003-05-22 21:59:35 +000059const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Chris Lattner8dc89a32001-08-23 17:07:56 +000060 IDMapType &TheMap = getIDMap();
61 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
62 assert(I != TheMap.end() && "Annotation ID is unknown!");
63 if (I->second == ID.ID) return I->first;
64 }
65}
66
67
68// registerAnnotationFactory - This method is used to register a callback
69// function used to create an annotation on demand if it is needed by the
70// Annotable::findOrCreateAnnotation method.
71//
72void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
Chris Lattnerda8f0042001-08-27 05:19:10 +000073 AnnFactory F,
74 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000075 if (F)
Chris Lattner01e770a2003-05-22 21:59:35 +000076 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +000077 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +000078 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +000079}
80
81// createAnnotation - Create an annotation of the specified ID for the
82// specified object, using a register annotation creation function.
83//
84Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Chris Lattnerc0f483d2001-09-07 16:44:01 +000085 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000086 FactMapType::iterator I = getFactMap().find(ID.ID);
87 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +000088 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000089}