blob: fdf6dc717f46dc8cb7f3bc4b826b9065b21f1503 [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>
Nuno Lopes440954c2008-11-27 16:42:44 +000017#include <cstring>
Chris Lattner2cdd21c2003-12-14 21:35:53 +000018using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000019
Chris Lattner1cd4c722004-02-26 07:24:18 +000020Annotation::~Annotation() {} // Designed to be subclassed
21
22Annotable::~Annotable() { // Virtual because it's designed to be subclassed...
23 Annotation *A = AnnotationList;
24 while (A) {
25 Annotation *Next = A->getNext();
26 delete A;
27 A = Next;
28 }
29}
30
Nuno Lopes4ba9aac2008-11-27 16:37:02 +000031namespace {
32 class StrCmp {
33 public:
34 bool operator()(const char *a, const char *b) {
35 return strcmp(a, b) < 0;
36 }
37 };
38}
39
40typedef std::map<const char*, unsigned, StrCmp> IDMapType;
Chris Lattner8dc89a32001-08-23 17:07:56 +000041static unsigned IDCounter = 0; // Unique ID counter
42
43// Static member to ensure initialiation on demand.
Chris Lattnerc055a912006-10-04 22:13:11 +000044static ManagedStatic<IDMapType> IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000045
46// On demand annotation creation support...
Chris Lattnerc0f483d2001-09-07 16:44:01 +000047typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
Chris Lattner01e770a2003-05-22 21:59:35 +000048typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
Chris Lattner3fa61eb2003-01-13 00:52:43 +000049
50static FactMapType *TheFactMap = 0;
51static FactMapType &getFactMap() {
52 if (TheFactMap == 0)
53 TheFactMap = new FactMapType();
54 return *TheFactMap;
55}
56
57static void eraseFromFactMap(unsigned ID) {
58 assert(TheFactMap && "No entries found!");
59 TheFactMap->erase(ID);
60 if (TheFactMap->empty()) { // Delete when empty
61 delete TheFactMap;
62 TheFactMap = 0;
63 }
64}
Chris Lattner8dc89a32001-08-23 17:07:56 +000065
Nuno Lopes08d67c72008-11-26 00:00:44 +000066AnnotationID AnnotationManager::getID(const char *Name) { // Name -> ID
Chris Lattnerc055a912006-10-04 22:13:11 +000067 IDMapType::iterator I = IDMap->find(Name);
68 if (I == IDMap->end()) {
69 (*IDMap)[Name] = IDCounter++; // Add a new element
Dan Gohmanb5660dc2008-02-20 16:44:09 +000070 return AnnotationID(IDCounter-1);
Chris Lattner8dc89a32001-08-23 17:07:56 +000071 }
Dan Gohmanb5660dc2008-02-20 16:44:09 +000072 return AnnotationID(I->second);
Chris Lattner8dc89a32001-08-23 17:07:56 +000073}
74
Chris Lattner82072d42001-09-09 21:02:38 +000075// getID - Name -> ID + registration of a factory function for demand driven
76// annotation support.
Nuno Lopes08d67c72008-11-26 00:00:44 +000077AnnotationID AnnotationManager::getID(const char *Name, Factory Fact,
Misha Brukman710d1ce2004-11-09 04:27:19 +000078 void *Data) {
Chris Lattner82072d42001-09-09 21:02:38 +000079 AnnotationID Result(getID(Name));
80 registerAnnotationFactory(Result, Fact, Data);
Misha Brukmanf976c852005-04-21 22:55:34 +000081 return Result;
Chris Lattner82072d42001-09-09 21:02:38 +000082}
83
Chris Lattner8dc89a32001-08-23 17:07:56 +000084// getName - This function is especially slow, but that's okay because it should
85// only be used for debugging.
86//
Nuno Lopes08d67c72008-11-26 00:00:44 +000087const char *AnnotationManager::getName(AnnotationID ID) { // ID -> Name
Chris Lattnerc055a912006-10-04 22:13:11 +000088 IDMapType &TheMap = *IDMap;
Chris Lattner8dc89a32001-08-23 17:07:56 +000089 for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
90 assert(I != TheMap.end() && "Annotation ID is unknown!");
91 if (I->second == ID.ID) return I->first;
92 }
93}
94
Chris Lattner8dc89a32001-08-23 17:07:56 +000095// registerAnnotationFactory - This method is used to register a callback
Misha Brukmanf976c852005-04-21 22:55:34 +000096// function used to create an annotation on demand if it is needed by the
Chris Lattner8dc89a32001-08-23 17:07:56 +000097// Annotable::findOrCreateAnnotation method.
98//
Misha Brukman710d1ce2004-11-09 04:27:19 +000099void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
100 void *ExtraData) {
Chris Lattner8dc89a32001-08-23 17:07:56 +0000101 if (F)
Chris Lattner01e770a2003-05-22 21:59:35 +0000102 getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000103 else
Chris Lattner3fa61eb2003-01-13 00:52:43 +0000104 eraseFromFactMap(ID.ID);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000105}
106
107// createAnnotation - Create an annotation of the specified ID for the
108// specified object, using a register annotation creation function.
109//
Misha Brukmanf976c852005-04-21 22:55:34 +0000110Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
Misha Brukman710d1ce2004-11-09 04:27:19 +0000111 const Annotable *Obj) {
Chris Lattner8dc89a32001-08-23 17:07:56 +0000112 FactMapType::iterator I = getFactMap().find(ID.ID);
113 if (I == getFactMap().end()) return 0;
Chris Lattnerda8f0042001-08-27 05:19:10 +0000114 return I->second.first(ID, Obj, I->second.second);
Chris Lattner8dc89a32001-08-23 17:07:56 +0000115}