LLVM API Documentation

Annotation.cpp

Go to the documentation of this file.
00001 //===-- Annotation.cpp - Implement the Annotation Classes -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the AnnotationManager class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/Annotation.h"
00015 #include <map>
00016 using namespace llvm;
00017 
00018 Annotation::~Annotation() {}  // Designed to be subclassed
00019 
00020 Annotable::~Annotable() {   // Virtual because it's designed to be subclassed...
00021   Annotation *A = AnnotationList;
00022   while (A) {
00023     Annotation *Next = A->getNext();
00024     delete A;
00025     A = Next;
00026   }
00027 }
00028 
00029 typedef std::map<const std::string, unsigned> IDMapType;
00030 static unsigned IDCounter = 0;  // Unique ID counter
00031 
00032 // Static member to ensure initialiation on demand.
00033 static IDMapType &getIDMap() { static IDMapType TheMap; return TheMap; }
00034 
00035 // On demand annotation creation support...
00036 typedef Annotation *(*AnnFactory)(AnnotationID, const Annotable *, void *);
00037 typedef std::map<unsigned, std::pair<AnnFactory,void*> > FactMapType;
00038 
00039 static FactMapType *TheFactMap = 0;
00040 static FactMapType &getFactMap() {
00041   if (TheFactMap == 0)
00042     TheFactMap = new FactMapType();
00043   return *TheFactMap;
00044 }
00045 
00046 static void eraseFromFactMap(unsigned ID) {
00047   assert(TheFactMap && "No entries found!");
00048   TheFactMap->erase(ID);
00049   if (TheFactMap->empty()) {   // Delete when empty
00050     delete TheFactMap;
00051     TheFactMap = 0;
00052   }
00053 }
00054 
00055 AnnotationID AnnotationManager::getID(const std::string &Name) {  // Name -> ID
00056   IDMapType::iterator I = getIDMap().find(Name);
00057   if (I == getIDMap().end()) {
00058     getIDMap()[Name] = IDCounter++;   // Add a new element
00059     return IDCounter-1;
00060   }
00061   return I->second;
00062 }
00063 
00064 // getID - Name -> ID + registration of a factory function for demand driven
00065 // annotation support.
00066 AnnotationID AnnotationManager::getID(const std::string &Name, Factory Fact,
00067                                       void *Data) {
00068   AnnotationID Result(getID(Name));
00069   registerAnnotationFactory(Result, Fact, Data);
00070   return Result;
00071 }
00072 
00073 // getName - This function is especially slow, but that's okay because it should
00074 // only be used for debugging.
00075 //
00076 const std::string &AnnotationManager::getName(AnnotationID ID) {  // ID -> Name
00077   IDMapType &TheMap = getIDMap();
00078   for (IDMapType::iterator I = TheMap.begin(); ; ++I) {
00079     assert(I != TheMap.end() && "Annotation ID is unknown!");
00080     if (I->second == ID.ID) return I->first;
00081   }
00082 }
00083 
00084 // registerAnnotationFactory - This method is used to register a callback
00085 // function used to create an annotation on demand if it is needed by the
00086 // Annotable::findOrCreateAnnotation method.
00087 //
00088 void AnnotationManager::registerAnnotationFactory(AnnotationID ID, AnnFactory F,
00089                                                   void *ExtraData) {
00090   if (F)
00091     getFactMap()[ID.ID] = std::make_pair(F, ExtraData);
00092   else
00093     eraseFromFactMap(ID.ID);
00094 }
00095 
00096 // createAnnotation - Create an annotation of the specified ID for the
00097 // specified object, using a register annotation creation function.
00098 //
00099 Annotation *AnnotationManager::createAnnotation(AnnotationID ID,
00100                                                 const Annotable *Obj) {
00101   FactMapType::iterator I = getFactMap().find(ID.ID);
00102   if (I == getFactMap().end()) return 0;
00103   return I->second.first(ID, Obj, I->second.second);
00104 }