blob: ac3cdee63becf9322440827a4b0cf3ccbe6ef338 [file] [log] [blame]
Anders Carlsson2bb27f52009-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 Carlssonfb4dda42009-11-13 17:08:56 +000018#include "GlobalDecl.h"
Anders Carlsson2bb27f52009-10-11 22:13:54 +000019
Anders Carlssonabe274a2009-11-26 02:49:32 +000020namespace llvm {
21 class Constant;
22}
23
Anders Carlsson2bb27f52009-10-11 22:13:54 +000024namespace clang {
Anders Carlsson2bb27f52009-10-11 22:13:54 +000025 class CXXRecordDecl;
Benjamin Kramer334af992009-11-26 13:09:03 +000026
Anders Carlsson2bb27f52009-10-11 22:13:54 +000027namespace CodeGen {
28 class CodeGenModule;
Anders Carlssonc7785402009-11-26 02:32:05 +000029
Benjamin Kramer334af992009-11-26 13:09:03 +000030/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks.
31class ThunkAdjustment {
32public:
Anders Carlssonc7785402009-11-26 02:32:05 +000033 ThunkAdjustment(int64_t NonVirtual, int64_t Virtual)
Benjamin Kramer334af992009-11-26 13:09:03 +000034 : NonVirtual(NonVirtual),
Anders Carlssonc7785402009-11-26 02:32:05 +000035 Virtual(Virtual) { }
Benjamin Kramer334af992009-11-26 13:09:03 +000036
Anders Carlssonc7785402009-11-26 02:32:05 +000037 ThunkAdjustment()
38 : NonVirtual(0), Virtual(0) { }
Benjamin Kramer334af992009-11-26 13:09:03 +000039
Anders Carlssonc7785402009-11-26 02:32:05 +000040 // isEmpty - Return whether this thunk adjustment is empty.
Benjamin Kramer334af992009-11-26 13:09:03 +000041 bool isEmpty() const {
Anders Carlssonc7785402009-11-26 02:32:05 +000042 return NonVirtual == 0 && Virtual == 0;
43 }
Benjamin Kramer334af992009-11-26 13:09:03 +000044
Anders Carlssonc7785402009-11-26 02:32:05 +000045 /// NonVirtual - The non-virtual adjustment.
46 int64_t NonVirtual;
Benjamin Kramer334af992009-11-26 13:09:03 +000047
Anders Carlssonc7785402009-11-26 02:32:05 +000048 /// Virtual - The virtual adjustment.
49 int64_t Virtual;
50};
51
Anders Carlsson2f87c4f2009-11-26 03:09:37 +000052/// CovariantThunkAdjustment - Adjustment of the 'this' pointer and the
53/// return pointer for covariant thunks.
Benjamin Kramer334af992009-11-26 13:09:03 +000054class CovariantThunkAdjustment {
55public:
Anders Carlsson2f87c4f2009-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 Carlsson2bb27f52009-10-11 22:13:54 +000066class CGVtableInfo {
67 CodeGenModule &CGM;
Benjamin Kramer334af992009-11-26 13:09:03 +000068
Anders Carlsson2bb27f52009-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 Carlssonfb4dda42009-11-13 17:08:56 +000071 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000072 MethodVtableIndicesTy MethodVtableIndices;
Benjamin Kramer334af992009-11-26 13:09:03 +000073
Anders Carlsson2bb27f52009-10-11 22:13:54 +000074 typedef std::pair<const CXXRecordDecl *,
75 const CXXRecordDecl *> ClassPairTy;
Benjamin Kramer334af992009-11-26 13:09:03 +000076
Anders Carlsson2bb27f52009-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 Stumpd846d082009-11-10 07:44:33 +000081
82 llvm::DenseMap<const CXXRecordDecl *, llvm::Constant *> Vtables;
Anders Carlssonf942ee02009-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 Carlsson2bb27f52009-10-11 22:13:54 +000094public:
Benjamin Kramer334af992009-11-26 13:09:03 +000095 CGVtableInfo(CodeGenModule &CGM)
Anders Carlsson2bb27f52009-10-11 22:13:54 +000096 : CGM(CGM) { }
97
98 /// getMethodVtableIndex - Return the index (relative to the vtable address
Benjamin Kramer334af992009-11-26 13:09:03 +000099 /// point) where the function pointer for the given virtual function is
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000100 /// stored.
Anders Carlssonf942ee02009-11-27 20:47:55 +0000101 uint64_t getMethodVtableIndex(GlobalDecl GD);
Benjamin Kramer334af992009-11-26 13:09:03 +0000102
Mike Stump28431212009-10-13 22:54:56 +0000103 /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable
104 /// address point) where the offset of the virtual base that contains the
105 /// given Base is stored, otherwise, if no virtual base contains the given
106 /// class, return 0. Base must be a virtual base class or an unambigious
107 /// base.
Benjamin Kramer334af992009-11-26 13:09:03 +0000108 int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000109 const CXXRecordDecl *VBase);
Mike Stumpd846d082009-11-10 07:44:33 +0000110
Mike Stumpeac45592009-11-11 20:26:26 +0000111 llvm::Constant *getVtable(const CXXRecordDecl *RD);
112 llvm::Constant *getCtorVtable(const CXXRecordDecl *RD,
113 const CXXRecordDecl *Class, uint64_t Offset);
Mike Stump1a139f82009-11-19 01:08:19 +0000114 /// GenerateClassData - Generate all the class data requires to be generated
115 /// upon definition of a KeyFunction. This includes the vtable, the
116 /// rtti data structure and the VTT.
117 void GenerateClassData(const CXXRecordDecl *RD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000118};
Benjamin Kramer334af992009-11-26 13:09:03 +0000119
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000120}
121}
122#endif