blob: bcd196da44881ab1fe5c73979866747ce6e17a58 [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 Lattner2cdd21c2003-12-14 21:35:53 +000016using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000017
Chris Lattner01e770a2003-05-22 21:59:35 +000018typedef std::map<const std::string, unsigned> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000019static unsigned IDCounter = 0; // Unique ID counter
20
21// Static member to ensure initialiation on demand.
22static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
23
24// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000025typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000026typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000027
28static FactMapType *TheFactMap = 0;
29static FactMapType &getFactMap() {
30 if (TheFactMap == 0)
31 TheFactMap = new FactMapType();
32 return *TheFactMap;
33}
34
35static void eraseFromFactMap(unsigned ID) {
36 assert(TheFactMap && "No entries found!");
37 TheFactMap->erase(ID);
38 if (TheFactMap->empty()) { // Delete when empty
39 delete TheFactMap;
40 TheFactMap = 0;
41 }
42}
Chris Lattner8dc89a32001-08-23 17:07:56 +000043
44
Chris Lattner01e770a2003-05-22 21:59:35 +000045AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID
Chris Lattner8dc89a32001-08-23 17:07:56 +000046 IDMapType::iterator I = getIDMap().find(Name);
47 if (I == getIDMap().end()) {
48 getIDMap()[Name] = IDCounter++; // Add a new element
49 return IDCounter-1;
50 }
51 return I->second;
52}
53
Chris Lattner82072d42001-09-09 21:02:38 +000054// getID - Name -> ID + registration of a factory function for demand driven
55// annotation support.
Chris Lattner01e770a2003-05-22 21:59:35 +000056AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
Chris Lattner60bfeb82002-07-24 20:17:22 +000057 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000058 AnnotationID Result(getID(Name));
59 registerAnnotationFactory(Result, Fact, Data);
60 return Result;
61}
62
63
Chris Lattner8dc89a32001-08-23 17:07:56 +000064// getName - This function is especially slow, but that's okay because it should
65// only be used for debugging.
66//
Chris Lattner01e770a2003-05-22 21:59:35 +000067const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Chris Lattner8dc89a32001-08-23 17:07:56 +000068 IDMapType &TheMap = getIDMap();
69 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
70 assert(I != TheMap.end() && "Annotation ID is unknown!");
71 if (I->second == ID.ID) return I->first;
72 }
73}
74
75
76// registerAnnotationFactory - This method is used to register a callback
77// function used to create an annotation on demand if it is needed by the
78// Annotable::findOrCreateAnnotation method.
79//
80void AnnotationManager::registerAnnotationFactory(AnnotationID ID,
Chris Lattnerda8f0042001-08-27 05:19:10 +000081 AnnFactory F,
82 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000083 if (F)
Chris Lattner01e770a2003-05-22 21:59:35 +000084 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +000085 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +000086 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +000087}
88
89// createAnnotation - Create an annotation of the specified ID for the
90// specified object, using a register annotation creation function.
91//
92Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Chris Lattnerc0f483d2001-09-07 16:44:01 +000093 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000094 FactMapType::iterator I = getFactMap().find(ID.ID);
95 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +000096 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000097}