blob: 9d5e5457d9a814afc622378152bbd05150412e91 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Chris Lattnerc055a912006-10-04 22:13:11 +000015#include "llvm/Support/ManagedStatic.h"
Misha Brukman710d1ce2004-11-09 04:27:19 +000016#include <map>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000017using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000018
Chris Lattner1cd4c722004-02-26 07:24:18 +000019Annotation::~Annotation() {} // Designed to be subclassed
20
21Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
22 Annotation *A = AnnotationList;
23 while (A) {
24 Annotation *Next = A->getNext();
25 delete A;
26 A = Next;
27 }
28}
29
Chris Lattner01e770a2003-05-22 21:59:35 +000030typedef std::map<const std::string, unsigned> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000031static unsigned IDCounter = 0; // Unique ID counter
32
33// Static member to ensure initialiation on demand.
Chris Lattnerc055a912006-10-04 22:13:11 +000034static ManagedStatic<IDMapType> IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000035
36// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000037typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000038typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000039
40static FactMapType *TheFactMap = 0;
41static FactMapType &getFactMap() {
42 if (TheFactMap == 0)
43 TheFactMap = new FactMapType();
44 return *TheFactMap;
45}
46
47static void eraseFromFactMap(unsigned ID) {
48 assert(TheFactMap && "No entries found!");
49 TheFactMap->erase(ID);
50 if (TheFactMap->empty()) { // Delete when empty
51 delete TheFactMap;
52 TheFactMap = 0;
53 }
54}
Chris Lattner8dc89a32001-08-23 17:07:56 +000055
Chris Lattner01e770a2003-05-22 21:59:35 +000056AnnotationID AnnotationManager::getID(const std::string &Name) { // Name -> ID
Chris Lattnerc055a912006-10-04 22:13:11 +000057 IDMapType::iterator I = IDMap->find(Name);
58 if (I == IDMap->end()) {
59 (*IDMap)[Name] = IDCounter++; // Add a new element
Chris Lattner8dc89a32001-08-23 17:07:56 +000060 return IDCounter-1;
61 }
62 return I->second;
63}
64
Chris Lattner82072d42001-09-09 21:02:38 +000065// getID - Name -> ID + registration of a factory function for demand driven
66// annotation support.
Chris Lattner01e770a2003-05-22 21:59:35 +000067AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
Misha Brukman710d1ce2004-11-09 04:27:19 +000068 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000069 AnnotationID Result(getID(Name));
70 registerAnnotationFactory(Result, Fact, Data);
Misha Brukmanf976c852005-04-21 22:55:34 +000071 return Result;
Chris Lattner82072d42001-09-09 21:02:38 +000072}
73
Chris Lattner8dc89a32001-08-23 17:07:56 +000074// getName - This function is especially slow, but that's okay because it should
75// only be used for debugging.
76//
Chris Lattner01e770a2003-05-22 21:59:35 +000077const std::string &AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Chris Lattnerc055a912006-10-04 22:13:11 +000078 IDMapType &TheMap = *IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000079 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
80 assert(I != TheMap.end() && "Annotation ID is unknown!");
81 if (I->second == ID.ID) return I->first;
82 }
83}
84
Chris Lattner8dc89a32001-08-23 17:07:56 +000085// registerAnnotationFactory - This method is used to register a callback
Misha Brukmanf976c852005-04-21 22:55:34 +000086// function used to create an annotation on demand if it is needed by the
Chris Lattner8dc89a32001-08-23 17:07:56 +000087// Annotable::findOrCreateAnnotation method.
88//
Misha Brukman710d1ce2004-11-09 04:27:19 +000089void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
90 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +000091 if (F)
Chris Lattner01e770a2003-05-22 21:59:35 +000092 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +000093 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +000094 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +000095}
96
97// createAnnotation - Create an annotation of the specified ID for the
98// specified object, using a register annotation creation function.
99//
Misha Brukmanf976c852005-04-21 22:55:34 +0000100Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Misha Brukman710d1ce2004-11-09 04:27:19 +0000101 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +0000102 FactMapType::iterator I = getFactMap().find(ID.ID);
103 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +0000104 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000105}