blob: 9c3efa37d8672a77be7651614a3b675b2bf2a5f1 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner8dc89a32001-08-23 17:07:56 +00009//
10// This file implements the AnnotationManager class.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/Annotation.h"
Chris Lattnerc055a912006-10-04 22:13:11 +000015#include "llvm/Support/ManagedStatic.h"
Owen Anderson521db562009-06-22 22:44:15 +000016#include "llvm/System/RWMutex.h"
Misha Brukman710d1ce2004-11-09 04:27:19 +000017#include <map>
Nuno Lopes440954c2008-11-27 16:42:44 +000018#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner1cd4c722004-02-26 07:24:18 +000021Annotation::~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 Lopes4ba9aac2008-11-27 16:37:02 +000032namespace {
33 class StrCmp {
34 public:
Nick Lewycky28ea4f62008-12-08 00:45:02 +000035 bool operator()(const char *a, const char *b) const {
Nuno Lopes4ba9aac2008-11-27 16:37:02 +000036 return strcmp(a, b) < 0;
37 }
38 };
39}
40
41typedef std::map<const char*, unsigned, StrCmp> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000042static unsigned IDCounter = 0; // Unique ID counter
43
44// Static member to ensure initialiation on demand.
Chris Lattnerc055a912006-10-04 22:13:11 +000045static ManagedStatic<IDMapType> IDMap;
Owen Anderson521db562009-06-22 22:44:15 +000046static ManagedStatic<sys::SmartRWMutex<true> > AnnotationsLock;
Chris Lattner8dc89a32001-08-23 17:07:56 +000047
48// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000049typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000050typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000051
Owen Anderson521db562009-06-22 22:44:15 +000052static ManagedStatic<FactMapType> TheFactMap;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000053static FactMapType &getFactMap() {
Chris Lattner3fa61eb2003-01-13 00:52:43 +000054 return *TheFactMap;
55}
56
57static void eraseFromFactMap(unsigned ID) {
Owen Anderson521db562009-06-22 22:44:15 +000058 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
Chris Lattner3fa61eb2003-01-13 00:52:43 +000059 TheFactMap->erase(ID);
Chris Lattner3fa61eb2003-01-13 00:52:43 +000060}
Chris Lattner8dc89a32001-08-23 17:07:56 +000061
Nuno Lopes08d67c72008-11-26 00:00:44 +000062AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
Owen Anderson521db562009-06-22 22:44:15 +000063 AnnotationsLock->reader_acquire();
Chris Lattnerc055a912006-10-04 22:13:11 +000064 IDMapType::iterator I = IDMap->find(Name);
Owen Anderson521db562009-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 Gohmanb5660dc2008-02-20 16:44:09 +000073 return AnnotationID(IDCounter-1);
Chris Lattner8dc89a32001-08-23 17:07:56 +000074 }
Dan Gohmanb5660dc2008-02-20 16:44:09 +000075 return AnnotationID(I->second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000076}
77
Chris Lattner82072d42001-09-09 21:02:38 +000078// getID - Name -> ID + registration of a factory function for demand driven
79// annotation support.
Nuno Lopes08d67c72008-11-26 00:00:44 +000080AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
Misha Brukman710d1ce2004-11-09 04:27:19 +000081 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000082 AnnotationID Result(getID(Name));
83 registerAnnotationFactory(Result, Fact, Data);
Misha Brukmanf976c852005-04-21 22:55:34 +000084 return Result;
Chris Lattner82072d42001-09-09 21:02:38 +000085}
86
Chris Lattner8dc89a32001-08-23 17:07:56 +000087// getName - This function is especially slow, but that's okay because it should
88// only be used for debugging.
89//
Nuno Lopes08d67c72008-11-26 00:00:44 +000090const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Owen Anderson521db562009-06-22 22:44:15 +000091 sys::SmartScopedReader<true> Reader(&*AnnotationsLock);
Chris Lattnerc055a912006-10-04 22:13:11 +000092 IDMapType &TheMap = *IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000093 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
Chris Lattner8dc89a32001-08-23 17:07:56 +000099// registerAnnotationFactory - This method is used to register a callback
Misha Brukmanf976c852005-04-21 22:55:34 +0000100// function used to create an annotation on demand if it is needed by the
Chris Lattner8dc89a32001-08-23 17:07:56 +0000101// Annotable::findOrCreateAnnotation method.
102//
Misha Brukman710d1ce2004-11-09 04:27:19 +0000103void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
104 void *ExtraData) {
Owen Anderson521db562009-06-22 22:44:15 +0000105 if (F) {
106 sys::SmartScopedWriter<true> Writer(&*AnnotationsLock);
Chris Lattner01e770a2003-05-22 21:59:35 +0000107 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Owen Anderson521db562009-06-22 22:44:15 +0000108 } else {
Chris Lattner3fa61eb2003-01-13 00:52:43 +0000109 eraseFromFactMap(ID.ID);
Owen Anderson521db562009-06-22 22:44:15 +0000110 }
Chris Lattner8dc89a32001-08-23 17:07:56 +0000111}
112
113// createAnnotation - Create an annotation of the specified ID for the
114// specified object, using a register annotation creation function.
115//
Misha Brukmanf976c852005-04-21 22:55:34 +0000116Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Misha Brukman710d1ce2004-11-09 04:27:19 +0000117 const Annotable *Obj) {
Owen Anderson521db562009-06-22 22:44:15 +0000118 AnnotationsLock->reader_acquire();
Chris Lattner8dc89a32001-08-23 17:07:56 +0000119 FactMapType::iterator I = getFactMap().find(ID.ID);
Owen Anderson521db562009-06-22 22:44:15 +0000120 if (I == getFactMap().end()) {
121 AnnotationsLock->reader_release();
122 return 0;
123 }
124
125 AnnotationsLock->reader_release();
Chris Lattnerda8f0042001-08-27 05:19:10 +0000126 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000127}