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_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 // ValueSubClass   - The type of objects that I hold, e.g. Instruction.
00038 // ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
00039 //
00040 template<typename ValueSubClass, typename ItemParentClass>
00041 class SymbolTableListTraits : public ilist_default_traits<ValueSubClass> {
00042   typedef ilist_traits<ValueSubClass> TraitsClass;
00043 public:
00044   SymbolTableListTraits() {}
00045 
00046   /// getListOwner - Return the object that owns this list.  If this is a list
00047   /// of instructions, it returns the BasicBlock that owns them.
00048   ItemParentClass *getListOwner() {
00049     size_t Offset(size_t(&((ItemParentClass*)nullptr->*ItemParentClass::
00050                            getSublistAccess(static_cast<ValueSubClass*>(nullptr)))));
00051     iplist<ValueSubClass>* Anchor(static_cast<iplist<ValueSubClass>*>(this));
00052     return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
00053                                               Offset);
00054   }
00055 
00056   static iplist<ValueSubClass> &getList(ItemParentClass *Par) {
00057     return Par->*(Par->getSublistAccess((ValueSubClass*)nullptr));
00058   }
00059 
00060   static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
00061     return Par ? toPtr(Par->getValueSymbolTable()) : nullptr;
00062   }
00063 
00064   void addNodeToList(ValueSubClass *V);
00065   void removeNodeFromList(ValueSubClass *V);
00066   void transferNodesFromList(ilist_traits<ValueSubClass> &L2,
00067                              ilist_iterator<ValueSubClass> first,
00068                              ilist_iterator<ValueSubClass> last);
00069 //private:
00070   template<typename TPtr>
00071   void setSymTabObject(TPtr *, TPtr);
00072   static ValueSymbolTable *toPtr(ValueSymbolTable *P) { return P; }
00073   static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
00074 };
00075 
00076 } // End llvm namespace
00077 
00078 #endif