Use LLVMContext to generate metadata constants.
[oota-llvm.git] / lib / Support / Annotation.cpp
1 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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 "llvm/System/RWMutex.h"
17 #include <map>
18 #include <cstring>
19 using namespace llvm;
20
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
32 namespace {
33   class StrCmp {
34   public:
35     bool operator()(const char *a, const char *b) const {
36       return strcmp(a, b) < 0;
37     }
38   };
39 }
40
41 typedef std::map<const char*, unsigned, StrCmp> IDMapType;
42 static volatile sys::cas_flag IDCounter = 0;  // Unique ID counter
43
44 // Static member to ensure initialiation on demand.
45 static ManagedStatic<IDMapType> IDMap;
46 static ManagedStatic<sys::SmartRWMutex<true> > AnnotationsLock;
47
48 // On demand annotation creation support...
49 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
50 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
51
52 static ManagedStatic<FactMapType> TheFactMap;
53 static FactMapType &getFactMap() {
54   return *TheFactMap;
55 }
56
57 static void eraseFromFactMap(unsigned ID) {
58   sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
59   TheFactMap->erase(ID);
60 }
61
62 AnnotationID AnnotationManager::getID(const char *Name) {  // Name -> ID
63   AnnotationsLock->reader_acquire();
64   IDMapType::iterator I = IDMap->find(Name);
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);
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);
77   }
78   return AnnotationID(I->second);
79 }
80
81 // getID - Name -> ID + registration of a factory function for demand driven
82 // annotation support.
83 AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
84                                       void *Data) {
85   AnnotationID Result(getID(Name));
86   registerAnnotationFactory(Result, Fact, Data);
87   return Result;
88 }
89
90 // getName - This function is especially slow, but that's okay because it should
91 // only be used for debugging.
92 //
93 const char *AnnotationManager::getName(AnnotationID ID) {  // ID -> Name
94   sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
95   IDMapType &TheMap = *IDMap;
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
102 // registerAnnotationFactory - This method is used to register a callback
103 // function used to create an annotation on demand if it is needed by the
104 // Annotable::findOrCreateAnnotation method.
105 //
106 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
107                                                   void *ExtraData) {
108   if (F) {
109     sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
110     getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
111   } else {
112     eraseFromFactMap(ID.ID);
113   }
114 }
115
116 // createAnnotation - Create an annotation of the specified ID for the
117 // specified object, using a register annotation creation function.
118 //
119 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
120                                                 const Annotable *Obj) {
121   AnnotationsLock->reader_acquire();
122   FactMapType::iterator I = getFactMap().find(ID.ID);
123   if (I == getFactMap().end()) {
124     AnnotationsLock->reader_release();
125     return 0;
126   }
127   
128   AnnotationsLock->reader_release();
129   return I->second.first(ID, Obj, I->second.second);
130 }