blob: 9c3efa37d8672a77be7651614a3b675b2bf2a5f1 [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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042static unsigned IDCounter = 0; // Unique ID counter
43
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);
71 if (I == IDMap->end())
72 (*IDMap)[Name] = IDCounter++; // Add a new element
Dan Gohmana789bff2008-02-20 16:44:09 +000073 return AnnotationID(IDCounter-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 }
Dan Gohmana789bff2008-02-20 16:44:09 +000075 return AnnotationID(I->second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076}
77
78// getID - Name -> ID + registration of a factory function for demand driven
79// annotation support.
Nuno Lopese53fc632008-11-26 00:00:44 +000080AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 void *Data) {
82 AnnotationID Result(getID(Name));
83 registerAnnotationFactory(Result, Fact, Data);
84 return Result;
85}
86
87// getName - This function is especially slow, but that's okay because it should
88// only be used for debugging.
89//
Nuno Lopese53fc632008-11-26 00:00:44 +000090const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Owen Anderson096a14a2009-06-22 22:44:15 +000091 sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 IDMapType &TheMap = *IDMap;
93 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
94 assert(I != TheMap.end() && "Annotation ID is unknown!");
95 if (I->second == ID.ID) return I->first;
96 }
97}
98
99// registerAnnotationFactory - This method is used to register a callback
100// function used to create an annotation on demand if it is needed by the
101// Annotable::findOrCreateAnnotation method.
102//
103void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
104 void *ExtraData) {
Owen Anderson096a14a2009-06-22 22:44:15 +0000105 if (F) {
106 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Owen Anderson096a14a2009-06-22 22:44:15 +0000108 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 eraseFromFactMap(ID.ID);
Owen Anderson096a14a2009-06-22 22:44:15 +0000110 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111}
112
113// createAnnotation - Create an annotation of the specified ID for the
114// specified object, using a register annotation creation function.
115//
116Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
117 const Annotable *Obj) {
Owen Anderson096a14a2009-06-22 22:44:15 +0000118 AnnotationsLock->reader_acquire();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 FactMapType::iterator I = getFactMap().find(ID.ID);
Owen Anderson096a14a2009-06-22 22:44:15 +0000120 if (I == getFactMap().end()) {
121 AnnotationsLock->reader_release();
122 return 0;
123 }
124
125 AnnotationsLock->reader_release();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 return I->second.first(ID, Obj, I->second.second);
127}