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> |
| 8 | #include "llvm/Annotation.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 9 | using std::string; |
| 10 | using std::map; |
| 11 | using std::pair; |
| 12 | using std::make_pair; |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 13 | |
| 14 | typedef map<const string, unsigned> IDMapType; |
| 15 | static unsigned IDCounter = 0; // Unique ID counter |
| 16 | |
| 17 | // Static member to ensure initialiation on demand. |
| 18 | static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; } |
| 19 | |
| 20 | // On demand annotation creation support... |
Chris Lattner | c0f483d | 2001-09-07 16:44:01 +0000 | [diff] [blame] | 21 | typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *); |
Chris Lattner | da8f004 | 2001-08-27 05:19:10 +0000 | [diff] [blame] | 22 | typedef map<unsigned, pair<AnnFactory,void*> > FactMapType; |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 23 | static FactMapType &getFactMap() { static FactMapType FactMap; return FactMap; } |
| 24 | |
| 25 | |
| 26 | AnnotationID AnnotationManager::getID(const string &Name) { // Name -> ID |
| 27 | IDMapType::iterator I = getIDMap().find(Name); |
| 28 | if (I == getIDMap().end()) { |
| 29 | getIDMap()[Name] = IDCounter++; // Add a new element |
| 30 | return IDCounter-1; |
| 31 | } |
| 32 | return I->second; |
| 33 | } |
| 34 | |
Chris Lattner | 82072d4 | 2001-09-09 21:02:38 +0000 | [diff] [blame] | 35 | // getID - Name -> ID + registration of a factory function for demand driven |
| 36 | // annotation support. |
| 37 | AnnotationID AnnotationManager::getID(const string &Name, Factory Fact, |
Chris Lattner | 60bfeb8 | 2002-07-24 20:17:22 +0000 | [diff] [blame^] | 38 | void *Data) { |
Chris Lattner | 82072d4 | 2001-09-09 21:02:38 +0000 | [diff] [blame] | 39 | AnnotationID Result(getID(Name)); |
| 40 | registerAnnotationFactory(Result, Fact, Data); |
| 41 | return Result; |
| 42 | } |
| 43 | |
| 44 | |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 45 | // getName - This function is especially slow, but that's okay because it should |
| 46 | // only be used for debugging. |
| 47 | // |
| 48 | const string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name |
| 49 | IDMapType &TheMap = getIDMap(); |
| 50 | for (IDMapType::iterator I = TheMap.begin(); ; ++I) { |
| 51 | assert(I != TheMap.end() && "Annotation ID is unknown!"); |
| 52 | if (I->second == ID.ID) return I->first; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
| 57 | // registerAnnotationFactory - This method is used to register a callback |
| 58 | // function used to create an annotation on demand if it is needed by the |
| 59 | // Annotable::findOrCreateAnnotation method. |
| 60 | // |
| 61 | void AnnotationManager::registerAnnotationFactory(AnnotationID ID, |
Chris Lattner | da8f004 | 2001-08-27 05:19:10 +0000 | [diff] [blame] | 62 | AnnFactory F, |
| 63 | void *ExtraData) { |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 64 | if (F) |
Chris Lattner | da8f004 | 2001-08-27 05:19:10 +0000 | [diff] [blame] | 65 | getFactMap()[ID.ID] = make_pair(F, ExtraData); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 66 | else |
| 67 | getFactMap().erase(ID.ID); |
| 68 | } |
| 69 | |
| 70 | // createAnnotation - Create an annotation of the specified ID for the |
| 71 | // specified object, using a register annotation creation function. |
| 72 | // |
| 73 | Annotation *AnnotationManager::createAnnotation(AnnotationID ID, |
Chris Lattner | c0f483d | 2001-09-07 16:44:01 +0000 | [diff] [blame] | 74 | const Annotable *Obj) { |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 75 | FactMapType::iterator I = getFactMap().find(ID.ID); |
| 76 | if (I == getFactMap().end()) return 0; |
Chris Lattner | da8f004 | 2001-08-27 05:19:10 +0000 | [diff] [blame] | 77 | return I->second.first(ID, Obj, I->second.second); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 78 | } |