blob: 0e208e08ba02d05a8a42f81676c69ed447340b75 [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"
16#include <map>
17using namespace llvm;
18
19Annotation::~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 Lopes1d7ae4b2008-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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040static unsigned IDCounter = 0; // Unique ID counter
41
42// Static member to ensure initialiation on demand.
43static ManagedStatic<IDMapType> IDMap;
44
45// On demand annotation creation support...
46typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
47typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
48
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}
64
Nuno Lopese53fc632008-11-26 00:00:44 +000065AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 IDMapType::iterator I = IDMap->find(Name);
67 if (I == IDMap->end()) {
68 (*IDMap)[Name] = IDCounter++; // Add a new element
Dan Gohmana789bff2008-02-20 16:44:09 +000069 return AnnotationID(IDCounter-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 }
Dan Gohmana789bff2008-02-20 16:44:09 +000071 return AnnotationID(I->second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072}
73
74// getID - Name -> ID + registration of a factory function for demand driven
75// annotation support.
Nuno Lopese53fc632008-11-26 00:00:44 +000076AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 void *Data) {
78 AnnotationID Result(getID(Name));
79 registerAnnotationFactory(Result, Fact, Data);
80 return Result;
81}
82
83// getName - This function is especially slow, but that's okay because it should
84// only be used for debugging.
85//
Nuno Lopese53fc632008-11-26 00:00:44 +000086const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 IDMapType &TheMap = *IDMap;
88 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
94// registerAnnotationFactory - This method is used to register a callback
95// function used to create an annotation on demand if it is needed by the
96// Annotable::findOrCreateAnnotation method.
97//
98void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
99 void *ExtraData) {
100 if (F)
101 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
102 else
103 eraseFromFactMap(ID.ID);
104}
105
106// createAnnotation - Create an annotation of the specified ID for the
107// specified object, using a register annotation creation function.
108//
109Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
110 const Annotable *Obj) {
111 FactMapType::iterator I = getFactMap().find(ID.ID);
112 if (I == getFactMap().end()) return 0;
113 return I->second.first(ID, Obj, I->second.second);
114}