blob: 0e208e08ba02d05a8a42f81676c69ed447340b75 [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"
Misha Brukman710d1ce2004-11-09 04:27:19 +000016#include <map>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000017using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000018
Chris Lattner1cd4c722004-02-26 07:24:18 +000019Annotation::~Annotation() {} // Designed to be subclassed
20
21Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
22 Annotation *A = AnnotationList;
23 while (A) {
24 Annotation *Next = A->getNext();
25 delete A;
26 A = Next;
27 }
28}
29
Nuno Lopes4ba9aac2008-11-27 16:37:02 +000030namespace {
31 class StrCmp {
32 public:
33 bool operator()(const char *a, const char *b) {
34 return strcmp(a, b) < 0;
35 }
36 };
37}
38
39typedef std::map<const char*, unsigned, StrCmp> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000040static unsigned IDCounter = 0; // Unique ID counter
41
42// Static member to ensure initialiation on demand.
Chris Lattnerc055a912006-10-04 22:13:11 +000043static ManagedStatic<IDMapType> IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000044
45// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000046typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000047typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000048
49static FactMapType *TheFactMap = 0;
50static FactMapType &getFactMap() {
51 if (TheFactMap == 0)
52 TheFactMap = new FactMapType();
53 return *TheFactMap;
54}
55
56static void eraseFromFactMap(unsigned ID) {
57 assert(TheFactMap && "No entries found!");
58 TheFactMap->erase(ID);
59 if (TheFactMap->empty()) { // Delete when empty
60 delete TheFactMap;
61 TheFactMap = 0;
62 }
63}
Chris Lattner8dc89a32001-08-23 17:07:56 +000064
Nuno Lopes08d67c72008-11-26 00:00:44 +000065AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
Chris Lattnerc055a912006-10-04 22:13:11 +000066 IDMapType::iterator I = IDMap->find(Name);
67 if (I == IDMap->end()) {
68 (*IDMap)[Name] = IDCounter++; // Add a new element
Dan Gohmanb5660dc2008-02-20 16:44:09 +000069 return AnnotationID(IDCounter-1);
Chris Lattner8dc89a32001-08-23 17:07:56 +000070 }
Dan Gohmanb5660dc2008-02-20 16:44:09 +000071 return AnnotationID(I->second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000072}
73
Chris Lattner82072d42001-09-09 21:02:38 +000074// getID - Name -> ID + registration of a factory function for demand driven
75// annotation support.
Nuno Lopes08d67c72008-11-26 00:00:44 +000076AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
Misha Brukman710d1ce2004-11-09 04:27:19 +000077 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000078 AnnotationID Result(getID(Name));
79 registerAnnotationFactory(Result, Fact, Data);
Misha Brukmanf976c852005-04-21 22:55:34 +000080 return Result;
Chris Lattner82072d42001-09-09 21:02:38 +000081}
82
Chris Lattner8dc89a32001-08-23 17:07:56 +000083// getName - This function is especially slow, but that's okay because it should
84// only be used for debugging.
85//
Nuno Lopes08d67c72008-11-26 00:00:44 +000086const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Chris Lattnerc055a912006-10-04 22:13:11 +000087 IDMapType &TheMap = *IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000088 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
89 assert(I != TheMap.end() && "Annotation ID is unknown!");
90 if (I->second == ID.ID) return I->first;
91 }
92}
93
Chris Lattner8dc89a32001-08-23 17:07:56 +000094// registerAnnotationFactory - This method is used to register a callback
Misha Brukmanf976c852005-04-21 22:55:34 +000095// function used to create an annotation on demand if it is needed by the
Chris Lattner8dc89a32001-08-23 17:07:56 +000096// Annotable::findOrCreateAnnotation method.
97//
Misha Brukman710d1ce2004-11-09 04:27:19 +000098void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
99 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +0000100 if (F)
Chris Lattner01e770a2003-05-22 21:59:35 +0000101 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000102 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +0000103 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000104}
105
106// createAnnotation - Create an annotation of the specified ID for the
107// specified object, using a register annotation creation function.
108//
Misha Brukmanf976c852005-04-21 22:55:34 +0000109Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Misha Brukman710d1ce2004-11-09 04:27:19 +0000110 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +0000111 FactMapType::iterator I = getFactMap().find(ID.ID);
112 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +0000113 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000114}