blob: 9a25ce3033647ba73295e3897da5556ede1ab4be [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 {
25 class CXXMethodDecl;
26 class CXXRecordDecl;
27
28namespace CodeGen {
29 class CodeGenModule;
Anders Carlssonc7785402009-11-26 02:32:05 +000030
31/// ThunkAdjustment - Virtual and non-virtual adjustment for thunks.
32struct ThunkAdjustment {
33 ThunkAdjustment(int64_t NonVirtual, int64_t Virtual)
34 : NonVirtual(NonVirtual),
35 Virtual(Virtual) { }
Anders Carlsson2bb27f52009-10-11 22:13:54 +000036
Anders Carlssonc7785402009-11-26 02:32:05 +000037 ThunkAdjustment()
38 : NonVirtual(0), Virtual(0) { }
39
40 // isEmpty - Return whether this thunk adjustment is empty.
41 bool isEmpty() const {
42 return NonVirtual == 0 && Virtual == 0;
43 }
44
45 /// NonVirtual - The non-virtual adjustment.
46 int64_t NonVirtual;
47
48 /// 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.
54struct CovariantThunkAdjustment {
55 CovariantThunkAdjustment(const ThunkAdjustment &ThisAdjustment,
56 const ThunkAdjustment &ReturnAdjustment)
57 : ThisAdjustment(ThisAdjustment), ReturnAdjustment(ReturnAdjustment) { }
58
59 CovariantThunkAdjustment() { }
60
61 ThunkAdjustment ThisAdjustment;
62 ThunkAdjustment ReturnAdjustment;
63};
64
Anders Carlsson2bb27f52009-10-11 22:13:54 +000065class CGVtableInfo {
66 CodeGenModule &CGM;
67
68 /// MethodVtableIndices - Contains the index (relative to the vtable address
69 /// point) where the function pointer for a virtual function is stored.
Anders Carlssonfb4dda42009-11-13 17:08:56 +000070 typedef llvm::DenseMap<GlobalDecl, int64_t> MethodVtableIndicesTy;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000071 MethodVtableIndicesTy MethodVtableIndices;
72
73 typedef std::pair<const CXXRecordDecl *,
74 const CXXRecordDecl *> ClassPairTy;
75
76 /// VirtualBaseClassIndicies - Contains the index into the vtable where the
77 /// offsets for virtual bases of a class are stored.
78 typedef llvm::DenseMap<ClassPairTy, int64_t> VirtualBaseClassIndiciesTy;
79 VirtualBaseClassIndiciesTy VirtualBaseClassIndicies;
Mike Stumpd846d082009-11-10 07:44:33 +000080
81 llvm::DenseMap<const CXXRecordDecl *, llvm::Constant *> Vtables;
Anders Carlsson2bb27f52009-10-11 22:13:54 +000082public:
83 CGVtableInfo(CodeGenModule &CGM)
84 : CGM(CGM) { }
85
86 /// getMethodVtableIndex - Return the index (relative to the vtable address
87 /// point) where the function pointer for the given virtual function is
88 /// stored.
Anders Carlssonfb4dda42009-11-13 17:08:56 +000089 int64_t getMethodVtableIndex(GlobalDecl GD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +000090
Mike Stump28431212009-10-13 22:54:56 +000091 /// getVirtualBaseOffsetIndex - Return the index (relative to the vtable
92 /// address point) where the offset of the virtual base that contains the
93 /// given Base is stored, otherwise, if no virtual base contains the given
94 /// class, return 0. Base must be a virtual base class or an unambigious
95 /// base.
Anders Carlsson2bb27f52009-10-11 22:13:54 +000096 int64_t getVirtualBaseOffsetIndex(const CXXRecordDecl *RD,
97 const CXXRecordDecl *VBase);
Mike Stumpd846d082009-11-10 07:44:33 +000098
Mike Stumpeac45592009-11-11 20:26:26 +000099 llvm::Constant *getVtable(const CXXRecordDecl *RD);
100 llvm::Constant *getCtorVtable(const CXXRecordDecl *RD,
101 const CXXRecordDecl *Class, uint64_t Offset);
Mike Stump1a139f82009-11-19 01:08:19 +0000102 /// GenerateClassData - Generate all the class data requires to be generated
103 /// upon definition of a KeyFunction. This includes the vtable, the
104 /// rtti data structure and the VTT.
105 void GenerateClassData(const CXXRecordDecl *RD);
Anders Carlsson2bb27f52009-10-11 22:13:54 +0000106};
107
108}
109}
110#endif