blob: 1507725f0fbd15f64f8c48fc171d9c4c73202122 [file] [log] [blame]
Anders Carlssondbd920c2009-10-11 22:13:54 +00001//===--- CGVtable.h - Emit LLVM Code for C++ vtables ----------------------===//
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 "llvm/ADT/DenseMap.h"
Eli Friedman72649ed2009-12-06 22:01:30 +000018#include "llvm/ADT/DenseSet.h"
Anders Carlsson35272252009-12-06 00:23:49 +000019#include "llvm/GlobalVariable.h"
Anders Carlssona0fdd912009-11-13 17:08:56 +000020#include "GlobalDecl.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000021
22namespace clang {
Anders Carlssondbd920c2009-10-11 22:13:54 +000023 class CXXRecordDecl;
Benjamin Kramer39411b92009-11-26 13:09:03 +000024
Anders Carlssondbd920c2009-10-11 22:13:54 +000025namespace CodeGen {
26 class CodeGenModule;
Anders Carlssona94822e2009-11-26 02:32:05 +000027
Benjamin Kramer39411b92009-11-26 13:09:03 +000028/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks.
29class ThunkAdjustment {
30public:
Anders Carlssona94822e2009-11-26 02:32:05 +000031 ThunkAdjustment(int64_t NonVirtual, int64_t Virtual)
Benjamin Kramer39411b92009-11-26 13:09:03 +000032 : NonVirtual(NonVirtual),
Anders Carlssona94822e2009-11-26 02:32:05 +000033 Virtual(Virtual) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +000034
Anders Carlssona94822e2009-11-26 02:32:05 +000035 ThunkAdjustment()
36 : NonVirtual(0), Virtual(0) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +000037
Anders Carlssona94822e2009-11-26 02:32:05 +000038 // isEmpty - Return whether this thunk adjustment is empty.
Benjamin Kramer39411b92009-11-26 13:09:03 +000039 bool isEmpty() const {
Anders Carlssona94822e2009-11-26 02:32:05 +000040 return NonVirtual == 0 && Virtual == 0;
41 }
Benjamin Kramer39411b92009-11-26 13:09:03 +000042
Anders Carlssona94822e2009-11-26 02:32:05 +000043 /// NonVirtual - The non-virtual adjustment.
44 int64_t NonVirtual;
Benjamin Kramer39411b92009-11-26 13:09:03 +000045
Anders Carlssona94822e2009-11-26 02:32:05 +000046 /// Virtual - The virtual adjustment.
47 int64_t Virtual;
48};
49
Anders Carlsson7622cd32009-11-26 03:09:37 +000050/// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the
51/// return pointer for covariant thunks.
Benjamin Kramer39411b92009-11-26 13:09:03 +000052class CovariantThunkAdjustment {
53public:
Anders Carlsson7622cd32009-11-26 03:09:37 +000054 CovariantThunkAdjustment(const ThunkAdjustment &ThisAdjustment,
55 const ThunkAdjustment &ReturnAdjustment)
56 : ThisAdjustment(ThisAdjustment), ReturnAdjustment(ReturnAdjustment) { }
57
58 CovariantThunkAdjustment() { }
59
60 ThunkAdjustment ThisAdjustment;
61 ThunkAdjustment ReturnAdjustment;
62};
63
Anders Carlssondbd920c2009-10-11 22:13:54 +000064class CGVtableInfo {
65 CodeGenModule &CGM;
Benjamin Kramer39411b92009-11-26 13:09:03 +000066
Anders Carlssondbd920c2009-10-11 22:13:54 +000067 /// MethodVtableIndices - Contains the index (relative to the vtable address
68 /// point) where the function pointer for a virtual function is stored.
Anders Carlssona0fdd912009-11-13 17:08:56 +000069 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy;
Anders Carlssondbd920c2009-10-11 22:13:54 +000070 MethodVtableIndicesTy MethodVtableIndices;
Benjamin Kramer39411b92009-11-26 13:09:03 +000071
Anders Carlssondbd920c2009-10-11 22:13:54 +000072 typedef std::pair<const CXXRecordDecl *,
73 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer39411b92009-11-26 13:09:03 +000074
Anders Carlssondbd920c2009-10-11 22:13:54 +000075 /// VirtualBaseClassIndicies - Contains the index into the vtable where the
76 /// offsets for virtual bases of a class are stored.
77 typedef llvm::DenseMap<ClassPairTy, int64_t> VirtualBaseClassIndiciesTy;
78 VirtualBaseClassIndiciesTy VirtualBaseClassIndicies;
Mike Stump380dd752009-11-10 07:44:33 +000079
Anders Carlsson8c2d36f2009-12-06 00:01:05 +000080 /// Vtables - All the vtables which have been defined.
81 llvm::DenseMap<const CXXRecordDecl *, llvm::GlobalVariable *> Vtables;
Anders Carlssond6b07fb2009-11-27 20:47:55 +000082
83 /// NumVirtualFunctionPointers - Contains the number of virtual function
84 /// pointers in the vtable for a given record decl.
85 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
86
Eli Friedman72649ed2009-12-06 22:01:30 +000087 typedef llvm::DenseMap<std::pair<GlobalDecl, GlobalDecl>,
88 ThunkAdjustment> SavedThisAdjustmentsTy;
89 SavedThisAdjustmentsTy SavedThisAdjustments;
90 llvm::DenseSet<const CXXRecordDecl*> SavedThisAdjustmentRecords;
91
Anders Carlssond6b07fb2009-11-27 20:47:55 +000092 /// getNumVirtualFunctionPointers - Return the number of virtual function
93 /// pointers in the vtable for a given record decl.
94 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
95
96 void ComputeMethodVtableIndices(const CXXRecordDecl *RD);
97
Anders Carlsson1a5e0d72009-11-30 23:41:22 +000098 /// GenerateClassData - Generate all the class data requires to be generated
99 /// upon definition of a KeyFunction. This includes the vtable, the
100 /// rtti data structure and the VTT.
Anders Carlsson5794c972009-12-06 00:53:22 +0000101 ///
102 /// \param Linkage - The desired linkage of the vtable, the RTTI and the VTT.
103 void GenerateClassData(llvm::GlobalVariable::LinkageTypes Linkage,
104 const CXXRecordDecl *RD);
Anders Carlsson8c2d36f2009-12-06 00:01:05 +0000105
Anders Carlsson35272252009-12-06 00:23:49 +0000106 llvm::GlobalVariable *
Anders Carlsson35272252009-12-06 00:23:49 +0000107 GenerateVtable(llvm::GlobalVariable::LinkageTypes Linkage,
Anders Carlsson5794c972009-12-06 00:53:22 +0000108 bool GenerateDefinition, const CXXRecordDecl *LayoutClass,
109 const CXXRecordDecl *RD, uint64_t Offset);
Anders Carlssonc3a46ef2009-12-06 01:09:21 +0000110
111 llvm::GlobalVariable *GenerateVTT(llvm::GlobalVariable::LinkageTypes Linkage,
112 const CXXRecordDecl *RD);
113
Anders Carlssondbd920c2009-10-11 22:13:54 +0000114public:
Benjamin Kramer39411b92009-11-26 13:09:03 +0000115 CGVtableInfo(CodeGenModule &CGM)
Anders Carlssondbd920c2009-10-11 22:13:54 +0000116 : CGM(CGM) { }
117
118 /// getMethodVtableIndex - Return the index (relative to the vtable address
Benjamin Kramer39411b92009-11-26 13:09:03 +0000119 /// point) where the function pointer for the given virtual function is
Anders Carlssondbd920c2009-10-11 22:13:54 +0000120 /// stored.
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000121 uint64_t getMethodVtableIndex(GlobalDecl GD);
Benjamin Kramer39411b92009-11-26 13:09:03 +0000122
Mike Stumpab28c132009-10-13 22:54:56 +0000123 /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable
124 /// address point) where the offset of the virtual base that contains the
125 /// given Base is stored, otherwise, if no virtual base contains the given
126 /// class, return 0. Base must be a virtual base class or an unambigious
127 /// base.
Benjamin Kramer39411b92009-11-26 13:09:03 +0000128 int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
Anders Carlssondbd920c2009-10-11 22:13:54 +0000129 const CXXRecordDecl *VBase);
Mike Stump380dd752009-11-10 07:44:33 +0000130
Eli Friedman72649ed2009-12-06 22:01:30 +0000131 ThunkAdjustment getThisAdjustment(GlobalDecl GD, GlobalDecl OGD);
132
Anders Carlsson9ac95b92009-12-05 21:03:56 +0000133 /// getVtableAddressPoint - returns the address point of the vtable for the
134 /// given record decl.
135 /// FIXME: This should return a list of address points.
136 uint64_t getVtableAddressPoint(const CXXRecordDecl *RD);
137
Anders Carlsson8c2d36f2009-12-06 00:01:05 +0000138 llvm::GlobalVariable *getVtable(const CXXRecordDecl *RD);
139 llvm::GlobalVariable *getCtorVtable(const CXXRecordDecl *RD,
140 const CXXRecordDecl *Class,
141 uint64_t Offset);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +0000142
143
144 void MaybeEmitVtable(GlobalDecl GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000145};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000146
Anders Carlssondbd920c2009-10-11 22:13:54 +0000147}
148}
149#endif