Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:46

0001 //===- VTTBuilder.h - C++ VTT layout builder --------------------*- 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 contains code dealing with generation of the layout of virtual table
0010 // tables (VTT).
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_AST_VTTBUILDER_H
0015 #define LLVM_CLANG_AST_VTTBUILDER_H
0016 
0017 #include "clang/AST/BaseSubobject.h"
0018 #include "clang/AST/CharUnits.h"
0019 #include "clang/Basic/LLVM.h"
0020 #include "llvm/ADT/DenseMap.h"
0021 #include "llvm/ADT/PointerIntPair.h"
0022 #include "llvm/ADT/SmallPtrSet.h"
0023 #include "llvm/ADT/SmallVector.h"
0024 #include <cstdint>
0025 
0026 namespace clang {
0027 
0028 class ASTContext;
0029 class ASTRecordLayout;
0030 class CXXRecordDecl;
0031 
0032 class VTTVTable {
0033   llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> BaseAndIsVirtual;
0034   CharUnits BaseOffset;
0035 
0036 public:
0037   VTTVTable() = default;
0038   VTTVTable(const CXXRecordDecl *Base, CharUnits BaseOffset, bool BaseIsVirtual)
0039       : BaseAndIsVirtual(Base, BaseIsVirtual), BaseOffset(BaseOffset) {}
0040   VTTVTable(BaseSubobject Base, bool BaseIsVirtual)
0041       : BaseAndIsVirtual(Base.getBase(), BaseIsVirtual),
0042         BaseOffset(Base.getBaseOffset()) {}
0043 
0044   const CXXRecordDecl *getBase() const {
0045     return BaseAndIsVirtual.getPointer();
0046   }
0047 
0048   CharUnits getBaseOffset() const {
0049     return BaseOffset;
0050   }
0051 
0052   bool isVirtual() const {
0053     return BaseAndIsVirtual.getInt();
0054   }
0055 
0056   BaseSubobject getBaseSubobject() const {
0057     return BaseSubobject(getBase(), getBaseOffset());
0058   }
0059 };
0060 
0061 struct VTTComponent {
0062   uint64_t VTableIndex;
0063   BaseSubobject VTableBase;
0064 
0065   VTTComponent() = default;
0066   VTTComponent(uint64_t VTableIndex, BaseSubobject VTableBase)
0067      : VTableIndex(VTableIndex), VTableBase(VTableBase) {}
0068 };
0069 
0070 /// Class for building VTT layout information.
0071 class VTTBuilder {
0072   ASTContext &Ctx;
0073 
0074   /// The most derived class for which we're building this vtable.
0075   const CXXRecordDecl *MostDerivedClass;
0076 
0077   using VTTVTablesVectorTy = SmallVector<VTTVTable, 64>;
0078 
0079   /// The VTT vtables.
0080   VTTVTablesVectorTy VTTVTables;
0081 
0082   using VTTComponentsVectorTy = SmallVector<VTTComponent, 64>;
0083 
0084   /// The VTT components.
0085   VTTComponentsVectorTy VTTComponents;
0086 
0087   /// The AST record layout of the most derived class.
0088   const ASTRecordLayout &MostDerivedClassLayout;
0089 
0090   using VisitedVirtualBasesSetTy = llvm::SmallPtrSet<const CXXRecordDecl *, 4>;
0091 
0092   using AddressPointsMapTy = llvm::DenseMap<BaseSubobject, uint64_t>;
0093 
0094   /// The sub-VTT indices for the bases of the most derived class.
0095   llvm::DenseMap<BaseSubobject, uint64_t> SubVTTIndices;
0096 
0097   /// The secondary virtual pointer indices of all subobjects of
0098   /// the most derived class.
0099   llvm::DenseMap<BaseSubobject, uint64_t> SecondaryVirtualPointerIndices;
0100 
0101   /// Whether the VTT builder should generate LLVM IR for the VTT.
0102   bool GenerateDefinition;
0103 
0104   /// Add a vtable pointer to the VTT currently being built.
0105   void AddVTablePointer(BaseSubobject Base, uint64_t VTableIndex,
0106                         const CXXRecordDecl *VTableClass);
0107 
0108   /// Lay out the secondary VTTs of the given base subobject.
0109   void LayoutSecondaryVTTs(BaseSubobject Base);
0110 
0111   /// Lay out the secondary virtual pointers for the given base
0112   /// subobject.
0113   ///
0114   /// \param BaseIsMorallyVirtual whether the base subobject is a virtual base
0115   /// or a direct or indirect base of a virtual base.
0116   void LayoutSecondaryVirtualPointers(BaseSubobject Base,
0117                                       bool BaseIsMorallyVirtual,
0118                                       uint64_t VTableIndex,
0119                                       const CXXRecordDecl *VTableClass,
0120                                       VisitedVirtualBasesSetTy &VBases);
0121 
0122   /// Lay out the secondary virtual pointers for the given base
0123   /// subobject.
0124   void LayoutSecondaryVirtualPointers(BaseSubobject Base,
0125                                       uint64_t VTableIndex);
0126 
0127   /// Lay out the VTTs for the virtual base classes of the given
0128   /// record declaration.
0129   void LayoutVirtualVTTs(const CXXRecordDecl *RD,
0130                          VisitedVirtualBasesSetTy &VBases);
0131 
0132   /// Lay out the VTT for the given subobject, including any
0133   /// secondary VTTs, secondary virtual pointers and virtual VTTs.
0134   void LayoutVTT(BaseSubobject Base, bool BaseIsVirtual);
0135 
0136 public:
0137   VTTBuilder(ASTContext &Ctx, const CXXRecordDecl *MostDerivedClass,
0138              bool GenerateDefinition);
0139 
0140   // Returns a reference to the VTT components.
0141   const VTTComponentsVectorTy &getVTTComponents() const {
0142     return VTTComponents;
0143   }
0144 
0145   // Returns a reference to the VTT vtables.
0146   const VTTVTablesVectorTy &getVTTVTables() const {
0147     return VTTVTables;
0148   }
0149 
0150   /// Returns a reference to the sub-VTT indices.
0151   const llvm::DenseMap<BaseSubobject, uint64_t> &getSubVTTIndices() const {
0152     return SubVTTIndices;
0153   }
0154 
0155   /// Returns a reference to the secondary virtual pointer indices.
0156   const llvm::DenseMap<BaseSubobject, uint64_t> &
0157   getSecondaryVirtualPointerIndices() const {
0158     return SecondaryVirtualPointerIndices;
0159   }
0160 };
0161 
0162 } // namespace clang
0163 
0164 #endif // LLVM_CLANG_AST_VTTBUILDER_H