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