1 //===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code dealing with C++ code generation of virtual tables.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
15 #define LLVM_CLANG_LIB_CODEGEN_CGVTABLES_H
16 
17 #include "clang/AST/BaseSubobject.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/AST/GlobalDecl.h"
20 #include "clang/AST/VTableBuilder.h"
21 #include "clang/Basic/ABI.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/IR/GlobalVariable.h"
24 
25 namespace clang {
26   class CXXRecordDecl;
27 
28 namespace CodeGen {
29   class CodeGenModule;
30   class ConstantArrayBuilder;
31 
32 class CodeGenVTables {
33   CodeGenModule &CGM;
34 
35   VTableContextBase *VTContext;
36 
37   /// VTableAddressPointsMapTy - Address points for a single vtable.
38   typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
39 
40   typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
41   typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
42 
43   /// SubVTTIndicies - Contains indices into the various sub-VTTs.
44   SubVTTIndiciesMapTy SubVTTIndicies;
45 
46   typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
47     SecondaryVirtualPointerIndicesMapTy;
48 
49   /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
50   /// indices.
51   SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
52 
53   /// Cache for the pure virtual member call function.
54   llvm::Constant *PureVirtualFn = nullptr;
55 
56   /// Cache for the deleted virtual member call function.
57   llvm::Constant *DeletedVirtualFn = nullptr;
58 
59   /// emitThunk - Emit a single thunk.
60   void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable);
61 
62   /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by
63   /// the ABI.
64   void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk);
65 
66   void addVTableComponent(ConstantArrayBuilder &builder,
67                           const VTableLayout &layout, unsigned idx,
68                           llvm::Constant *rtti,
69                           unsigned &nextVTableThunkIndex);
70 
71 public:
72   /// Add vtable components for the given vtable layout to the given
73   /// global initializer.
74   void createVTableInitializer(ConstantArrayBuilder &builder,
75                                const VTableLayout &layout,
76                                llvm::Constant *rtti);
77 
78   CodeGenVTables(CodeGenModule &CGM);
79 
80   ItaniumVTableContext &getItaniumVTableContext() {
81     return *cast<ItaniumVTableContext>(VTContext);
82   }
83 
84   MicrosoftVTableContext &getMicrosoftVTableContext() {
85     return *cast<MicrosoftVTableContext>(VTContext);
86   }
87 
88   /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
89   /// given record decl.
90   uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
91 
92   /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
93   /// virtual pointer for the given subobject is located.
94   uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
95                                            BaseSubobject Base);
96 
97   /// GenerateConstructionVTable - Generate a construction vtable for the given
98   /// base subobject.
99   llvm::GlobalVariable *
100   GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
101                              bool BaseIsVirtual,
102                              llvm::GlobalVariable::LinkageTypes Linkage,
103                              VTableAddressPointsMapTy& AddressPoints);
104 
105 
106   /// GetAddrOfVTT - Get the address of the VTT for the given record decl.
107   llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
108 
109   /// EmitVTTDefinition - Emit the definition of the given vtable.
110   void EmitVTTDefinition(llvm::GlobalVariable *VTT,
111                          llvm::GlobalVariable::LinkageTypes Linkage,
112                          const CXXRecordDecl *RD);
113 
114   /// EmitThunks - Emit the associated thunks for the given global decl.
115   void EmitThunks(GlobalDecl GD);
116 
117   /// GenerateClassData - Generate all the class data required to be
118   /// generated upon definition of a KeyFunction.  This includes the
119   /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
120   /// (if the class has virtual bases).
121   void GenerateClassData(const CXXRecordDecl *RD);
122 
123   bool isVTableExternal(const CXXRecordDecl *RD);
124 };
125 
126 } // end namespace CodeGen
127 } // end namespace clang
128 #endif
129