Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- Annotation.cpp - Implement the Annotation Classes -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the AnnotationManager class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/Annotation.h" |
| 15 | #include "llvm/Support/ManagedStatic.h" |
| 16 | #include <map> |
| 17 | using namespace llvm; |
| 18 | |
| 19 | Annotation::~Annotation() {} // Designed to be subclassed |
| 20 | |
| 21 | Annotable::~Annotable() { // Virtual because it's designed to be subclassed... |
| 22 | Annotation *A = AnnotationList; |
| 23 | while (A) { |
| 24 | Annotation *Next = A->getNext(); |
| 25 | delete A; |
| 26 | A = Next; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | typedef std::map<const std::string, unsigned> IDMapType; |
| 31 | static unsigned IDCounter = 0; // Unique ID counter |
| 32 | |
| 33 | // Static member to ensure initialiation on demand. |
| 34 | static ManagedStatic<IDMapType> IDMap; |
| 35 | |
| 36 | // On demand annotation creation support... |
| 37 | typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *); |
| 38 | typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType; |
| 39 | |
| 40 | static FactMapType *TheFactMap = 0; |
| 41 | static FactMapType &getFactMap() { |
| 42 | if (TheFactMap == 0) |
| 43 | TheFactMap = new FactMapType(); |
| 44 | return *TheFactMap; |
| 45 | } |
| 46 | |
| 47 | static void eraseFromFactMap(unsigned ID) { |
| 48 | assert(TheFactMap && "No entries found!"); |
| 49 | TheFactMap->erase(ID); |
| 50 | if (TheFactMap->empty()) { // Delete when empty |
| 51 | delete TheFactMap; |
| 52 | TheFactMap = 0; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID |
| 57 | IDMapType::iterator I = IDMap->find(Name); |
| 58 | if (I == IDMap->end()) { |
| 59 | (*IDMap)[Name] = IDCounter++; // Add a new element |
| 60 | return IDCounter-1; |
| 61 | } |
| 62 | return I->second; |
| 63 | } |
| 64 | |
| 65 | // getID - Name -> ID + registration of a factory function for demand driven |
| 66 | // annotation support. |
| 67 | AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact, |
| 68 | void *Data) { |
| 69 | AnnotationID Result(getID(Name)); |
| 70 | registerAnnotationFactory(Result, Fact, Data); |
| 71 | return Result; |
| 72 | } |
| 73 | |
| 74 | // getName - This function is especially slow, but that's okay because it should |
| 75 | // only be used for debugging. |
| 76 | // |
| 77 | const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name |
| 78 | IDMapType &TheMap = *IDMap; |
| 79 | for (IDMapType::iterator I = TheMap.begin(); ; ++I) { |
| 80 | assert(I != TheMap.end() && "Annotation ID is unknown!"); |
| 81 | if (I->second == ID.ID) return I->first; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // registerAnnotationFactory - This method is used to register a callback |
| 86 | // function used to create an annotation on demand if it is needed by the |
| 87 | // Annotable::findOrCreateAnnotation method. |
| 88 | // |
| 89 | void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F, |
| 90 | void *ExtraData) { |
| 91 | if (F) |
| 92 | getFactMap()[ID.ID] = std::make_pair(F, ExtraData); |
| 93 | else |
| 94 | eraseFromFactMap(ID.ID); |
| 95 | } |
| 96 | |
| 97 | // createAnnotation - Create an annotation of the specified ID for the |
| 98 | // specified object, using a register annotation creation function. |
| 99 | // |
| 100 | Annotation *AnnotationManager::createAnnotation(AnnotationID ID, |
| 101 | const Annotable *Obj) { |
| 102 | FactMapType::iterator I = getFactMap().find(ID.ID); |
| 103 | if (I == getFactMap().end()) return 0; |
| 104 | return I->second.first(ID, Obj, I->second.second); |
| 105 | } |