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