Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:10

0001 //===- llvm/SymbolTableListTraits.h - Traits for iplist ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines a generic class that is used to implement the automatic
0010 // symbol table manipulation that occurs when you put (for example) a named
0011 // instruction into a basic block.
0012 //
0013 // The way that this is implemented is by using a special traits class with the
0014 // intrusive list that makes up the list of instructions in a basic block.  When
0015 // a new element is added to the list of instructions, the traits class is
0016 // notified, allowing the symbol table to be updated.
0017 //
0018 // This generic class implements the traits class.  It must be generic so that
0019 // it can work for all uses it, which include lists of instructions, basic
0020 // blocks, arguments, functions, global variables, etc...
0021 //
0022 //===----------------------------------------------------------------------===//
0023 
0024 #ifndef LLVM_IR_SYMBOLTABLELISTTRAITS_H
0025 #define LLVM_IR_SYMBOLTABLELISTTRAITS_H
0026 
0027 #include "llvm/ADT/ilist.h"
0028 #include "llvm/ADT/simple_ilist.h"
0029 #include <cstddef>
0030 
0031 namespace llvm {
0032 
0033 class Argument;
0034 class BasicBlock;
0035 class Function;
0036 class GlobalAlias;
0037 class GlobalIFunc;
0038 class GlobalVariable;
0039 class Instruction;
0040 class Module;
0041 class ValueSymbolTable;
0042 
0043 /// Template metafunction to get the parent type for a symbol table list.
0044 ///
0045 /// Implementations create a typedef called \c type so that we only need a
0046 /// single template parameter for the list and traits.
0047 template <typename NodeTy> struct SymbolTableListParentType {};
0048 
0049 #define DEFINE_SYMBOL_TABLE_PARENT_TYPE(NODE, PARENT)                          \
0050   template <> struct SymbolTableListParentType<NODE> { using type = PARENT; };
0051 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Instruction, BasicBlock)
0052 DEFINE_SYMBOL_TABLE_PARENT_TYPE(BasicBlock, Function)
0053 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Argument, Function)
0054 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Function, Module)
0055 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalVariable, Module)
0056 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalAlias, Module)
0057 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalIFunc, Module)
0058 #undef DEFINE_SYMBOL_TABLE_PARENT_TYPE
0059 
0060 template <typename NodeTy, typename... Args> class SymbolTableList;
0061 
0062 // ValueSubClass   - The type of objects that I hold, e.g. Instruction.
0063 // ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
0064 // OptionsT        - Extra options to ilist nodes.
0065 //
0066 template <typename ValueSubClass, typename... Args>
0067 class SymbolTableListTraits : public ilist_alloc_traits<ValueSubClass> {
0068   using ListTy = SymbolTableList<ValueSubClass, Args...>;
0069   using iterator = typename simple_ilist<ValueSubClass, Args...>::iterator;
0070   using ItemParentClass =
0071       typename SymbolTableListParentType<ValueSubClass>::type;
0072 
0073 public:
0074   SymbolTableListTraits() = default;
0075 
0076 private:
0077   /// getListOwner - Return the object that owns this list.  If this is a list
0078   /// of instructions, it returns the BasicBlock that owns them.
0079   ItemParentClass *getListOwner() {
0080     size_t Offset = reinterpret_cast<size_t>(
0081         &((ItemParentClass *)nullptr->*ItemParentClass::getSublistAccess(
0082                                            static_cast<ValueSubClass *>(
0083                                                nullptr))));
0084     ListTy *Anchor = static_cast<ListTy *>(this);
0085     return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
0086                                               Offset);
0087   }
0088 
0089   static ListTy &getList(ItemParentClass *Par) {
0090     return Par->*(Par->getSublistAccess((ValueSubClass*)nullptr));
0091   }
0092 
0093   static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
0094     return Par ? toPtr(Par->getValueSymbolTable()) : nullptr;
0095   }
0096 
0097 public:
0098   void addNodeToList(ValueSubClass *V);
0099   void removeNodeFromList(ValueSubClass *V);
0100   void transferNodesFromList(SymbolTableListTraits &L2, iterator first,
0101                              iterator last);
0102   // private:
0103   template<typename TPtr>
0104   void setSymTabObject(TPtr *, TPtr);
0105   static ValueSymbolTable *toPtr(ValueSymbolTable *P) { return P; }
0106   static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
0107 };
0108 
0109 // The SymbolTableListTraits template is explicitly instantiated for the
0110 // following data types, so add extern template statements to prevent implicit
0111 // instantiation.
0112 extern template class SymbolTableListTraits<BasicBlock>;
0113 extern template class SymbolTableListTraits<Function>;
0114 extern template class SymbolTableListTraits<GlobalAlias>;
0115 extern template class SymbolTableListTraits<GlobalIFunc>;
0116 extern template class SymbolTableListTraits<GlobalVariable>;
0117 
0118 /// List that automatically updates parent links and symbol tables.
0119 ///
0120 /// When nodes are inserted into and removed from this list, the associated
0121 /// symbol table will be automatically updated.  Similarly, parent links get
0122 /// updated automatically.
0123 template <class T, typename... Args>
0124 class SymbolTableList : public iplist_impl<simple_ilist<T, Args...>,
0125                                            SymbolTableListTraits<T, Args...>> {
0126 };
0127 
0128 } // end namespace llvm
0129 
0130 #endif // LLVM_IR_SYMBOLTABLELISTTRAITS_H