blob: b88624dd1f0d059de2651e00fa37b19818bed19d [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner8dc89a32001-08-23 17:07:56 +00009//
10// This file implements the AnnotationManager class.
11//
12//===----------------------------------------------------------------------===//
13
14#include <map>
Chris Lattnercb09cc22003-01-14 21:29:58 +000015#include "Support/Annotation.h"
Chris Lattner8dc89a32001-08-23 17:07:56 +000016
Brian Gaeked0fde302003-11-11 22:41:34 +000017namespace llvm {
18
Chris Lattner01e770a2003-05-22 21:59:35 +000019typedef std::map<const std::string, unsigned> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000020static unsigned IDCounter = 0; // Unique ID counter
21
22// Static member to ensure initialiation on demand.
23static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
24
25// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000026typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000027typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000028
29static FactMapType *TheFactMap = 0;
30static FactMapType &getFactMap() {
31 if (TheFactMap == 0)
32 TheFactMap = new FactMapType();
33 return *TheFactMap;
34}
35
36static void eraseFromFactMap(unsigned ID) {
37 assert(TheFactMap && "No entries found!");
38 TheFactMap->erase(ID);
39 if (TheFactMap->empty()) { // Delete when empty
40 delete TheFactMap;
41 TheFactMap = 0;
42 }
43}
Chris Lattner8dc89a32001-08-23 17:07:56 +000044
45
Chris Lattner01e770a2003-05-22 21:59:35 +000046AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID
Chris Lattner8dc89a32001-08-23 17:07:56 +000047 IDMapType::iterator I = getIDMap().find(Name);
48 if (I == getIDMap().end()) {
49 getIDMap()[Name] = IDCounter++; // Add a new element
50 return IDCounter-1;
51 }
52 return I->second;
53}
54
Chris Lattner82072d42001-09-09 21:02:38 +000055// getID - Name -> ID + registration of a factory function for demand driven
56// annotation support.
Chris Lattner01e770a2003-05-22 21:59:35 +000057AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
Chris Lattner60bfeb82002-07-24 20:17:22 +000058 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000059 AnnotationID Result(getID(Name));
60 registerAnnotationFactory(Result, Fact, Data);
61 return Result;
62}
63
64
Chris Lattner8dc89a32001-08-23 17:07:56 +000065// getName - This function is especially slow, but that's okay because it should
66// only be used for debugging.
67//
Chris Lattner01e770a2003-05-22 21:59:35 +000068const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Chris Lattner8dc89a32001-08-23 17:07:56 +000069 IDMapType &TheMap = getIDMap();
70 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
71 assert(I != TheMap.end() && "Annotation ID is unknown!");
72 if (I->second == ID.ID) return I->first;
73 }
74}
75
76
77// registerAnnotationFactory - This method is used to register a callback
78// function used to create an annotation on demand if it is needed by the
79// Annotable::findOrCreateAnnotation method.
80//
81void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
Chris Lattnerda8f0042001-08-27 05:19:10 +000082 AnnFactory F,
83 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000084 if (F)
Chris Lattner01e770a2003-05-22 21:59:35 +000085 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +000086 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +000087 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +000088}
89
90// createAnnotation - Create an annotation of the specified ID for the
91// specified object, using a register annotation creation function.
92//
93Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Chris Lattnerc0f483d2001-09-07 16:44:01 +000094 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000095 FactMapType::iterator I = getFactMap().find(ID.ID);
96 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +000097 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000098}
Brian Gaeked0fde302003-11-11 22:41:34 +000099
100} // End llvm namespace