blob: 5c2b74c9dd865932b10196eeac68e5752176dcb3 [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"
Anders Carlssona0fdd912009-11-13 17:08:56 +000018#include "GlobalDecl.h"
Anders Carlssondbd920c2009-10-11 22:13:54 +000019
Anders Carlssonb73a5be2009-11-26 02:49:32 +000020namespace llvm {
21 class Constant;
22}
23
Anders Carlssondbd920c2009-10-11 22:13:54 +000024namespace clang {
Anders Carlssondbd920c2009-10-11 22:13:54 +000025 class CXXRecordDecl;
Benjamin Kramer39411b92009-11-26 13:09:03 +000026
Anders Carlssondbd920c2009-10-11 22:13:54 +000027namespace CodeGen {
28 class CodeGenModule;
Anders Carlssona94822e2009-11-26 02:32:05 +000029
Benjamin Kramer39411b92009-11-26 13:09:03 +000030/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks.
31class ThunkAdjustment {
32public:
Anders Carlssona94822e2009-11-26 02:32:05 +000033 ThunkAdjustment(int64_t NonVirtual, int64_t Virtual)
Benjamin Kramer39411b92009-11-26 13:09:03 +000034 : NonVirtual(NonVirtual),
Anders Carlssona94822e2009-11-26 02:32:05 +000035 Virtual(Virtual) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +000036
Anders Carlssona94822e2009-11-26 02:32:05 +000037 ThunkAdjustment()
38 : NonVirtual(0), Virtual(0) { }
Benjamin Kramer39411b92009-11-26 13:09:03 +000039
Anders Carlssona94822e2009-11-26 02:32:05 +000040 // isEmpty - Return whether this thunk adjustment is empty.
Benjamin Kramer39411b92009-11-26 13:09:03 +000041 bool isEmpty() const {
Anders Carlssona94822e2009-11-26 02:32:05 +000042 return NonVirtual == 0 && Virtual == 0;
43 }
Benjamin Kramer39411b92009-11-26 13:09:03 +000044
Anders Carlssona94822e2009-11-26 02:32:05 +000045 /// NonVirtual - The non-virtual adjustment.
46 int64_t NonVirtual;
Benjamin Kramer39411b92009-11-26 13:09:03 +000047
Anders Carlssona94822e2009-11-26 02:32:05 +000048 /// Virtual - The virtual adjustment.
49 int64_t Virtual;
50};
51
Anders Carlsson7622cd32009-11-26 03:09:37 +000052/// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the
53/// return pointer for covariant thunks.
Benjamin Kramer39411b92009-11-26 13:09:03 +000054class CovariantThunkAdjustment {
55public:
Anders Carlsson7622cd32009-11-26 03:09:37 +000056 CovariantThunkAdjustment(const ThunkAdjustment &ThisAdjustment,
57 const ThunkAdjustment &ReturnAdjustment)
58 : ThisAdjustment(ThisAdjustment), ReturnAdjustment(ReturnAdjustment) { }
59
60 CovariantThunkAdjustment() { }
61
62 ThunkAdjustment ThisAdjustment;
63 ThunkAdjustment ReturnAdjustment;
64};
65
Anders Carlssondbd920c2009-10-11 22:13:54 +000066class CGVtableInfo {
67 CodeGenModule &CGM;
Benjamin Kramer39411b92009-11-26 13:09:03 +000068
Anders Carlssondbd920c2009-10-11 22:13:54 +000069 /// MethodVtableIndices - Contains the index (relative to the vtable address
70 /// point) where the function pointer for a virtual function is stored.
Anders Carlssona0fdd912009-11-13 17:08:56 +000071 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy;
Anders Carlssondbd920c2009-10-11 22:13:54 +000072 MethodVtableIndicesTy MethodVtableIndices;
Benjamin Kramer39411b92009-11-26 13:09:03 +000073
Anders Carlssondbd920c2009-10-11 22:13:54 +000074 typedef std::pair<const CXXRecordDecl *,
75 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer39411b92009-11-26 13:09:03 +000076
Anders Carlssondbd920c2009-10-11 22:13:54 +000077 /// VirtualBaseClassIndicies - Contains the index into the vtable where the
78 /// offsets for virtual bases of a class are stored.
79 typedef llvm::DenseMap<ClassPairTy, int64_t> VirtualBaseClassIndiciesTy;
80 VirtualBaseClassIndiciesTy VirtualBaseClassIndicies;
Mike Stump380dd752009-11-10 07:44:33 +000081
82 llvm::DenseMap<const CXXRecordDecl *, llvm::Constant *> Vtables;
Anders Carlssond6b07fb2009-11-27 20:47:55 +000083
84 /// NumVirtualFunctionPointers - Contains the number of virtual function
85 /// pointers in the vtable for a given record decl.
86 llvm::DenseMap<const CXXRecordDecl *, uint64_t> NumVirtualFunctionPointers;
87
88 /// getNumVirtualFunctionPointers - Return the number of virtual function
89 /// pointers in the vtable for a given record decl.
90 uint64_t getNumVirtualFunctionPointers(const CXXRecordDecl *RD);
91
92 void ComputeMethodVtableIndices(const CXXRecordDecl *RD);
93
Anders Carlsson1a5e0d72009-11-30 23:41:22 +000094 /// GenerateClassData - Generate all the class data requires to be generated
95 /// upon definition of a KeyFunction. This includes the vtable, the
96 /// rtti data structure and the VTT.
97 void GenerateClassData(const CXXRecordDecl *RD);
98
Anders Carlssondbd920c2009-10-11 22:13:54 +000099public:
Benjamin Kramer39411b92009-11-26 13:09:03 +0000100 CGVtableInfo(CodeGenModule &CGM)
Anders Carlssondbd920c2009-10-11 22:13:54 +0000101 : CGM(CGM) { }
102
103 /// getMethodVtableIndex - Return the index (relative to the vtable address
Benjamin Kramer39411b92009-11-26 13:09:03 +0000104 /// point) where the function pointer for the given virtual function is
Anders Carlssondbd920c2009-10-11 22:13:54 +0000105 /// stored.
Anders Carlssond6b07fb2009-11-27 20:47:55 +0000106 uint64_t getMethodVtableIndex(GlobalDecl GD);
Benjamin Kramer39411b92009-11-26 13:09:03 +0000107
Mike Stumpab28c132009-10-13 22:54:56 +0000108 /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable
109 /// address point) where the offset of the virtual base that contains the
110 /// given Base is stored, otherwise, if no virtual base contains the given
111 /// class, return 0. Base must be a virtual base class or an unambigious
112 /// base.
Benjamin Kramer39411b92009-11-26 13:09:03 +0000113 int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
Anders Carlssondbd920c2009-10-11 22:13:54 +0000114 const CXXRecordDecl *VBase);
Mike Stump380dd752009-11-10 07:44:33 +0000115
Mike Stump8cfcb522009-11-11 20:26:26 +0000116 llvm::Constant *getVtable(const CXXRecordDecl *RD);
117 llvm::Constant *getCtorVtable(const CXXRecordDecl *RD,
118 const CXXRecordDecl *Class, uint64_t Offset);
Anders Carlsson1a5e0d72009-11-30 23:41:22 +0000119
120
121 void MaybeEmitVtable(GlobalDecl GD);
Anders Carlssondbd920c2009-10-11 22:13:54 +0000122};
Benjamin Kramer39411b92009-11-26 13:09:03 +0000123
Anders Carlssondbd920c2009-10-11 22:13:54 +0000124}
125}
126#endif