Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 1 | //===-- Annotation.cpp - Implement the Annotation Classes --------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the AnnotationManager class. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include <map> |
Chris Lattner | cb09cc2 | 2003-01-14 21:29:58 +0000 | [diff] [blame] | 8 | #include "Support/Annotation.h" |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 9 | |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 10 | typedef std::map<const std::string, unsigned> IDMapType; |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 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... |
Chris Lattner | c0f483d | 2001-09-07 16:44:01 +0000 | [diff] [blame] | 17 | typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *); |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 18 | typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType; |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 19 | |
| 20 | static FactMapType *TheFactMap = 0; |
| 21 | static FactMapType &getFactMap() { |
| 22 | if (TheFactMap == 0) |
| 23 | TheFactMap = new FactMapType(); |
| 24 | return *TheFactMap; |
| 25 | } |
| 26 | |
| 27 | static 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 Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 35 | |
| 36 | |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 37 | AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 38 | 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 Lattner | 82072d4 | 2001-09-09 21:02:38 +0000 | [diff] [blame] | 46 | // getID - Name -> ID + registration of a factory function for demand driven |
| 47 | // annotation support. |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 48 | AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact, |
Chris Lattner | 60bfeb8 | 2002-07-24 20:17:22 +0000 | [diff] [blame] | 49 | void *Data) { |
Chris Lattner | 82072d4 | 2001-09-09 21:02:38 +0000 | [diff] [blame] | 50 | AnnotationID Result(getID(Name)); |
| 51 | registerAnnotationFactory(Result, Fact, Data); |
| 52 | return Result; |
| 53 | } |
| 54 | |
| 55 | |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 56 | // getName - This function is especially slow, but that's okay because it should |
| 57 | // only be used for debugging. |
| 58 | // |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 59 | const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 60 | 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 | // |
| 72 | void AnnotationManager::registerAnnotationFactory(AnnotationID ID, |
Chris Lattner | da8f004 | 2001-08-27 05:19:10 +0000 | [diff] [blame] | 73 | AnnFactory F, |
| 74 | void *ExtraData) { |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 75 | if (F) |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 76 | getFactMap()[ID.ID] = std::make_pair(F, ExtraData); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 77 | else |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 78 | eraseFromFactMap(ID.ID); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // createAnnotation - Create an annotation of the specified ID for the |
| 82 | // specified object, using a register annotation creation function. |
| 83 | // |
| 84 | Annotation *AnnotationManager::createAnnotation(AnnotationID ID, |
Chris Lattner | c0f483d | 2001-09-07 16:44:01 +0000 | [diff] [blame] | 85 | const Annotable *Obj) { |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 86 | FactMapType::iterator I = getFactMap().find(ID.ID); |
| 87 | if (I == getFactMap().end()) return 0; |
Chris Lattner | da8f004 | 2001-08-27 05:19:10 +0000 | [diff] [blame] | 88 | return I->second.first(ID, Obj, I->second.second); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 89 | } |