2004-09-02 00:55:40 +02:00
|
|
|
//===-- llvm/Support/Annotation.h - Annotation classes ----------*- C++ -*-===//
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2003-10-20 21:46:57 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 22:48:15 +02:00
|
|
|
//
|
2003-10-20 21:46:57 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-08-23 19:07:56 +02:00
|
|
|
//
|
|
|
|
// This file contains the declarations for two classes: Annotation & Annotable.
|
|
|
|
// Using these two simple classes, anything that derives from Annotable can have
|
|
|
|
// Annotation subclasses attached to them, ready for easy retrieval.
|
|
|
|
//
|
2003-01-14 22:29:58 +01:00
|
|
|
// Annotations are designed to be easily attachable to various classes.
|
2001-08-23 19:07:56 +02:00
|
|
|
//
|
2003-01-14 22:29:58 +01:00
|
|
|
// The AnnotationManager class is essential for using these classes. It is
|
|
|
|
// responsible for turning Annotation name strings into tokens [unique id #'s]
|
|
|
|
// that may be used to search for and create annotations.
|
2001-08-23 19:07:56 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#ifndef LLVM_SUPPORT_ANNOTATION_H
|
|
|
|
#define LLVM_SUPPORT_ANNOTATION_H
|
2001-08-23 19:07:56 +02:00
|
|
|
|
2003-07-25 19:34:17 +02:00
|
|
|
#include <cassert>
|
2003-06-30 23:59:07 +02:00
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
namespace llvm {
|
|
|
|
|
2001-08-23 19:07:56 +02:00
|
|
|
class AnnotationID;
|
|
|
|
class Annotation;
|
|
|
|
class Annotable;
|
2004-10-27 18:14:51 +02:00
|
|
|
struct AnnotationManager;
|
2001-08-23 19:07:56 +02:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// AnnotationID - This class is a thin wrapper around an unsigned integer that
|
|
|
|
// is used to hopefully prevent errors using AnnotationID's. They may be copied
|
|
|
|
// freely around and passed byvalue with little or no overhead.
|
|
|
|
//
|
|
|
|
class AnnotationID {
|
2004-10-27 18:14:51 +02:00
|
|
|
friend struct AnnotationManager;
|
2001-08-23 19:07:56 +02:00
|
|
|
unsigned ID;
|
|
|
|
|
|
|
|
AnnotationID(); // Default ctor is disabled
|
2008-02-20 17:44:09 +01:00
|
|
|
|
|
|
|
// AnnotationID is only creatable from AnnMgr.
|
|
|
|
explicit inline AnnotationID(unsigned i) : ID(i) {}
|
2001-08-23 19:07:56 +02:00
|
|
|
public:
|
|
|
|
inline AnnotationID(const AnnotationID &A) : ID(A.ID) {}
|
|
|
|
|
|
|
|
inline bool operator==(const AnnotationID &A) const {
|
|
|
|
return A.ID == ID;
|
|
|
|
}
|
|
|
|
inline bool operator<(const AnnotationID &A) const {
|
|
|
|
return ID < A.ID;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Annotation Class - This class serves as a base class for any specific
|
|
|
|
// annotations that you might need. Simply subclass this to add extra
|
|
|
|
// information to the annotations.
|
|
|
|
//
|
|
|
|
class Annotation {
|
|
|
|
friend class Annotable; // Annotable manipulates Next list
|
|
|
|
AnnotationID ID; // ID number, as obtained from AnnotationManager
|
|
|
|
Annotation *Next; // The next annotation in the linked list
|
|
|
|
public:
|
2008-02-20 17:44:09 +01:00
|
|
|
explicit inline Annotation(AnnotationID id) : ID(id), Next(0) {}
|
2004-02-26 08:24:18 +01:00
|
|
|
virtual ~Annotation(); // Designed to be subclassed
|
2001-08-23 19:07:56 +02:00
|
|
|
|
|
|
|
// getID - Return the unique ID# of this annotation
|
|
|
|
inline AnnotationID getID() const { return ID; }
|
|
|
|
|
|
|
|
// getNext - Return the next annotation in the list...
|
|
|
|
inline Annotation *getNext() const { return Next; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Annotable - This class is used as a base class for all objects that would
|
2005-07-27 07:53:44 +02:00
|
|
|
// like to have annotation capability.
|
2001-08-23 19:07:56 +02:00
|
|
|
//
|
|
|
|
// Annotable objects keep their annotation list sorted as annotations are
|
|
|
|
// inserted and deleted. This is used to ensure that annotations with identical
|
|
|
|
// ID#'s are stored sequentially.
|
|
|
|
//
|
|
|
|
class Annotable {
|
2001-09-07 18:16:35 +02:00
|
|
|
mutable Annotation *AnnotationList;
|
2002-07-24 22:01:57 +02:00
|
|
|
|
|
|
|
Annotable(const Annotable &); // Do not implement
|
|
|
|
void operator=(const Annotable &); // Do not implement
|
2001-08-23 19:07:56 +02:00
|
|
|
public:
|
|
|
|
Annotable() : AnnotationList(0) {}
|
2004-06-27 20:36:39 +02:00
|
|
|
~Annotable();
|
2001-08-23 19:07:56 +02:00
|
|
|
|
|
|
|
// getAnnotation - Search the list for annotations of the specified ID. The
|
|
|
|
// pointer returned is either null (if no annotations of the specified ID
|
|
|
|
// exist), or it points to the first element of a potentially list of elements
|
|
|
|
// with identical ID #'s.
|
|
|
|
//
|
|
|
|
Annotation *getAnnotation(AnnotationID ID) const {
|
|
|
|
for (Annotation *A = AnnotationList; A; A = A->getNext())
|
|
|
|
if (A->getID() == ID) return A;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// getOrCreateAnnotation - Search through the annotation list, if there is
|
|
|
|
// no annotation with the specified ID, then use the AnnotationManager to
|
|
|
|
// create one.
|
|
|
|
//
|
2001-09-07 18:16:35 +02:00
|
|
|
inline Annotation *getOrCreateAnnotation(AnnotationID ID) const;
|
2001-08-23 19:07:56 +02:00
|
|
|
|
|
|
|
// addAnnotation - Insert the annotation into the list in a sorted location.
|
|
|
|
//
|
2001-09-07 18:16:35 +02:00
|
|
|
void addAnnotation(Annotation *A) const {
|
2001-08-23 19:07:56 +02:00
|
|
|
assert(A->Next == 0 && "Annotation already in list?!?");
|
|
|
|
|
|
|
|
Annotation **AL = &AnnotationList;
|
|
|
|
while (*AL && (*AL)->ID < A->getID()) // Find where to insert annotation
|
|
|
|
AL = &((*AL)->Next);
|
|
|
|
A->Next = *AL; // Link the annotation in
|
|
|
|
*AL = A;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlinkAnnotation - Remove the first annotation of the specified ID... and
|
|
|
|
// then return the unlinked annotation. The annotation object is not deleted.
|
|
|
|
//
|
2001-09-07 18:16:35 +02:00
|
|
|
inline Annotation *unlinkAnnotation(AnnotationID ID) const {
|
2001-08-23 19:07:56 +02:00
|
|
|
for (Annotation **A = &AnnotationList; *A; A = &((*A)->Next))
|
|
|
|
if ((*A)->getID() == ID) {
|
2005-04-22 05:35:49 +02:00
|
|
|
Annotation *Ret = *A;
|
|
|
|
*A = Ret->Next;
|
|
|
|
Ret->Next = 0;
|
|
|
|
return Ret;
|
2001-08-23 19:07:56 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteAnnotation - Delete the first annotation of the specified ID in the
|
|
|
|
// list. Unlink unlinkAnnotation, this actually deletes the annotation object
|
|
|
|
//
|
2001-09-07 18:16:35 +02:00
|
|
|
bool deleteAnnotation(AnnotationID ID) const {
|
2001-08-23 19:07:56 +02:00
|
|
|
Annotation *A = unlinkAnnotation(ID);
|
|
|
|
delete A;
|
|
|
|
return A != 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// AnnotationManager - This class is primarily responsible for maintaining a
|
|
|
|
// one-to-one mapping between string Annotation names and Annotation ID numbers.
|
|
|
|
//
|
|
|
|
// Compared to the rest of the Annotation system, these mapping methods are
|
2005-04-21 22:48:15 +02:00
|
|
|
// relatively slow, so they should be avoided by locally caching Annotation
|
2001-08-23 19:07:56 +02:00
|
|
|
// ID #'s. These methods are safe to call at any time, even by static ctors, so
|
|
|
|
// they should be used by static ctors most of the time.
|
|
|
|
//
|
|
|
|
// This class also provides support for annotations that are created on demand
|
|
|
|
// by the Annotable::getOrCreateAnnotation method. To get this to work, simply
|
2005-04-21 22:48:15 +02:00
|
|
|
// register an annotation handler
|
2001-08-23 19:07:56 +02:00
|
|
|
//
|
|
|
|
struct AnnotationManager {
|
2001-09-09 23:00:23 +02:00
|
|
|
typedef Annotation *(*Factory)(AnnotationID, const Annotable *, void*);
|
|
|
|
|
2001-08-23 19:07:56 +02:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Basic ID <-> Name map functionality
|
|
|
|
|
2008-11-26 01:00:44 +01:00
|
|
|
static AnnotationID getID(const char *Name); // Name -> ID
|
|
|
|
static const char *getName(AnnotationID ID); // ID -> Name
|
2001-08-23 19:07:56 +02:00
|
|
|
|
2001-09-09 23:00:23 +02:00
|
|
|
// getID - Name -> ID + registration of a factory function for demand driven
|
|
|
|
// annotation support.
|
2008-11-26 01:00:44 +01:00
|
|
|
static AnnotationID getID(const char *Name, Factory Fact, void *Data = 0);
|
2001-08-23 19:07:56 +02:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Annotation creation on demand support...
|
|
|
|
|
|
|
|
// registerAnnotationFactory - This method is used to register a callback
|
2005-04-21 22:48:15 +02:00
|
|
|
// function used to create an annotation on demand if it is needed by the
|
2001-08-23 19:07:56 +02:00
|
|
|
// Annotable::getOrCreateAnnotation method.
|
|
|
|
//
|
2001-09-09 23:00:23 +02:00
|
|
|
static void registerAnnotationFactory(AnnotationID ID, Factory Func,
|
2005-04-22 05:35:49 +02:00
|
|
|
void *ExtraData = 0);
|
2001-08-23 19:07:56 +02:00
|
|
|
|
|
|
|
// createAnnotation - Create an annotation of the specified ID for the
|
|
|
|
// specified object, using a register annotation creation function.
|
|
|
|
//
|
2001-09-07 18:16:35 +02:00
|
|
|
static Annotation *createAnnotation(AnnotationID ID, const Annotable *Obj);
|
2001-08-23 19:07:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// getOrCreateAnnotation - Search through the annotation list, if there is
|
|
|
|
// no annotation with the specified ID, then use the AnnotationManager to
|
|
|
|
// create one.
|
|
|
|
//
|
2001-09-07 18:16:35 +02:00
|
|
|
inline Annotation *Annotable::getOrCreateAnnotation(AnnotationID ID) const {
|
2001-08-23 19:07:56 +02:00
|
|
|
Annotation *A = getAnnotation(ID); // Fast path, check for preexisting ann
|
|
|
|
if (A) return A;
|
|
|
|
|
|
|
|
// No annotation found, ask the annotation manager to create an annotation...
|
|
|
|
A = AnnotationManager::createAnnotation(ID, this);
|
|
|
|
assert(A && "AnnotationManager could not create annotation!");
|
|
|
|
addAnnotation(A);
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2003-11-11 23:41:34 +01:00
|
|
|
} // End namespace llvm
|
|
|
|
|
2001-08-23 19:07:56 +02:00
|
|
|
#endif
|