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



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.