blob: 4b5b97e41f322daa54eedb4696a8c21c26810a0d [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AnnotationManager class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Annotation.h"
15#include "llvm/Support/ManagedStatic.h"
Owen Anderson096a14a2009-06-22 22:44:15 +000016#include "llvm/System/RWMutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include <map>
Nuno Lopesa1c76d92008-11-27 16:42:44 +000018#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019using namespace llvm;
20
21Annotation::~Annotation() {} // Designed to be subclassed
22
23Annotable::~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 Lopes1d7ae4b2008-11-27 16:37:02 +000032namespace {
33 class StrCmp {
34 public:
Nick Lewycky18b90af2008-12-08 00:45:02 +000035 bool operator()(const char *a, const char *b) const {
Nuno Lopes1d7ae4b2008-11-27 16:37:02 +000036 return strcmp(a, b) < 0;
37 }
38 };
39}
40
41typedef std::map<const char*, unsigned, StrCmp> IDMapType;
Owen Anderson35c161c2009-06-30 05:33:46 +000042static volatile sys::cas_flag IDCounter = 0; // Unique ID counter
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
44// Static member to ensure initialiation on demand.
45static ManagedStatic<IDMapType> IDMap;
Owen Anderson096a14a2009-06-22 22:44:15 +000046static ManagedStatic<sys::SmartRWMutex<true> > AnnotationsLock;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48// On demand annotation creation support...
49typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
50typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
51
Owen Anderson096a14a2009-06-22 22:44:15 +000052static ManagedStatic<FactMapType> TheFactMap;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053static FactMapType &getFactMap() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 return *TheFactMap;
55}
56
57static void eraseFromFactMap(unsigned ID) {
Owen Anderson096a14a2009-06-22 22:44:15 +000058 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 TheFactMap->erase(ID);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060}
61
Nuno Lopese53fc632008-11-26 00:00:44 +000062AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
Owen Anderson096a14a2009-06-22 22:44:15 +000063 AnnotationsLock->reader_acquire();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 IDMapType::iterator I = IDMap->find(Name);
Owen Anderson096a14a2009-06-22 22:44:15 +000065 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 Anderson3de3e9b2009-06-26 18:09:03 +000071 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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 }
Dan Gohmana789bff2008-02-20 16:44:09 +000078 return AnnotationID(I->second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079}
80
81// getID - Name -> ID + registration of a factory function for demand driven
82// annotation support.
Nuno Lopese53fc632008-11-26 00:00:44 +000083AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 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//
Nuno Lopese53fc632008-11-26 00:00:44 +000093const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Owen Anderson096a14a2009-06-22 22:44:15 +000094 sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 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//
106void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
107 void *ExtraData) {
Owen Anderson096a14a2009-06-22 22:44:15 +0000108 if (F) {
109 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Owen Anderson096a14a2009-06-22 22:44:15 +0000111 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 eraseFromFactMap(ID.ID);
Owen Anderson096a14a2009-06-22 22:44:15 +0000113 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114}
115
116// createAnnotation - Create an annotation of the specified ID for the
117// specified object, using a register annotation creation function.
118//
119Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
120 const Annotable *Obj) {
Owen Anderson096a14a2009-06-22 22:44:15 +0000121 AnnotationsLock->reader_acquire();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 FactMapType::iterator I = getFactMap().find(ID.ID);
Owen Anderson096a14a2009-06-22 22:44:15 +0000123 if (I == getFactMap().end()) {
124 AnnotationsLock->reader_release();
125 return 0;
126 }
127
128 AnnotationsLock->reader_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 return I->second.first(ID, Obj, I->second.second);
130}