1 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the AnnotationManager class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Annotation.h"
18 Annotation::~Annotation() {} // Designed to be subclassed
20 Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
21 Annotation
*A
= AnnotationList
;
23 Annotation
*Next
= A
->getNext();
29 typedef std::map
<const std::string
, unsigned> IDMapType
;
30 static unsigned IDCounter
= 0; // Unique ID counter
32 // Static member to ensure initialiation on demand.
33 static IDMapType
&getIDMap() { static IDMapType TheMap
; return TheMap
; }
35 // On demand annotation creation support...
36 typedef Annotation
*(*AnnFactory
)(AnnotationID
, const Annotable
*, void *);
37 typedef std::map
<unsigned, std::pair
<AnnFactory
,void*> > FactMapType
;
39 static FactMapType
*TheFactMap
= 0;
40 static FactMapType
&getFactMap() {
42 TheFactMap
= new FactMapType();
46 static void eraseFromFactMap(unsigned ID
) {
47 assert(TheFactMap
&& "No entries found!");
48 TheFactMap
->erase(ID
);
49 if (TheFactMap
->empty()) { // Delete when empty
55 AnnotationID
AnnotationManager::getID(const std::string
&Name
) { // Name -> ID
56 IDMapType::iterator I
= getIDMap().find(Name
);
57 if (I
== getIDMap().end()) {
58 getIDMap()[Name
] = IDCounter
++; // Add a new element
64 // getID - Name -> ID + registration of a factory function for demand driven
65 // annotation support.
66 AnnotationID
AnnotationManager::getID(const std::string
&Name
, Factory Fact
,
68 AnnotationID
Result(getID(Name
));
69 registerAnnotationFactory(Result
, Fact
, Data
);
73 // getName - This function is especially slow, but that's okay because it should
74 // only be used for debugging.
76 const std::string
&AnnotationManager::getName(AnnotationID ID
) { // ID -> Name
77 IDMapType
&TheMap
= getIDMap();
78 for (IDMapType::iterator I
= TheMap
.begin(); ; ++I
) {
79 assert(I
!= TheMap
.end() && "Annotation ID is unknown!");
80 if (I
->second
== ID
.ID
) return I
->first
;
84 // registerAnnotationFactory - This method is used to register a callback
85 // function used to create an annotation on demand if it is needed by the
86 // Annotable::findOrCreateAnnotation method.
88 void AnnotationManager::registerAnnotationFactory(AnnotationID ID
, AnnFactory F
,
91 getFactMap()[ID
.ID
] = std::make_pair(F
, ExtraData
);
93 eraseFromFactMap(ID
.ID
);
96 // createAnnotation - Create an annotation of the specified ID for the
97 // specified object, using a register annotation creation function.
99 Annotation
*AnnotationManager::createAnnotation(AnnotationID ID
,
100 const Annotable
*Obj
) {
101 FactMapType::iterator I
= getFactMap().find(ID
.ID
);
102 if (I
== getFactMap().end()) return 0;
103 return I
->second
.first(ID
, Obj
, I
->second
.second
);