clang  3.9.0
CGVTT.cpp
Go to the documentation of this file.
1 //===--- CGVTT.cpp - Emit LLVM Code for C++ VTTs --------------------------===//
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 VTTs (vtable tables).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenModule.h"
15 #include "CGCXXABI.h"
16 #include "clang/AST/RecordLayout.h"
17 #include "clang/AST/VTTBuilder.h"
18 using namespace clang;
19 using namespace CodeGen;
20 
21 static llvm::GlobalVariable *
23  const CXXRecordDecl *MostDerivedClass,
24  const VTTVTable &VTable,
25  llvm::GlobalVariable::LinkageTypes Linkage,
26  llvm::DenseMap<BaseSubobject, uint64_t> &AddressPoints) {
27  if (VTable.getBase() == MostDerivedClass) {
28  assert(VTable.getBaseOffset().isZero() &&
29  "Most derived class vtable must have a zero offset!");
30  // This is a regular vtable.
31  return CGM.getCXXABI().getAddrOfVTable(MostDerivedClass, CharUnits());
32  }
33 
34  return CGVT.GenerateConstructionVTable(MostDerivedClass,
35  VTable.getBaseSubobject(),
36  VTable.isVirtual(),
37  Linkage,
38  AddressPoints);
39 }
40 
41 void
42 CodeGenVTables::EmitVTTDefinition(llvm::GlobalVariable *VTT,
43  llvm::GlobalVariable::LinkageTypes Linkage,
44  const CXXRecordDecl *RD) {
45  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/true);
46 
47  llvm::Type *Int8PtrTy = CGM.Int8PtrTy, *Int32Ty = CGM.Int32Ty;
48  llvm::ArrayType *ArrayType =
49  llvm::ArrayType::get(Int8PtrTy, Builder.getVTTComponents().size());
50 
52  SmallVector<VTableAddressPointsMapTy, 8> VTableAddressPoints;
53  for (const VTTVTable *i = Builder.getVTTVTables().begin(),
54  *e = Builder.getVTTVTables().end(); i != e; ++i) {
55  VTableAddressPoints.push_back(VTableAddressPointsMapTy());
56  VTables.push_back(GetAddrOfVTTVTable(*this, CGM, RD, *i, Linkage,
57  VTableAddressPoints.back()));
58  }
59 
61  for (const VTTComponent *i = Builder.getVTTComponents().begin(),
62  *e = Builder.getVTTComponents().end(); i != e; ++i) {
63  const VTTVTable &VTTVT = Builder.getVTTVTables()[i->VTableIndex];
64  llvm::GlobalVariable *VTable = VTables[i->VTableIndex];
65  uint64_t AddressPoint;
66  if (VTTVT.getBase() == RD) {
67  // Just get the address point for the regular vtable.
68  AddressPoint =
70  i->VTableBase);
71  assert(AddressPoint != 0 && "Did not find vtable address point!");
72  } else {
73  AddressPoint = VTableAddressPoints[i->VTableIndex].lookup(i->VTableBase);
74  assert(AddressPoint != 0 && "Did not find ctor vtable address point!");
75  }
76 
77  llvm::Value *Idxs[] = {
78  llvm::ConstantInt::get(Int32Ty, 0),
79  llvm::ConstantInt::get(Int32Ty, AddressPoint)
80  };
81 
82  llvm::Constant *Init = llvm::ConstantExpr::getInBoundsGetElementPtr(
83  VTable->getValueType(), VTable, Idxs);
84 
85  Init = llvm::ConstantExpr::getBitCast(Init, Int8PtrTy);
86 
87  VTTComponents.push_back(Init);
88  }
89 
90  llvm::Constant *Init = llvm::ConstantArray::get(ArrayType, VTTComponents);
91 
92  VTT->setInitializer(Init);
93 
94  // Set the correct linkage.
95  VTT->setLinkage(Linkage);
96 
97  if (CGM.supportsCOMDAT() && VTT->isWeakForLinker())
98  VTT->setComdat(CGM.getModule().getOrInsertComdat(VTT->getName()));
99 
100  // Set the right visibility.
101  CGM.setGlobalVisibility(VTT, RD);
102 }
103 
104 llvm::GlobalVariable *CodeGenVTables::GetAddrOfVTT(const CXXRecordDecl *RD) {
105  assert(RD->getNumVBases() && "Only classes with virtual bases need a VTT");
106 
107  SmallString<256> OutName;
108  llvm::raw_svector_ostream Out(OutName);
109  cast<ItaniumMangleContext>(CGM.getCXXABI().getMangleContext())
110  .mangleCXXVTT(RD, Out);
111  StringRef Name = OutName.str();
112 
113  // This will also defer the definition of the VTT.
114  (void) CGM.getCXXABI().getAddrOfVTable(RD, CharUnits());
115 
116  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
117 
118  llvm::ArrayType *ArrayType =
119  llvm::ArrayType::get(CGM.Int8PtrTy, Builder.getVTTComponents().size());
120 
121  llvm::GlobalVariable *GV =
122  CGM.CreateOrReplaceCXXRuntimeVariable(Name, ArrayType,
124  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
125  return GV;
126 }
127 
130  BaseSubobjectPairTy ClassSubobjectPair(RD, Base);
131 
132  SubVTTIndiciesMapTy::iterator I = SubVTTIndicies.find(ClassSubobjectPair);
133  if (I != SubVTTIndicies.end())
134  return I->second;
135 
136  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
137 
138  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
139  Builder.getSubVTTIndicies().begin(),
140  E = Builder.getSubVTTIndicies().end(); I != E; ++I) {
141  // Insert all indices.
142  BaseSubobjectPairTy ClassSubobjectPair(RD, I->first);
143 
144  SubVTTIndicies.insert(std::make_pair(ClassSubobjectPair, I->second));
145  }
146 
147  I = SubVTTIndicies.find(ClassSubobjectPair);
148  assert(I != SubVTTIndicies.end() && "Did not find index!");
149 
150  return I->second;
151 }
152 
153 uint64_t
157  SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
158 
159  if (I != SecondaryVirtualPointerIndices.end())
160  return I->second;
161 
162  VTTBuilder Builder(CGM.getContext(), RD, /*GenerateDefinition=*/false);
163 
164  // Insert all secondary vpointer indices.
165  for (llvm::DenseMap<BaseSubobject, uint64_t>::const_iterator I =
166  Builder.getSecondaryVirtualPointerIndices().begin(),
167  E = Builder.getSecondaryVirtualPointerIndices().end(); I != E; ++I) {
168  std::pair<const CXXRecordDecl *, BaseSubobject> Pair =
169  std::make_pair(RD, I->first);
170 
171  SecondaryVirtualPointerIndices.insert(std::make_pair(Pair, I->second));
172  }
173 
174  I = SecondaryVirtualPointerIndices.find(std::make_pair(RD, Base));
175  assert(I != SecondaryVirtualPointerIndices.end() && "Did not find index!");
176 
177  return I->second;
178 }
void EmitVTTDefinition(llvm::GlobalVariable *VTT, llvm::GlobalVariable::LinkageTypes Linkage, const CXXRecordDecl *RD)
EmitVTTDefinition - Emit the definition of the given vtable.
Definition: CGVTT.cpp:42
External linkage, which indicates that the entity can be referred to from other translation units...
Definition: Linkage.h:50
llvm::Module & getModule() const
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2456
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSubVTTIndex - Return the index of the sub-VTT for the base class of the given record decl...
Definition: CGVTT.cpp:128
uint64_t getAddressPoint(BaseSubobject Base) const
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
ItaniumVTableContext & getItaniumVTableContext()
Definition: CGVTables.h:71
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const
Set the visibility for the given LLVM GlobalValue.
detail::InMemoryDirectory::const_iterator I
CGCXXABI & getCXXABI() const
const CXXRecordDecl * getBase() const
Definition: VTTBuilder.h:40
ASTContext & getContext() const
llvm::GlobalVariable * CreateOrReplaceCXXRuntimeVariable(StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage)
Will return a global variable of the given type.
The l-value was considered opaque, so the alignment was determined from a type.
const TemplateArgument * iterator
Definition: Type.h:4233
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:104
CharUnits getBaseOffset() const
Definition: VTTBuilder.h:44
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:95
llvm::GlobalVariable * GenerateConstructionVTable(const CXXRecordDecl *RD, const BaseSubobject &Base, bool BaseIsVirtual, llvm::GlobalVariable::LinkageTypes Linkage, VTableAddressPointsMapTy &AddressPoints)
GenerateConstructionVTable - Generate a construction vtable for the given base subobject.
Definition: CGVTables.cpp:660
This class organizes the cross-function state that is used while generating LLVM code.
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
virtual llvm::GlobalVariable * getAddrOfVTable(const CXXRecordDecl *RD, CharUnits VPtrOffset)=0
Get the address of the vtable for the given record decl which should be used for the vptr at the give...
detail::InMemoryDirectory::const_iterator E
uint64_t getSecondaryVirtualPointerIndex(const CXXRecordDecl *RD, BaseSubobject Base)
getSecondaryVirtualPointerIndex - Return the index in the VTT where the virtual pointer for the given...
Definition: CGVTT.cpp:154
Class for building VTT layout information.
Definition: VTTBuilder.h:67
const VTableLayout & getVTableLayout(const CXXRecordDecl *RD)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:263
static llvm::GlobalVariable * GetAddrOfVTTVTable(CodeGenVTables &CGVT, CodeGenModule &CGM, const CXXRecordDecl *MostDerivedClass, const VTTVTable &VTable, llvm::GlobalVariable::LinkageTypes Linkage, llvm::DenseMap< BaseSubobject, uint64_t > &AddressPoints)
Definition: CGVTT.cpp:22
BoundNodesTreeBuilder *const Builder
BaseSubobject getBaseSubobject() const
Definition: VTTBuilder.h:52
bool isVirtual() const
Definition: VTTBuilder.h:48
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:733