blob: b46e218416cd359ba96e5f55cb39c8d5d357bcdf [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner8dc89a32001-08-23 17:07:56 +00009//
10// This file implements the AnnotationManager class.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/Annotation.h"
Misha Brukman710d1ce2004-11-09 04:27:19 +000015#include <map>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000016using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000017
Chris Lattner1cd4c722004-02-26 07:24:18 +000018Annotation::~Annotation() {} // Designed to be subclassed
19
20Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
21 Annotation *A = AnnotationList;
22 while (A) {
23 Annotation *Next = A->getNext();
24 delete A;
25 A = Next;
26 }
27}
28
Chris Lattner01e770a2003-05-22 21:59:35 +000029typedef std::map<const std::string, unsigned> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000030static unsigned IDCounter = 0; // Unique ID counter
31
32// Static member to ensure initialiation on demand.
33static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
34
35// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000036typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000037typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000038
39static FactMapType *TheFactMap = 0;
40static FactMapType &getFactMap() {
41 if (TheFactMap == 0)
42 TheFactMap = new FactMapType();
43 return *TheFactMap;
44}
45
46static void eraseFromFactMap(unsigned ID) {
47 assert(TheFactMap && "No entries found!");
48 TheFactMap->erase(ID);
49 if (TheFactMap->empty()) { // Delete when empty
50 delete TheFactMap;
51 TheFactMap = 0;
52 }
53}
Chris Lattner8dc89a32001-08-23 17:07:56 +000054
Chris Lattner01e770a2003-05-22 21:59:35 +000055AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID
Chris Lattner8dc89a32001-08-23 17:07:56 +000056 IDMapType::iterator I = getIDMap().find(Name);
57 if (I == getIDMap().end()) {
58 getIDMap()[Name] = IDCounter++; // Add a new element
59 return IDCounter-1;
60 }
61 return I->second;
62}
63
Chris Lattner82072d42001-09-09 21:02:38 +000064// getID - Name -> ID + registration of a factory function for demand driven
65// annotation support.
Chris Lattner01e770a2003-05-22 21:59:35 +000066AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
Misha Brukman710d1ce2004-11-09 04:27:19 +000067 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000068 AnnotationID Result(getID(Name));
69 registerAnnotationFactory(Result, Fact, Data);
Misha Brukmanf976c852005-04-21 22:55:34 +000070 return Result;
Chris Lattner82072d42001-09-09 21:02:38 +000071}
72
Chris Lattner8dc89a32001-08-23 17:07:56 +000073// getName - This function is especially slow, but that's okay because it should
74// only be used for debugging.
75//
Chris Lattner01e770a2003-05-22 21:59:35 +000076const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Chris Lattner8dc89a32001-08-23 17:07:56 +000077 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;
81 }
82}
83
Chris Lattner8dc89a32001-08-23 17:07:56 +000084// registerAnnotationFactory - This method is used to register a callback
Misha Brukmanf976c852005-04-21 22:55:34 +000085// function used to create an annotation on demand if it is needed by the
Chris Lattner8dc89a32001-08-23 17:07:56 +000086// Annotable::findOrCreateAnnotation method.
87//
Misha Brukman710d1ce2004-11-09 04:27:19 +000088void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
89 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000090 if (F)
Chris Lattner01e770a2003-05-22 21:59:35 +000091 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +000092 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +000093 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +000094}
95
96// createAnnotation - Create an annotation of the specified ID for the
97// specified object, using a register annotation creation function.
98//
Misha Brukmanf976c852005-04-21 22:55:34 +000099Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Misha Brukman710d1ce2004-11-09 04:27:19 +0000100 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +0000101 FactMapType::iterator I = getFactMap().find(ID.ID);
102 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +0000103 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000104}