Chris Lattner | cf3056d | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Annotation.cpp - Implement the Annotation Classes -----------------===// |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the AnnotationManager class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Annotation.h" |
Chris Lattner | c055a91 | 2006-10-04 22:13:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ManagedStatic.h" |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 16 | #include "llvm/System/RWMutex.h" |
Misha Brukman | 710d1ce | 2004-11-09 04:27:19 +0000 | [diff] [blame] | 17 | #include <map> |
Nuno Lopes | 440954c | 2008-11-27 16:42:44 +0000 | [diff] [blame] | 18 | #include <cstring> |
Chris Lattner | 2cdd21c | 2003-12-14 21:35:53 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | |
Chris Lattner | 1cd4c72 | 2004-02-26 07:24:18 +0000 | [diff] [blame] | 21 | Annotation::~Annotation() {} // Designed to be subclassed |
| 22 | |
| 23 | Annotable::~Annotable() { // Virtual because it's designed to be subclassed... |
| 24 | Annotation *A = AnnotationList; |
| 25 | while (A) { |
| 26 | Annotation *Next = A->getNext(); |
| 27 | delete A; |
| 28 | A = Next; |
| 29 | } |
| 30 | } |
| 31 | |
Nuno Lopes | 4ba9aac | 2008-11-27 16:37:02 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | class StrCmp { |
| 34 | public: |
Nick Lewycky | 28ea4f6 | 2008-12-08 00:45:02 +0000 | [diff] [blame] | 35 | bool operator()(const char *a, const char *b) const { |
Nuno Lopes | 4ba9aac | 2008-11-27 16:37:02 +0000 | [diff] [blame] | 36 | return strcmp(a, b) < 0; |
| 37 | } |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | typedef std::map<const char*, unsigned, StrCmp> IDMapType; |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 42 | static unsigned IDCounter = 0; // Unique ID counter |
| 43 | |
| 44 | // Static member to ensure initialiation on demand. |
Chris Lattner | c055a91 | 2006-10-04 22:13:11 +0000 | [diff] [blame] | 45 | static ManagedStatic<IDMapType> IDMap; |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 46 | static ManagedStatic<sys::SmartRWMutex<true> > AnnotationsLock; |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 47 | |
| 48 | // On demand annotation creation support... |
Chris Lattner | c0f483d | 2001-09-07 16:44:01 +0000 | [diff] [blame] | 49 | typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *); |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 50 | typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType; |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 51 | |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 52 | static ManagedStatic<FactMapType> TheFactMap; |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 53 | static FactMapType &getFactMap() { |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 54 | return *TheFactMap; |
| 55 | } |
| 56 | |
| 57 | static void eraseFromFactMap(unsigned ID) { |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 58 | sys::SmartScopedWriter<true> Writer(&*AnnotationsLock); |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 59 | TheFactMap->erase(ID); |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 61 | |
Nuno Lopes | 08d67c7 | 2008-11-26 00:00:44 +0000 | [diff] [blame] | 62 | AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 63 | AnnotationsLock->reader_acquire(); |
Chris Lattner | c055a91 | 2006-10-04 22:13:11 +0000 | [diff] [blame] | 64 | IDMapType::iterator I = IDMap->find(Name); |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 65 | IDMapType::iterator E = IDMap->end(); |
| 66 | AnnotationsLock->reader_release(); |
| 67 | |
| 68 | if (I == E) { |
| 69 | sys::SmartScopedWriter<true> Writer(&*AnnotationsLock); |
| 70 | I = IDMap->find(Name); |
Owen Anderson | cb73734 | 2009-06-26 18:09:03 +0000 | [diff] [blame] | 71 | if (I == IDMap->end()) { |
| 72 | unsigned newCount = sys::AtomicIncrement(&IDCounter); |
| 73 | (*IDMap)[Name] = newCount-1; // Add a new element |
| 74 | return AnnotationID(newCount-1); |
| 75 | } else |
| 76 | return AnnotationID(I->second); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 77 | } |
Dan Gohman | b5660dc | 2008-02-20 16:44:09 +0000 | [diff] [blame] | 78 | return AnnotationID(I->second); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | 82072d4 | 2001-09-09 21:02:38 +0000 | [diff] [blame] | 81 | // getID - Name -> ID + registration of a factory function for demand driven |
| 82 | // annotation support. |
Nuno Lopes | 08d67c7 | 2008-11-26 00:00:44 +0000 | [diff] [blame] | 83 | AnnotationID AnnotationManager::getID(const char *Name, Factory Fact, |
Misha Brukman | 710d1ce | 2004-11-09 04:27:19 +0000 | [diff] [blame] | 84 | void *Data) { |
Chris Lattner | 82072d4 | 2001-09-09 21:02:38 +0000 | [diff] [blame] | 85 | AnnotationID Result(getID(Name)); |
| 86 | registerAnnotationFactory(Result, Fact, Data); |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 87 | return Result; |
Chris Lattner | 82072d4 | 2001-09-09 21:02:38 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 90 | // getName - This function is especially slow, but that's okay because it should |
| 91 | // only be used for debugging. |
| 92 | // |
Nuno Lopes | 08d67c7 | 2008-11-26 00:00:44 +0000 | [diff] [blame] | 93 | const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 94 | sys::SmartScopedReader<true> Reader(&*AnnotationsLock); |
Chris Lattner | c055a91 | 2006-10-04 22:13:11 +0000 | [diff] [blame] | 95 | IDMapType &TheMap = *IDMap; |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 96 | for (IDMapType::iterator I = TheMap.begin(); ; ++I) { |
| 97 | assert(I != TheMap.end() && "Annotation ID is unknown!"); |
| 98 | if (I->second == ID.ID) return I->first; |
| 99 | } |
| 100 | } |
| 101 | |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 102 | // registerAnnotationFactory - This method is used to register a callback |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 103 | // function used to create an annotation on demand if it is needed by the |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 104 | // Annotable::findOrCreateAnnotation method. |
| 105 | // |
Misha Brukman | 710d1ce | 2004-11-09 04:27:19 +0000 | [diff] [blame] | 106 | void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F, |
| 107 | void *ExtraData) { |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 108 | if (F) { |
| 109 | sys::SmartScopedWriter<true> Writer(&*AnnotationsLock); |
Chris Lattner | 01e770a | 2003-05-22 21:59:35 +0000 | [diff] [blame] | 110 | getFactMap()[ID.ID] = std::make_pair(F, ExtraData); |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 111 | } else { |
Chris Lattner | 3fa61eb | 2003-01-13 00:52:43 +0000 | [diff] [blame] | 112 | eraseFromFactMap(ID.ID); |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // createAnnotation - Create an annotation of the specified ID for the |
| 117 | // specified object, using a register annotation creation function. |
| 118 | // |
Misha Brukman | f976c85 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 119 | Annotation *AnnotationManager::createAnnotation(AnnotationID ID, |
Misha Brukman | 710d1ce | 2004-11-09 04:27:19 +0000 | [diff] [blame] | 120 | const Annotable *Obj) { |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 121 | AnnotationsLock->reader_acquire(); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 122 | FactMapType::iterator I = getFactMap().find(ID.ID); |
Owen Anderson | 521db56 | 2009-06-22 22:44:15 +0000 | [diff] [blame] | 123 | if (I == getFactMap().end()) { |
| 124 | AnnotationsLock->reader_release(); |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | AnnotationsLock->reader_release(); |
Chris Lattner | da8f004 | 2001-08-27 05:19:10 +0000 | [diff] [blame] | 129 | return I->second.first(ID, Obj, I->second.second); |
Chris Lattner | 8dc89a3 | 2001-08-23 17:07:56 +0000 | [diff] [blame] | 130 | } |