blob: e17ad89c5e7dbd8b11e1a7faf4baf5a9ff81a05e [file] [log] [blame]
John McCall358d0562011-03-27 09:00:25 +00001//===--- CGVTables.h - Emit LLVM Code for C++ vtables -----------*- C++ -*-===//
Anders Carlsson2bb27f52009-10-11 22:13:54 +00002//
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
Peter Collingbourne66c3a832011-09-26 01:56:16 +000017#include "clang/AST/BaseSubobject.h"
Ken Dyck16ffcac2011-03-24 01:21:01 +000018#include "clang/AST/CharUnits.h"
Peter Collingbourneee781d52011-06-14 04:02:39 +000019#include "clang/AST/GlobalDecl.h"
Peter Collingbournecfd23562011-09-26 01:57:12 +000020#include "clang/AST/VTableBuilder.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Basic/ABI.h"
22#include "llvm/ADT/DenseMap.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000023#include "llvm/IR/GlobalVariable.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000024
25namespace clang {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000026 class CXXRecordDecl;
Benjamin Kramer334af992009-11-26 13:09:03 +000027
Anders Carlsson2bb27f52009-10-11 22:13:54 +000028namespace CodeGen {
29 class CodeGenModule;
Anders Carlssonc7785402009-11-26 02:32:05 +000030
Peter Collingbournea8341662011-09-26 01:56:30 +000031class CodeGenVTables {
32 CodeGenModule &CGM;
33
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +000034 // FIXME: Consider moving VTContext and VFTContext into respective CXXABI
35 // classes?
Peter Collingbournea8341662011-09-26 01:56:30 +000036 VTableContext VTContext;
Timur Iskhodzhanovdf7e7fb2013-07-30 09:46:19 +000037 OwningPtr<MicrosoftVFTableContext> VFTContext;
Anders Carlssonf942ee02009-11-27 20:47:55 +000038
Anders Carlssona208b392010-03-26 03:56:54 +000039 /// VTableAddressPointsMapTy - Address points for a single vtable.
40 typedef llvm::DenseMap<BaseSubobject, uint64_t> VTableAddressPointsMapTy;
41
Peter Collingbourne5ee9ee42011-09-26 01:56:41 +000042 typedef std::pair<const CXXRecordDecl *, BaseSubobject> BaseSubobjectPairTy;
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +000043 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t> SubVTTIndiciesMapTy;
Anders Carlssonf1a994c2010-03-26 04:23:58 +000044
45 /// SubVTTIndicies - Contains indices into the various sub-VTTs.
46 SubVTTIndiciesMapTy SubVTTIndicies;
47
Anders Carlsson8bdbb5b2010-05-03 00:55:11 +000048 typedef llvm::DenseMap<BaseSubobjectPairTy, uint64_t>
Anders Carlssonf1a994c2010-03-26 04:23:58 +000049 SecondaryVirtualPointerIndicesMapTy;
50
51 /// SecondaryVirtualPointerIndices - Contains the secondary virtual pointer
52 /// indices.
53 SecondaryVirtualPointerIndicesMapTy SecondaryVirtualPointerIndices;
Anders Carlssone36a6b32010-01-02 01:01:18 +000054
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +000055 /// emitThunk - Emit a single thunk.
56 void emitThunk(GlobalDecl GD, const ThunkInfo &Thunk, bool ForVTable);
Anders Carlsson8b021832011-02-06 18:31:40 +000057
Timur Iskhodzhanovad9d3b82013-10-09 09:23:58 +000058 /// maybeEmitThunkForVTable - Emit the given thunk for the vtable if needed by
59 /// the ABI.
60 void maybeEmitThunkForVTable(GlobalDecl GD, const ThunkInfo &Thunk);
Anders Carlsson8b021832011-02-06 18:31:40 +000061
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +000062public:
Anders Carlssona4147142010-03-25 15:26:28 +000063 /// 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,
Peter Collingbourneaffe1112011-09-26 01:56:50 +000068 const VTableComponent *Components,
Anders Carlssona4147142010-03-25 15:26:28 +000069 unsigned NumComponents,
Peter Collingbourneaffe1112011-09-26 01:56:50 +000070 const VTableLayout::VTableThunkTy *VTableThunks,
71 unsigned NumVTableThunks);
Anders Carlssona208b392010-03-26 03:56:54 +000072
Peter Collingbournea8341662011-09-26 01:56:30 +000073 CodeGenVTables(CodeGenModule &CGM);
74
75 VTableContext &getVTableContext() { return VTContext; }
Anders Carlsson2bb27f52009-10-11 22:13:54 +000076
Timur Iskhodzhanov88fd4392013-08-21 06:25:03 +000077 MicrosoftVFTableContext &getVFTableContext() { return *VFTContext.get(); }
78
Anders Carlssone36a6b32010-01-02 01:01:18 +000079 /// getSubVTTIndex - Return the index of the sub-VTT for the base class of the
80 /// given record decl.
Anders Carlsson859b3062010-05-02 23:53:25 +000081 uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base);
Anders Carlssone36a6b32010-01-02 01:01:18 +000082
Anders Carlssonf1a994c2010-03-26 04:23:58 +000083 /// getSecondaryVirtualPointerIndex - Return the index in the VTT where the
84 /// virtual pointer for the given subobject is located.
85 uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD,
86 BaseSubobject Base);
87
Anders Carlssonf6f24c62010-03-29 02:08:26 +000088 /// getAddressPoint - Get the address point of the given subobject in the
89 /// class decl.
90 uint64_t getAddressPoint(BaseSubobject Base, const CXXRecordDecl *RD);
91
Anders Carlsson0534b022010-03-25 00:35:49 +000092 /// GenerateConstructionVTable - Generate a construction vtable for the given
93 /// base subobject.
94 llvm::GlobalVariable *
95 GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base,
96 bool BaseIsVirtual,
John McCall358d0562011-03-27 09:00:25 +000097 llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlssona208b392010-03-26 03:56:54 +000098 VTableAddressPointsMapTy& AddressPoints);
Anders Carlsson883fc722011-01-29 19:16:51 +000099
100
101 /// GetAddrOfVTable - Get the address of the VTT for the given record decl.
102 llvm::GlobalVariable *GetAddrOfVTT(const CXXRecordDecl *RD);
103
104 /// EmitVTTDefinition - Emit the definition of the given vtable.
105 void EmitVTTDefinition(llvm::GlobalVariable *VTT,
106 llvm::GlobalVariable::LinkageTypes Linkage,
107 const CXXRecordDecl *RD);
Rafael Espindolae7113ca2010-03-10 02:19:29 +0000108
Douglas Gregor88d292c2010-05-13 16:44:06 +0000109 /// EmitThunks - Emit the associated thunks for the given global decl.
110 void EmitThunks(GlobalDecl GD);
111
John McCall6bd2a892013-01-25 22:31:03 +0000112 /// GenerateClassData - Generate all the class data required to be
113 /// generated upon definition of a KeyFunction. This includes the
114 /// vtable, the RTTI data structure (if RTTI is enabled) and the VTT
115 /// (if the class has virtual bases).
116 void GenerateClassData(const CXXRecordDecl *RD);
117
118 bool isVTableExternal(const CXXRecordDecl *RD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000119};
Benjamin Kramer334af992009-11-26 13:09:03 +0000120
Anders Carlsson5f9a8812010-01-14 02:29:07 +0000121} // end namespace CodeGen
122} // end namespace clang
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000123#endif