LLVM API Documentation

SymbolTableListTraits.h

Go to the documentation of this file.
00001 //===-- llvm/SymbolTableListTraits.h - Traits for iplist --------*- C++ -*-===//
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 defines a generic class that is used to implement the automatic
00011 // symbol table manipulation that occurs when you put (for example) a named
00012 // instruction into a basic block.
00013 //
00014 // The way that this is implemented is by using a special traits class with the
00015 // intrusive list that makes up the list of instructions in a basic block.  When
00016 // a new element is added to the list of instructions, the traits class is
00017 // notified, allowing the symbol table to be updated.
00018 //
00019 // This generic class implements the traits class.  It must be generic so that
00020 // it can work for all uses it, which include lists of instructions, basic
00021 // blocks, arguments, functions, global variables, etc...
00022 //
00023 //===----------------------------------------------------------------------===//
00024 
00025 #ifndef LLVM_SYMBOLTABLELISTTRAITS_H
00026 #define LLVM_SYMBOLTABLELISTTRAITS_H
00027 
00028 #include "llvm/ADT/ilist.h"
00029 
00030 namespace llvm {
00031 
00032 template<typename NodeTy> class ilist_iterator;
00033 template<typename NodeTy, typename Traits> class iplist;
00034 template<typename Ty> struct ilist_traits;
00035 
00036 // ValueSubClass   - The type of objects that I hold, e.g. Instruction.
00037 // ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
00038 //
00039 template<typename ValueSubClass, typename ItemParentClass>
00040 class SymbolTableListTraits : public ilist_default_traits<ValueSubClass> {
00041   typedef ilist_traits<ValueSubClass> TraitsClass;
00042 public:
00043   SymbolTableListTraits() {}
00044 
00045   /// getListOwner - Return the object that owns this list.  If this is a list
00046   /// of instructions, it returns the BasicBlock that owns them.
00047   ItemParentClass *getListOwner() {
00048     typedef iplist<ValueSubClass> ItemParentClass::*Sublist;
00049     Sublist Sub(ItemParentClass::
00050                 getSublistAccess(static_cast<ValueSubClass*>(0)));
00051     size_t Offset(size_t(&((ItemParentClass*)0->*Sub)));
00052     iplist<ValueSubClass>* Anchor(static_cast<iplist<ValueSubClass>*>(this));
00053     return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
00054                                               Offset);
00055   }
00056 
00057   static iplist<ValueSubClass> &getList(ItemParentClass *Par) {
00058     return Par->*(Par->getSublistAccess((ValueSubClass*)0));
00059   }
00060 
00061   static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
00062     return Par ? toPtr(Par->getValueSymbolTable()) : 0;
00063   }
00064 
00065   void addNodeToList(ValueSubClass *V);
00066   void removeNodeFromList(ValueSubClass *V);
00067   void transferNodesFromList(ilist_traits<ValueSubClass> &L2,
00068                              ilist_iterator<ValueSubClass> first,
00069                              ilist_iterator<ValueSubClass> last);
00070 //private:
00071   template<typename TPtr>
00072   void setSymTabObject(TPtr *, TPtr);
00073   static ValueSymbolTable *toPtr(ValueSymbolTable *P) { return P; }
00074   static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
00075 };
00076 
00077 } // End llvm namespace
00078 
00079 #endif