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 CLANG_CODEGEN_CGVTABLE_H
15 #define CLANG_CODEGEN_CGVTABLE_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 
31 class CodeGenVTables {
32   CodeGenModule &CGM;
33 
34   VTableContextBase *VTContext;
35 
36   /// VTableAddressPointsMapTy - Address points for a single vtable.
37   typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
38 
39   typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
40   typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
41 
42   /// SubVTTIndicies - Contains indices into the various sub-VTTs.
43   SubVTTIndiciesMapTy SubVTTIndicies;
44 
45   typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
46     SecondaryVirtualPointerIndicesMapTy;
47 
48   /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
49   /// indices.
50   SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
51 
52   /// emitThunk - Emit a single thunk.
53   void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable);
54 
55   /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by
56   /// the ABI.
57   void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk);
58 
59 public:
60   /// CreateVTableInitializer - Create a vtable initializer for the given record
61   /// decl.
62   /// \param Components - The vtable components; this is really an array of
63   /// VTableComponents.
64   llvm::Constant *CreateVTableInitializer(const CXXRecordDecl *RD,
65                                           const VTableComponent *Components,
66                                           unsigned NumComponents,
67                                 const VTableLayout::VTableThunkTy *VTableThunks,
68                                           unsigned NumVTableThunks);
69 
70   CodeGenVTables(CodeGenModule &CGM);
71 
72   ItaniumVTableContext &getItaniumVTableContext() {
73     return *cast<ItaniumVTableContext>(VTContext);
74   }
75 
76   MicrosoftVTableContext &getMicrosoftVTableContext() {
77     return *cast<MicrosoftVTableContext>(VTContext);
78   }
79 
80   /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
81   /// given record decl.
82   uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
83 
84   /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
85   /// virtual pointer for the given subobject is located.
86   uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
87                                            BaseSubobject Base);
88 
89   /// getAddressPoint - Get the address point of the given subobject in the
90   /// class decl.
91   uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
92 
93   /// GenerateConstructionVTable - Generate a construction vtable for the given
94   /// base subobject.
95   llvm::GlobalVariable *
96   GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
97                              bool BaseIsVirtual,
98                              llvm::GlobalVariable::LinkageTypes Linkage,
99                              VTableAddressPointsMapTy& AddressPoints);
100 
101 
102   /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
103   llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
104 
105   /// EmitVTTDefinition - Emit the definition of the given vtable.
106   void EmitVTTDefinition(llvm::GlobalVariable *VTT,
107                          llvm::GlobalVariable::LinkageTypes Linkage,
108                          const CXXRecordDecl *RD);
109 
110   /// EmitThunks - Emit the associated thunks for the given global decl.
111   void EmitThunks(GlobalDecl GD);
112 
113   /// GenerateClassData - Generate all the class data required to be
114   /// generated upon definition of a KeyFunction.  This includes the
115   /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
116   /// (if the class has virtual bases).
117   void GenerateClassData(const CXXRecordDecl *RD);
118 
119   bool isVTableExternal(const CXXRecordDecl *RD);
120 };
121 
122 } // end namespace CodeGen
123 } // end namespace clang
124 #endif
125