LLVM  mainline
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_IR_SYMBOLTABLELISTTRAITS_H
00026 #define LLVM_IR_SYMBOLTABLELISTTRAITS_H
00027 
00028 #include "llvm/ADT/ilist.h"
00029 
00030 namespace llvm {
00031 class ValueSymbolTable;
00032 
00033 template <typename NodeTy> class ilist_iterator;
00034 template <typename NodeTy, typename Traits> class iplist;
00035 template <typename Ty> struct ilist_traits;
00036 
00037 template <typename NodeTy>
00038 struct SymbolTableListSentinelTraits
00039     : public ilist_embedded_sentinel_traits<NodeTy> {};
00040 
00041 /// Template metafunction to get the parent type for a symbol table list.
00042 ///
00043 /// Implementations create a typedef called \c type so that we only need a
00044 /// single template parameter for the list and traits.
00045 template <typename NodeTy> struct SymbolTableListParentType {};
00046 class Argument;
00047 class BasicBlock;
00048 class Function;
00049 class Instruction;
00050 class GlobalVariable;
00051 class GlobalAlias;
00052 class Module;
00053 #define DEFINE_SYMBOL_TABLE_PARENT_TYPE(NODE, PARENT)                          \
00054   template <> struct SymbolTableListParentType<NODE> { typedef PARENT type; };
00055 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Instruction, BasicBlock)
00056 DEFINE_SYMBOL_TABLE_PARENT_TYPE(BasicBlock, Function)
00057 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Argument, Function)
00058 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Function, Module)
00059 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalVariable, Module)
00060 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalAlias, Module)
00061 #undef DEFINE_SYMBOL_TABLE_PARENT_TYPE
00062 
00063 template <typename NodeTy> class SymbolTableList;
00064 
00065 // ValueSubClass   - The type of objects that I hold, e.g. Instruction.
00066 // ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
00067 //
00068 template <typename ValueSubClass>
00069 class SymbolTableListTraits
00070     : public ilist_nextprev_traits<ValueSubClass>,
00071       public SymbolTableListSentinelTraits<ValueSubClass>,
00072       public ilist_node_traits<ValueSubClass> {
00073   typedef SymbolTableList<ValueSubClass> ListTy;
00074   typedef
00075       typename SymbolTableListParentType<ValueSubClass>::type ItemParentClass;
00076 
00077 public:
00078   SymbolTableListTraits() {}
00079 
00080 private:
00081   /// getListOwner - Return the object that owns this list.  If this is a list
00082   /// of instructions, it returns the BasicBlock that owns them.
00083   ItemParentClass *getListOwner() {
00084     size_t Offset(size_t(&((ItemParentClass*)nullptr->*ItemParentClass::
00085                            getSublistAccess(static_cast<ValueSubClass*>(nullptr)))));
00086     ListTy *Anchor(static_cast<ListTy *>(this));
00087     return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
00088                                               Offset);
00089   }
00090 
00091   static ListTy &getList(ItemParentClass *Par) {
00092     return Par->*(Par->getSublistAccess((ValueSubClass*)nullptr));
00093   }
00094 
00095   static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
00096     return Par ? toPtr(Par->getValueSymbolTable()) : nullptr;
00097   }
00098 
00099 public:
00100   void addNodeToList(ValueSubClass *V);
00101   void removeNodeFromList(ValueSubClass *V);
00102   void transferNodesFromList(SymbolTableListTraits &L2,
00103                              ilist_iterator<ValueSubClass> first,
00104                              ilist_iterator<ValueSubClass> last);
00105 //private:
00106   template<typename TPtr>
00107   void setSymTabObject(TPtr *, TPtr);
00108   static ValueSymbolTable *toPtr(ValueSymbolTable *P) { return P; }
00109   static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
00110 };
00111 
00112 /// List that automatically updates parent links and symbol tables.
00113 ///
00114 /// When nodes are inserted into and removed from this list, the associated
00115 /// symbol table will be automatically updated.  Similarly, parent links get
00116 /// updated automatically.
00117 template <typename NodeTy>
00118 class SymbolTableList : public iplist<NodeTy, SymbolTableListTraits<NodeTy>> {};
00119 
00120 } // End llvm namespace
00121 
00122 #endif