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