blob: 1ec700204aba57818d4effadc60fc79c93a93ba8 [file] [log] [blame]
Hans Wennborgdcfba332015-10-06 23:40:43 +00001//===--- CGClass.cpp - Emit LLVM Code for C++ classes -----------*- C++ -*-===//
Anders Carlsson9a57c5a2009-09-12 04:27:24 +00002//
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 classes
11//
12//===----------------------------------------------------------------------===//
13
Eli Friedman2495ab02012-02-25 02:48:22 +000014#include "CGBlocks.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000015#include "CGCXXABI.h"
Devang Pateld76c1db2010-08-11 21:04:37 +000016#include "CGDebugInfo.h"
Lang Hamesbf122742013-02-17 07:22:09 +000017#include "CGRecordLayout.h"
Anders Carlsson9a57c5a2009-09-12 04:27:24 +000018#include "CodeGenFunction.h"
Anders Carlssonc6d171e2009-10-06 22:43:30 +000019#include "clang/AST/CXXInheritance.h"
Faisal Vali571df122013-09-29 08:45:24 +000020#include "clang/AST/DeclTemplate.h"
John McCall769250e2010-09-17 02:31:44 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson9a57c5a2009-09-12 04:27:24 +000022#include "clang/AST/RecordLayout.h"
John McCallb81884d2010-02-19 09:25:03 +000023#include "clang/AST/StmtCXX.h"
Lang Hamesbf122742013-02-17 07:22:09 +000024#include "clang/Basic/TargetBuiltins.h"
Mark Laceya8e7df32013-10-30 21:53:58 +000025#include "clang/CodeGen/CGFunctionInfo.h"
Devang Patelb6ed3692011-02-22 20:55:26 +000026#include "clang/Frontend/CodeGenOptions.h"
Peter Collingbournea4ccff32015-02-20 20:30:56 +000027#include "llvm/IR/Intrinsics.h"
Piotr Padlewski4b1ac722015-09-15 21:46:55 +000028#include "llvm/IR/Metadata.h"
Peter Collingbournedc134532016-01-16 00:31:22 +000029#include "llvm/Transforms/Utils/SanitizerStats.h"
Anders Carlssonc6d171e2009-10-06 22:43:30 +000030
Anders Carlsson9a57c5a2009-09-12 04:27:24 +000031using namespace clang;
32using namespace CodeGen;
33
John McCall7f416cc2015-09-08 08:05:57 +000034/// Return the best known alignment for an unknown pointer to a
35/// particular class.
36CharUnits CodeGenModule::getClassPointerAlignment(const CXXRecordDecl *RD) {
37 if (!RD->isCompleteDefinition())
38 return CharUnits::One(); // Hopefully won't be used anywhere.
39
40 auto &layout = getContext().getASTRecordLayout(RD);
41
42 // If the class is final, then we know that the pointer points to an
43 // object of that type and can use the full alignment.
44 if (RD->hasAttr<FinalAttr>()) {
45 return layout.getAlignment();
46
47 // Otherwise, we have to assume it could be a subclass.
48 } else {
49 return layout.getNonVirtualAlignment();
50 }
51}
52
53/// Return the best known alignment for a pointer to a virtual base,
54/// given the alignment of a pointer to the derived class.
55CharUnits CodeGenModule::getVBaseAlignment(CharUnits actualDerivedAlign,
56 const CXXRecordDecl *derivedClass,
57 const CXXRecordDecl *vbaseClass) {
58 // The basic idea here is that an underaligned derived pointer might
59 // indicate an underaligned base pointer.
60
61 assert(vbaseClass->isCompleteDefinition());
62 auto &baseLayout = getContext().getASTRecordLayout(vbaseClass);
63 CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment();
64
65 return getDynamicOffsetAlignment(actualDerivedAlign, derivedClass,
66 expectedVBaseAlign);
67}
68
69CharUnits
70CodeGenModule::getDynamicOffsetAlignment(CharUnits actualBaseAlign,
71 const CXXRecordDecl *baseDecl,
72 CharUnits expectedTargetAlign) {
73 // If the base is an incomplete type (which is, alas, possible with
74 // member pointers), be pessimistic.
75 if (!baseDecl->isCompleteDefinition())
76 return std::min(actualBaseAlign, expectedTargetAlign);
77
78 auto &baseLayout = getContext().getASTRecordLayout(baseDecl);
79 CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment();
80
81 // If the class is properly aligned, assume the target offset is, too.
82 //
83 // This actually isn't necessarily the right thing to do --- if the
84 // class is a complete object, but it's only properly aligned for a
85 // base subobject, then the alignments of things relative to it are
86 // probably off as well. (Note that this requires the alignment of
87 // the target to be greater than the NV alignment of the derived
88 // class.)
89 //
90 // However, our approach to this kind of under-alignment can only
91 // ever be best effort; after all, we're never going to propagate
92 // alignments through variables or parameters. Note, in particular,
93 // that constructing a polymorphic type in an address that's less
94 // than pointer-aligned will generally trap in the constructor,
95 // unless we someday add some sort of attribute to change the
96 // assumed alignment of 'this'. So our goal here is pretty much
97 // just to allow the user to explicitly say that a pointer is
Eric Christopherd160c502016-01-29 01:35:53 +000098 // under-aligned and then safely access its fields and vtables.
John McCall7f416cc2015-09-08 08:05:57 +000099 if (actualBaseAlign >= expectedBaseAlign) {
100 return expectedTargetAlign;
101 }
102
103 // Otherwise, we might be offset by an arbitrary multiple of the
104 // actual alignment. The correct adjustment is to take the min of
105 // the two alignments.
106 return std::min(actualBaseAlign, expectedTargetAlign);
107}
108
109Address CodeGenFunction::LoadCXXThisAddress() {
110 assert(CurFuncDecl && "loading 'this' without a func declaration?");
111 assert(isa<CXXMethodDecl>(CurFuncDecl));
112
113 // Lazily compute CXXThisAlignment.
114 if (CXXThisAlignment.isZero()) {
115 // Just use the best known alignment for the parent.
116 // TODO: if we're currently emitting a complete-object ctor/dtor,
117 // we can always use the complete-object alignment.
118 auto RD = cast<CXXMethodDecl>(CurFuncDecl)->getParent();
119 CXXThisAlignment = CGM.getClassPointerAlignment(RD);
120 }
121
122 return Address(LoadCXXThis(), CXXThisAlignment);
123}
124
125/// Emit the address of a field using a member data pointer.
126///
127/// \param E Only used for emergency diagnostics
128Address
129CodeGenFunction::EmitCXXMemberDataPointerAddress(const Expr *E, Address base,
130 llvm::Value *memberPtr,
131 const MemberPointerType *memberPtrType,
132 AlignmentSource *alignSource) {
133 // Ask the ABI to compute the actual address.
134 llvm::Value *ptr =
135 CGM.getCXXABI().EmitMemberDataPointerAddress(*this, E, base,
136 memberPtr, memberPtrType);
137
138 QualType memberType = memberPtrType->getPointeeType();
139 CharUnits memberAlign = getNaturalTypeAlignment(memberType, alignSource);
140 memberAlign =
141 CGM.getDynamicOffsetAlignment(base.getAlignment(),
142 memberPtrType->getClass()->getAsCXXRecordDecl(),
143 memberAlign);
144 return Address(ptr, memberAlign);
145}
146
David Majnemerc1709d32015-06-23 07:31:11 +0000147CharUnits CodeGenModule::computeNonVirtualBaseClassOffset(
148 const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start,
149 CastExpr::path_const_iterator End) {
Ken Dycka1a4ae32011-03-22 00:53:26 +0000150 CharUnits Offset = CharUnits::Zero();
Justin Bogner1cd11f12015-05-20 15:53:59 +0000151
David Majnemerc1709d32015-06-23 07:31:11 +0000152 const ASTContext &Context = getContext();
Anders Carlssond829a022010-04-24 21:06:20 +0000153 const CXXRecordDecl *RD = DerivedClass;
Justin Bogner1cd11f12015-05-20 15:53:59 +0000154
John McCallcf142162010-08-07 06:22:56 +0000155 for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
Anders Carlssond829a022010-04-24 21:06:20 +0000156 const CXXBaseSpecifier *Base = *I;
157 assert(!Base->isVirtual() && "Should not see virtual bases here!");
158
159 // Get the layout.
160 const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000161
162 const CXXRecordDecl *BaseDecl =
Anders Carlssond829a022010-04-24 21:06:20 +0000163 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
Justin Bogner1cd11f12015-05-20 15:53:59 +0000164
Anders Carlssond829a022010-04-24 21:06:20 +0000165 // Add the offset.
Ken Dycka1a4ae32011-03-22 00:53:26 +0000166 Offset += Layout.getBaseClassOffset(BaseDecl);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000167
Anders Carlssond829a022010-04-24 21:06:20 +0000168 RD = BaseDecl;
169 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000170
Ken Dycka1a4ae32011-03-22 00:53:26 +0000171 return Offset;
Anders Carlssond829a022010-04-24 21:06:20 +0000172}
Anders Carlsson9a57c5a2009-09-12 04:27:24 +0000173
Anders Carlsson9150a2a2009-09-29 03:13:20 +0000174llvm::Constant *
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000175CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
John McCallcf142162010-08-07 06:22:56 +0000176 CastExpr::path_const_iterator PathBegin,
177 CastExpr::path_const_iterator PathEnd) {
178 assert(PathBegin != PathEnd && "Base path should not be empty!");
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000179
Justin Bogner1cd11f12015-05-20 15:53:59 +0000180 CharUnits Offset =
David Majnemerc1709d32015-06-23 07:31:11 +0000181 computeNonVirtualBaseClassOffset(ClassDecl, PathBegin, PathEnd);
Ken Dycka1a4ae32011-03-22 00:53:26 +0000182 if (Offset.isZero())
Craig Topper8a13c412014-05-21 05:09:00 +0000183 return nullptr;
184
Justin Bogner1cd11f12015-05-20 15:53:59 +0000185 llvm::Type *PtrDiffTy =
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000186 Types.ConvertType(getContext().getPointerDiffType());
Justin Bogner1cd11f12015-05-20 15:53:59 +0000187
Ken Dycka1a4ae32011-03-22 00:53:26 +0000188 return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
Anders Carlsson9150a2a2009-09-29 03:13:20 +0000189}
190
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000191/// Gets the address of a direct base class within a complete object.
John McCall6ce74722010-02-16 04:15:37 +0000192/// This should only be used for (1) non-virtual bases or (2) virtual bases
193/// when the type is known to be complete (e.g. in complete destructors).
194///
195/// The object pointed to by 'This' is assumed to be non-null.
John McCall7f416cc2015-09-08 08:05:57 +0000196Address
197CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(Address This,
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000198 const CXXRecordDecl *Derived,
199 const CXXRecordDecl *Base,
200 bool BaseIsVirtual) {
John McCall6ce74722010-02-16 04:15:37 +0000201 // 'this' must be a pointer (in some address space) to Derived.
John McCall7f416cc2015-09-08 08:05:57 +0000202 assert(This.getElementType() == ConvertType(Derived));
John McCall6ce74722010-02-16 04:15:37 +0000203
204 // Compute the offset of the virtual base.
Ken Dyck6aa767c2011-03-22 01:21:15 +0000205 CharUnits Offset;
John McCall6ce74722010-02-16 04:15:37 +0000206 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000207 if (BaseIsVirtual)
Ken Dyck6aa767c2011-03-22 01:21:15 +0000208 Offset = Layout.getVBaseClassOffset(Base);
John McCall6ce74722010-02-16 04:15:37 +0000209 else
Ken Dyck6aa767c2011-03-22 01:21:15 +0000210 Offset = Layout.getBaseClassOffset(Base);
John McCall6ce74722010-02-16 04:15:37 +0000211
212 // Shift and cast down to the base type.
213 // TODO: for complete types, this should be possible with a GEP.
John McCall7f416cc2015-09-08 08:05:57 +0000214 Address V = This;
215 if (!Offset.isZero()) {
216 V = Builder.CreateElementBitCast(V, Int8Ty);
217 V = Builder.CreateConstInBoundsByteGEP(V, Offset);
John McCall6ce74722010-02-16 04:15:37 +0000218 }
John McCall7f416cc2015-09-08 08:05:57 +0000219 V = Builder.CreateElementBitCast(V, ConvertType(Base));
John McCall6ce74722010-02-16 04:15:37 +0000220
221 return V;
Anders Carlssone87fae92010-03-28 19:40:00 +0000222}
John McCall6ce74722010-02-16 04:15:37 +0000223
John McCall7f416cc2015-09-08 08:05:57 +0000224static Address
225ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, Address addr,
John McCall13a39c62012-08-01 05:04:58 +0000226 CharUnits nonVirtualOffset,
John McCall7f416cc2015-09-08 08:05:57 +0000227 llvm::Value *virtualOffset,
228 const CXXRecordDecl *derivedClass,
229 const CXXRecordDecl *nearestVBase) {
John McCall13a39c62012-08-01 05:04:58 +0000230 // Assert that we have something to do.
Craig Topper8a13c412014-05-21 05:09:00 +0000231 assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
John McCall13a39c62012-08-01 05:04:58 +0000232
233 // Compute the offset from the static and dynamic components.
234 llvm::Value *baseOffset;
235 if (!nonVirtualOffset.isZero()) {
236 baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy,
237 nonVirtualOffset.getQuantity());
238 if (virtualOffset) {
239 baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset);
240 }
241 } else {
242 baseOffset = virtualOffset;
243 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000244
Anders Carlsson53cebd12010-04-20 16:03:35 +0000245 // Apply the base offset.
John McCall7f416cc2015-09-08 08:05:57 +0000246 llvm::Value *ptr = addr.getPointer();
John McCall13a39c62012-08-01 05:04:58 +0000247 ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
248 ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr");
John McCall7f416cc2015-09-08 08:05:57 +0000249
250 // If we have a virtual component, the alignment of the result will
251 // be relative only to the known alignment of that vbase.
252 CharUnits alignment;
253 if (virtualOffset) {
254 assert(nearestVBase && "virtual offset without vbase?");
255 alignment = CGF.CGM.getVBaseAlignment(addr.getAlignment(),
256 derivedClass, nearestVBase);
257 } else {
258 alignment = addr.getAlignment();
259 }
260 alignment = alignment.alignmentAtOffset(nonVirtualOffset);
261
262 return Address(ptr, alignment);
Anders Carlsson53cebd12010-04-20 16:03:35 +0000263}
264
John McCall7f416cc2015-09-08 08:05:57 +0000265Address CodeGenFunction::GetAddressOfBaseClass(
266 Address Value, const CXXRecordDecl *Derived,
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000267 CastExpr::path_const_iterator PathBegin,
268 CastExpr::path_const_iterator PathEnd, bool NullCheckValue,
269 SourceLocation Loc) {
John McCallcf142162010-08-07 06:22:56 +0000270 assert(PathBegin != PathEnd && "Base path should not be empty!");
Anders Carlssond829a022010-04-24 21:06:20 +0000271
John McCallcf142162010-08-07 06:22:56 +0000272 CastExpr::path_const_iterator Start = PathBegin;
Craig Topper8a13c412014-05-21 05:09:00 +0000273 const CXXRecordDecl *VBase = nullptr;
274
John McCall13a39c62012-08-01 05:04:58 +0000275 // Sema has done some convenient canonicalization here: if the
276 // access path involved any virtual steps, the conversion path will
277 // *start* with a step down to the correct virtual base subobject,
278 // and hence will not require any further steps.
Anders Carlssond829a022010-04-24 21:06:20 +0000279 if ((*Start)->isVirtual()) {
Justin Bogner1cd11f12015-05-20 15:53:59 +0000280 VBase =
Anders Carlssond829a022010-04-24 21:06:20 +0000281 cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl());
282 ++Start;
283 }
John McCall13a39c62012-08-01 05:04:58 +0000284
285 // Compute the static offset of the ultimate destination within its
286 // allocating subobject (the virtual base, if there is one, or else
287 // the "complete" object that we see).
David Majnemerc1709d32015-06-23 07:31:11 +0000288 CharUnits NonVirtualOffset = CGM.computeNonVirtualBaseClassOffset(
289 VBase ? VBase : Derived, Start, PathEnd);
Anders Carlssond829a022010-04-24 21:06:20 +0000290
John McCall13a39c62012-08-01 05:04:58 +0000291 // If there's a virtual step, we can sometimes "devirtualize" it.
292 // For now, that's limited to when the derived type is final.
293 // TODO: "devirtualize" this for accesses to known-complete objects.
294 if (VBase && Derived->hasAttr<FinalAttr>()) {
295 const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived);
296 CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase);
297 NonVirtualOffset += vBaseOffset;
Craig Topper8a13c412014-05-21 05:09:00 +0000298 VBase = nullptr; // we no longer have a virtual step
John McCall13a39c62012-08-01 05:04:58 +0000299 }
300
Anders Carlssond829a022010-04-24 21:06:20 +0000301 // Get the base pointer type.
Justin Bogner1cd11f12015-05-20 15:53:59 +0000302 llvm::Type *BasePtrTy =
John McCallcf142162010-08-07 06:22:56 +0000303 ConvertType((PathEnd[-1])->getType())->getPointerTo();
John McCall13a39c62012-08-01 05:04:58 +0000304
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000305 QualType DerivedTy = getContext().getRecordType(Derived);
John McCall7f416cc2015-09-08 08:05:57 +0000306 CharUnits DerivedAlign = CGM.getClassPointerAlignment(Derived);
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000307
John McCall13a39c62012-08-01 05:04:58 +0000308 // If the static offset is zero and we don't have a virtual step,
309 // just do a bitcast; null checks are unnecessary.
Ken Dycka1a4ae32011-03-22 00:53:26 +0000310 if (NonVirtualOffset.isZero() && !VBase) {
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000311 if (sanitizePerformTypeCheck()) {
John McCall7f416cc2015-09-08 08:05:57 +0000312 EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(),
313 DerivedTy, DerivedAlign, !NullCheckValue);
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000314 }
Anders Carlssond829a022010-04-24 21:06:20 +0000315 return Builder.CreateBitCast(Value, BasePtrTy);
Craig Topper8a13c412014-05-21 05:09:00 +0000316 }
John McCall13a39c62012-08-01 05:04:58 +0000317
Craig Topper8a13c412014-05-21 05:09:00 +0000318 llvm::BasicBlock *origBB = nullptr;
319 llvm::BasicBlock *endBB = nullptr;
320
John McCall13a39c62012-08-01 05:04:58 +0000321 // Skip over the offset (and the vtable load) if we're supposed to
322 // null-check the pointer.
Anders Carlssond829a022010-04-24 21:06:20 +0000323 if (NullCheckValue) {
John McCall13a39c62012-08-01 05:04:58 +0000324 origBB = Builder.GetInsertBlock();
325 llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull");
326 endBB = createBasicBlock("cast.end");
Justin Bogner1cd11f12015-05-20 15:53:59 +0000327
John McCall7f416cc2015-09-08 08:05:57 +0000328 llvm::Value *isNull = Builder.CreateIsNull(Value.getPointer());
John McCall13a39c62012-08-01 05:04:58 +0000329 Builder.CreateCondBr(isNull, endBB, notNullBB);
330 EmitBlock(notNullBB);
Anders Carlssond829a022010-04-24 21:06:20 +0000331 }
332
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000333 if (sanitizePerformTypeCheck()) {
John McCall7f416cc2015-09-08 08:05:57 +0000334 EmitTypeCheck(VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc,
335 Value.getPointer(), DerivedTy, DerivedAlign, true);
Alexey Samsonoveb47d8a2014-10-13 23:59:00 +0000336 }
337
John McCall13a39c62012-08-01 05:04:58 +0000338 // Compute the virtual offset.
Craig Topper8a13c412014-05-21 05:09:00 +0000339 llvm::Value *VirtualOffset = nullptr;
Anders Carlssona376b532011-01-29 03:18:56 +0000340 if (VBase) {
Reid Klecknerd8cbeec2013-05-29 18:02:47 +0000341 VirtualOffset =
342 CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase);
Anders Carlssona376b532011-01-29 03:18:56 +0000343 }
Anders Carlssond829a022010-04-24 21:06:20 +0000344
John McCall13a39c62012-08-01 05:04:58 +0000345 // Apply both offsets.
John McCall7f416cc2015-09-08 08:05:57 +0000346 Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset,
347 VirtualOffset, Derived, VBase);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000348
John McCall13a39c62012-08-01 05:04:58 +0000349 // Cast to the destination type.
Anders Carlssond829a022010-04-24 21:06:20 +0000350 Value = Builder.CreateBitCast(Value, BasePtrTy);
John McCall13a39c62012-08-01 05:04:58 +0000351
352 // Build a phi if we needed a null check.
Anders Carlssond829a022010-04-24 21:06:20 +0000353 if (NullCheckValue) {
John McCall13a39c62012-08-01 05:04:58 +0000354 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
355 Builder.CreateBr(endBB);
356 EmitBlock(endBB);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000357
John McCall13a39c62012-08-01 05:04:58 +0000358 llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result");
John McCall7f416cc2015-09-08 08:05:57 +0000359 PHI->addIncoming(Value.getPointer(), notNullBB);
John McCall13a39c62012-08-01 05:04:58 +0000360 PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB);
John McCall7f416cc2015-09-08 08:05:57 +0000361 Value = Address(PHI, Value.getAlignment());
Anders Carlssond829a022010-04-24 21:06:20 +0000362 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000363
Anders Carlssond829a022010-04-24 21:06:20 +0000364 return Value;
365}
366
John McCall7f416cc2015-09-08 08:05:57 +0000367Address
368CodeGenFunction::GetAddressOfDerivedClass(Address BaseAddr,
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000369 const CXXRecordDecl *Derived,
John McCallcf142162010-08-07 06:22:56 +0000370 CastExpr::path_const_iterator PathBegin,
371 CastExpr::path_const_iterator PathEnd,
Anders Carlsson8c793172009-11-23 17:57:54 +0000372 bool NullCheckValue) {
John McCallcf142162010-08-07 06:22:56 +0000373 assert(PathBegin != PathEnd && "Base path should not be empty!");
Anders Carlsson8a64c1c2010-04-24 21:23:59 +0000374
Anders Carlsson8c793172009-11-23 17:57:54 +0000375 QualType DerivedTy =
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000376 getContext().getCanonicalType(getContext().getTagDeclType(Derived));
Chris Lattner2192fe52011-07-18 04:24:23 +0000377 llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
Richard Smith2c5868c2013-02-13 21:18:23 +0000378
Anders Carlsson600f7372010-01-31 01:43:37 +0000379 llvm::Value *NonVirtualOffset =
John McCallcf142162010-08-07 06:22:56 +0000380 CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000381
Anders Carlsson600f7372010-01-31 01:43:37 +0000382 if (!NonVirtualOffset) {
383 // No offset, we can just cast back.
John McCall7f416cc2015-09-08 08:05:57 +0000384 return Builder.CreateBitCast(BaseAddr, DerivedPtrTy);
Anders Carlsson600f7372010-01-31 01:43:37 +0000385 }
Craig Topper8a13c412014-05-21 05:09:00 +0000386
387 llvm::BasicBlock *CastNull = nullptr;
388 llvm::BasicBlock *CastNotNull = nullptr;
389 llvm::BasicBlock *CastEnd = nullptr;
390
Anders Carlsson8c793172009-11-23 17:57:54 +0000391 if (NullCheckValue) {
392 CastNull = createBasicBlock("cast.null");
393 CastNotNull = createBasicBlock("cast.notnull");
394 CastEnd = createBasicBlock("cast.end");
Justin Bogner1cd11f12015-05-20 15:53:59 +0000395
John McCall7f416cc2015-09-08 08:05:57 +0000396 llvm::Value *IsNull = Builder.CreateIsNull(BaseAddr.getPointer());
Anders Carlsson8c793172009-11-23 17:57:54 +0000397 Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
398 EmitBlock(CastNotNull);
399 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000400
Anders Carlsson600f7372010-01-31 01:43:37 +0000401 // Apply the offset.
John McCall7f416cc2015-09-08 08:05:57 +0000402 llvm::Value *Value = Builder.CreateBitCast(BaseAddr.getPointer(), Int8PtrTy);
Eli Friedman87549262012-02-28 22:07:56 +0000403 Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset),
404 "sub.ptr");
Anders Carlsson600f7372010-01-31 01:43:37 +0000405
406 // Just cast.
407 Value = Builder.CreateBitCast(Value, DerivedPtrTy);
Anders Carlsson8c793172009-11-23 17:57:54 +0000408
John McCall7f416cc2015-09-08 08:05:57 +0000409 // Produce a PHI if we had a null-check.
Anders Carlsson8c793172009-11-23 17:57:54 +0000410 if (NullCheckValue) {
411 Builder.CreateBr(CastEnd);
412 EmitBlock(CastNull);
413 Builder.CreateBr(CastEnd);
414 EmitBlock(CastEnd);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000415
Jay Foad20c0f022011-03-30 11:28:58 +0000416 llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
Anders Carlsson8c793172009-11-23 17:57:54 +0000417 PHI->addIncoming(Value, CastNotNull);
John McCall7f416cc2015-09-08 08:05:57 +0000418 PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
Anders Carlsson8c793172009-11-23 17:57:54 +0000419 Value = PHI;
420 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000421
John McCall7f416cc2015-09-08 08:05:57 +0000422 return Address(Value, CGM.getClassPointerAlignment(Derived));
Anders Carlsson9a57c5a2009-09-12 04:27:24 +0000423}
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000424
425llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD,
426 bool ForVirtualBase,
427 bool Delegating) {
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000428 if (!CGM.getCXXABI().NeedsVTTParameter(GD)) {
Anders Carlssone36a6b32010-01-02 01:01:18 +0000429 // This constructor/destructor does not need a VTT parameter.
Craig Topper8a13c412014-05-21 05:09:00 +0000430 return nullptr;
Anders Carlssone36a6b32010-01-02 01:01:18 +0000431 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000432
John McCalldec348f72013-05-03 07:33:41 +0000433 const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent();
Anders Carlssone36a6b32010-01-02 01:01:18 +0000434 const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
John McCall5c60a6f2010-02-18 19:59:28 +0000435
Anders Carlssone36a6b32010-01-02 01:01:18 +0000436 llvm::Value *VTT;
437
John McCall5c60a6f2010-02-18 19:59:28 +0000438 uint64_t SubVTTIndex;
439
Douglas Gregor61535002013-01-31 05:50:40 +0000440 if (Delegating) {
441 // If this is a delegating constructor call, just load the VTT.
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000442 return LoadCXXVTT();
Douglas Gregor61535002013-01-31 05:50:40 +0000443 } else if (RD == Base) {
444 // If the record matches the base, this is the complete ctor/dtor
445 // variant calling the base variant in a class with virtual bases.
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000446 assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) &&
John McCall5c60a6f2010-02-18 19:59:28 +0000447 "doing no-op VTT offset in base dtor/ctor?");
Anders Carlsson4d205ba2010-05-02 23:33:10 +0000448 assert(!ForVirtualBase && "Can't have same class as virtual base!");
John McCall5c60a6f2010-02-18 19:59:28 +0000449 SubVTTIndex = 0;
450 } else {
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000451 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000452 CharUnits BaseOffset = ForVirtualBase ?
453 Layout.getVBaseClassOffset(Base) :
Ken Dyck16ffcac2011-03-24 01:21:01 +0000454 Layout.getBaseClassOffset(Base);
Anders Carlsson859b3062010-05-02 23:53:25 +0000455
Justin Bogner1cd11f12015-05-20 15:53:59 +0000456 SubVTTIndex =
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000457 CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
John McCall5c60a6f2010-02-18 19:59:28 +0000458 assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
459 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000460
Peter Collingbourne66f82e62013-06-28 20:45:28 +0000461 if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) {
Anders Carlssone36a6b32010-01-02 01:01:18 +0000462 // A VTT parameter was passed to the constructor, use it.
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000463 VTT = LoadCXXVTT();
464 VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex);
Anders Carlssone36a6b32010-01-02 01:01:18 +0000465 } else {
466 // We're the complete constructor, so get the VTT by name.
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +0000467 VTT = CGM.getVTables().GetAddrOfVTT(RD);
468 VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex);
Anders Carlssone36a6b32010-01-02 01:01:18 +0000469 }
470
471 return VTT;
472}
473
John McCall1d987562010-07-21 01:23:41 +0000474namespace {
John McCallf99a6312010-07-21 05:30:47 +0000475 /// Call the destructor for a direct base class.
David Blaikie7e70d682015-08-18 22:40:54 +0000476 struct CallBaseDtor final : EHScopeStack::Cleanup {
John McCallf99a6312010-07-21 05:30:47 +0000477 const CXXRecordDecl *BaseClass;
478 bool BaseIsVirtual;
479 CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
480 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
John McCall1d987562010-07-21 01:23:41 +0000481
Craig Topper4f12f102014-03-12 06:41:41 +0000482 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallf99a6312010-07-21 05:30:47 +0000483 const CXXRecordDecl *DerivedClass =
484 cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
485
486 const CXXDestructorDecl *D = BaseClass->getDestructor();
John McCall7f416cc2015-09-08 08:05:57 +0000487 Address Addr =
488 CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThisAddress(),
John McCallf99a6312010-07-21 05:30:47 +0000489 DerivedClass, BaseClass,
490 BaseIsVirtual);
Douglas Gregor61535002013-01-31 05:50:40 +0000491 CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual,
492 /*Delegating=*/false, Addr);
John McCall1d987562010-07-21 01:23:41 +0000493 }
494 };
John McCall769250e2010-09-17 02:31:44 +0000495
496 /// A visitor which checks whether an initializer uses 'this' in a
497 /// way which requires the vtable to be properly set.
Scott Douglass503fc392015-06-10 13:53:15 +0000498 struct DynamicThisUseChecker : ConstEvaluatedExprVisitor<DynamicThisUseChecker> {
499 typedef ConstEvaluatedExprVisitor<DynamicThisUseChecker> super;
John McCall769250e2010-09-17 02:31:44 +0000500
501 bool UsesThis;
502
Scott Douglass503fc392015-06-10 13:53:15 +0000503 DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {}
John McCall769250e2010-09-17 02:31:44 +0000504
505 // Black-list all explicit and implicit references to 'this'.
506 //
507 // Do we need to worry about external references to 'this' derived
508 // from arbitrary code? If so, then anything which runs arbitrary
509 // external code might potentially access the vtable.
Scott Douglass503fc392015-06-10 13:53:15 +0000510 void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; }
John McCall769250e2010-09-17 02:31:44 +0000511 };
Hans Wennborgdcfba332015-10-06 23:40:43 +0000512} // end anonymous namespace
John McCall769250e2010-09-17 02:31:44 +0000513
514static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
515 DynamicThisUseChecker Checker(C);
Scott Douglass503fc392015-06-10 13:53:15 +0000516 Checker.Visit(Init);
John McCall769250e2010-09-17 02:31:44 +0000517 return Checker.UsesThis;
John McCall1d987562010-07-21 01:23:41 +0000518}
519
Justin Bogner1cd11f12015-05-20 15:53:59 +0000520static void EmitBaseInitializer(CodeGenFunction &CGF,
Anders Carlssonfb404882009-12-24 22:46:43 +0000521 const CXXRecordDecl *ClassDecl,
Alexis Hunt1d792652011-01-08 20:30:50 +0000522 CXXCtorInitializer *BaseInit,
Anders Carlssonfb404882009-12-24 22:46:43 +0000523 CXXCtorType CtorType) {
524 assert(BaseInit->isBaseInitializer() &&
525 "Must have base initializer!");
526
John McCall7f416cc2015-09-08 08:05:57 +0000527 Address ThisPtr = CGF.LoadCXXThisAddress();
Justin Bogner1cd11f12015-05-20 15:53:59 +0000528
Anders Carlssonfb404882009-12-24 22:46:43 +0000529 const Type *BaseType = BaseInit->getBaseClass();
530 CXXRecordDecl *BaseClassDecl =
531 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
532
Anders Carlsson1c0f8bb2010-04-12 00:51:03 +0000533 bool isBaseVirtual = BaseInit->isBaseVirtual();
Anders Carlssonfb404882009-12-24 22:46:43 +0000534
535 // The base constructor doesn't construct virtual bases.
536 if (CtorType == Ctor_Base && isBaseVirtual)
537 return;
538
John McCall769250e2010-09-17 02:31:44 +0000539 // If the initializer for the base (other than the constructor
540 // itself) accesses 'this' in any way, we need to initialize the
541 // vtables.
542 if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
543 CGF.InitializeVTablePointers(ClassDecl);
544
John McCall6ce74722010-02-16 04:15:37 +0000545 // We can pretend to be a complete class because it only matters for
546 // virtual bases, and we only do virtual bases for complete ctors.
John McCall7f416cc2015-09-08 08:05:57 +0000547 Address V =
Anders Carlssonc4ba0cd2010-04-24 23:01:49 +0000548 CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
John McCallf99a6312010-07-21 05:30:47 +0000549 BaseClassDecl,
550 isBaseVirtual);
John McCall8d6fc952011-08-25 20:40:09 +0000551 AggValueSlot AggSlot =
John McCall7f416cc2015-09-08 08:05:57 +0000552 AggValueSlot::forAddr(V, Qualifiers(),
John McCall8d6fc952011-08-25 20:40:09 +0000553 AggValueSlot::IsDestructed,
John McCalla5efa732011-08-25 23:04:34 +0000554 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier615ed1a2012-03-29 17:37:10 +0000555 AggValueSlot::IsNotAliased);
John McCall7a626f62010-09-15 10:14:12 +0000556
557 CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000558
559 if (CGF.CGM.getLangOpts().Exceptions &&
Anders Carlsson08ce5ed2011-02-20 00:20:27 +0000560 !BaseClassDecl->hasTrivialDestructor())
John McCallcda666c2010-07-21 07:22:38 +0000561 CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
562 isBaseVirtual);
Anders Carlssonfb404882009-12-24 22:46:43 +0000563}
564
Douglas Gregor94f9a482010-05-05 05:51:00 +0000565static void EmitAggMemberInitializer(CodeGenFunction &CGF,
566 LValue LHS,
Eli Friedman6ae63022012-02-14 02:15:49 +0000567 Expr *Init,
John McCall7f416cc2015-09-08 08:05:57 +0000568 Address ArrayIndexVar,
Douglas Gregor94f9a482010-05-05 05:51:00 +0000569 QualType T,
Eli Friedman6ae63022012-02-14 02:15:49 +0000570 ArrayRef<VarDecl *> ArrayIndexes,
Douglas Gregor94f9a482010-05-05 05:51:00 +0000571 unsigned Index) {
Eli Friedman6ae63022012-02-14 02:15:49 +0000572 if (Index == ArrayIndexes.size()) {
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000573 LValue LV = LHS;
Eli Friedmanc1d85b92011-12-03 00:54:26 +0000574
John McCall7f416cc2015-09-08 08:05:57 +0000575 if (ArrayIndexVar.isValid()) {
Richard Smithcc1b96d2013-06-12 22:31:48 +0000576 // If we have an array index variable, load it and use it as an offset.
577 // Then, increment the value.
John McCall7f416cc2015-09-08 08:05:57 +0000578 llvm::Value *Dest = LHS.getPointer();
Richard Smithcc1b96d2013-06-12 22:31:48 +0000579 llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar);
580 Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress");
581 llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1);
582 Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc");
583 CGF.Builder.CreateStore(Next, ArrayIndexVar);
Sebastian Redl4e04dd12012-02-19 15:41:54 +0000584
Richard Smithcc1b96d2013-06-12 22:31:48 +0000585 // Update the LValue.
John McCall7f416cc2015-09-08 08:05:57 +0000586 CharUnits EltSize = CGF.getContext().getTypeSizeInChars(T);
587 CharUnits Align = LV.getAlignment().alignmentOfArrayElement(EltSize);
588 LV.setAddress(Address(Dest, Align));
Douglas Gregor94f9a482010-05-05 05:51:00 +0000589 }
John McCall7a626f62010-09-15 10:14:12 +0000590
Richard Smithcc1b96d2013-06-12 22:31:48 +0000591 switch (CGF.getEvaluationKind(T)) {
592 case TEK_Scalar:
Craig Topper8a13c412014-05-21 05:09:00 +0000593 CGF.EmitScalarInit(Init, /*decl*/ nullptr, LV, false);
Richard Smithcc1b96d2013-06-12 22:31:48 +0000594 break;
595 case TEK_Complex:
596 CGF.EmitComplexExprIntoLValue(Init, LV, /*isInit*/ true);
597 break;
598 case TEK_Aggregate: {
599 AggValueSlot Slot =
600 AggValueSlot::forLValue(LV,
601 AggValueSlot::IsDestructed,
602 AggValueSlot::DoesNotNeedGCBarriers,
603 AggValueSlot::IsNotAliased);
604
605 CGF.EmitAggExpr(Init, Slot);
606 break;
607 }
608 }
Sebastian Redl4e04dd12012-02-19 15:41:54 +0000609
Douglas Gregor94f9a482010-05-05 05:51:00 +0000610 return;
611 }
Richard Smithcc1b96d2013-06-12 22:31:48 +0000612
Douglas Gregor94f9a482010-05-05 05:51:00 +0000613 const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T);
614 assert(Array && "Array initialization without the array type?");
John McCall7f416cc2015-09-08 08:05:57 +0000615 Address IndexVar = CGF.GetAddrOfLocalVar(ArrayIndexes[Index]);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000616
Douglas Gregor94f9a482010-05-05 05:51:00 +0000617 // Initialize this index variable to zero.
618 llvm::Value* Zero
John McCall7f416cc2015-09-08 08:05:57 +0000619 = llvm::Constant::getNullValue(IndexVar.getElementType());
Douglas Gregor94f9a482010-05-05 05:51:00 +0000620 CGF.Builder.CreateStore(Zero, IndexVar);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000621
Douglas Gregor94f9a482010-05-05 05:51:00 +0000622 // Start the loop with a block that tests the condition.
623 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond");
624 llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end");
Justin Bogner1cd11f12015-05-20 15:53:59 +0000625
Douglas Gregor94f9a482010-05-05 05:51:00 +0000626 CGF.EmitBlock(CondBlock);
627
628 llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body");
629 // Generate: if (loop-index < number-of-elements) fall to the loop body,
630 // otherwise, go to the block after the for-loop.
631 uint64_t NumElements = Array->getSize().getZExtValue();
Douglas Gregor94f9a482010-05-05 05:51:00 +0000632 llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar);
Chris Lattner44456d22010-05-06 06:35:23 +0000633 llvm::Value *NumElementsPtr =
634 llvm::ConstantInt::get(Counter->getType(), NumElements);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000635 llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr,
636 "isless");
Justin Bogner1cd11f12015-05-20 15:53:59 +0000637
Douglas Gregor94f9a482010-05-05 05:51:00 +0000638 // If the condition is true, execute the body.
639 CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor);
640
641 CGF.EmitBlock(ForBody);
642 llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc");
Richard Smithcc1b96d2013-06-12 22:31:48 +0000643
644 // Inside the loop body recurse to emit the inner loop or, eventually, the
645 // constructor call.
646 EmitAggMemberInitializer(CGF, LHS, Init, ArrayIndexVar,
647 Array->getElementType(), ArrayIndexes, Index + 1);
648
Douglas Gregor94f9a482010-05-05 05:51:00 +0000649 CGF.EmitBlock(ContinueBlock);
650
651 // Emit the increment of the loop counter.
652 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
653 Counter = CGF.Builder.CreateLoad(IndexVar);
654 NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc");
655 CGF.Builder.CreateStore(NextVal, IndexVar);
656
657 // Finally, branch back up to the condition for the next iteration.
658 CGF.EmitBranch(CondBlock);
659
660 // Emit the fall-through block.
661 CGF.EmitBlock(AfterFor, true);
662}
John McCall1d987562010-07-21 01:23:41 +0000663
Richard Smith419bd092015-04-29 19:26:57 +0000664static bool isMemcpyEquivalentSpecialMember(const CXXMethodDecl *D) {
665 auto *CD = dyn_cast<CXXConstructorDecl>(D);
666 if (!(CD && CD->isCopyOrMoveConstructor()) &&
667 !D->isCopyAssignmentOperator() && !D->isMoveAssignmentOperator())
668 return false;
669
670 // We can emit a memcpy for a trivial copy or move constructor/assignment.
671 if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding())
672 return true;
673
674 // We *must* emit a memcpy for a defaulted union copy or move op.
675 if (D->getParent()->isUnion() && D->isDefaulted())
676 return true;
677
678 return false;
679}
680
Alexey Bataev152c71f2015-07-14 07:55:48 +0000681static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF,
682 CXXCtorInitializer *MemberInit,
683 LValue &LHS) {
684 FieldDecl *Field = MemberInit->getAnyMember();
685 if (MemberInit->isIndirectMemberInitializer()) {
686 // If we are initializing an anonymous union field, drill down to the field.
687 IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
688 for (const auto *I : IndirectField->chain())
689 LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I));
690 } else {
691 LHS = CGF.EmitLValueForFieldInitialization(LHS, Field);
692 }
693}
694
Anders Carlssonfb404882009-12-24 22:46:43 +0000695static void EmitMemberInitializer(CodeGenFunction &CGF,
696 const CXXRecordDecl *ClassDecl,
Alexis Hunt1d792652011-01-08 20:30:50 +0000697 CXXCtorInitializer *MemberInit,
Douglas Gregor94f9a482010-05-05 05:51:00 +0000698 const CXXConstructorDecl *Constructor,
699 FunctionArgList &Args) {
David Blaikiea81d4102015-01-18 00:12:58 +0000700 ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation());
Francois Pichetd583da02010-12-04 09:14:42 +0000701 assert(MemberInit->isAnyMemberInitializer() &&
Anders Carlssonfb404882009-12-24 22:46:43 +0000702 "Must have member initializer!");
Richard Smith938f40b2011-06-11 17:19:42 +0000703 assert(MemberInit->getInit() && "Must have initializer!");
Justin Bogner1cd11f12015-05-20 15:53:59 +0000704
Anders Carlssonfb404882009-12-24 22:46:43 +0000705 // non-static data member initializers.
Francois Pichetd583da02010-12-04 09:14:42 +0000706 FieldDecl *Field = MemberInit->getAnyMember();
Eli Friedman6ae63022012-02-14 02:15:49 +0000707 QualType FieldType = Field->getType();
Anders Carlssonfb404882009-12-24 22:46:43 +0000708
709 llvm::Value *ThisPtr = CGF.LoadCXXThis();
Eli Friedman7f1ff602012-04-16 03:54:45 +0000710 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
Eli Friedmanf6d21842012-08-08 03:51:37 +0000711 LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
Eli Friedman7f1ff602012-04-16 03:54:45 +0000712
Alexey Bataev152c71f2015-07-14 07:55:48 +0000713 EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS);
Anders Carlssonfb404882009-12-24 22:46:43 +0000714
Eli Friedman6ae63022012-02-14 02:15:49 +0000715 // Special case: if we are in a copy or move constructor, and we are copying
716 // an array of PODs or classes with trivial copy constructors, ignore the
717 // AST and perform the copy we know is equivalent.
718 // FIXME: This is hacky at best... if we had a bit more explicit information
719 // in the AST, we could generalize it more easily.
720 const ConstantArrayType *Array
721 = CGF.getContext().getAsConstantArrayType(FieldType);
Jordan Rose54533f72013-08-07 16:16:48 +0000722 if (Array && Constructor->isDefaulted() &&
Eli Friedman6ae63022012-02-14 02:15:49 +0000723 Constructor->isCopyOrMoveConstructor()) {
724 QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
Richard Smith993f25a2012-11-07 23:56:21 +0000725 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
Eli Friedman6ae63022012-02-14 02:15:49 +0000726 if (BaseElementTy.isPODType(CGF.getContext()) ||
Richard Smith419bd092015-04-29 19:26:57 +0000727 (CE && isMemcpyEquivalentSpecialMember(CE->getConstructor()))) {
David Majnemer1573d732014-10-15 04:54:54 +0000728 unsigned SrcArgIndex =
729 CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args);
Eli Friedman6ae63022012-02-14 02:15:49 +0000730 llvm::Value *SrcPtr
731 = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
Eli Friedman7f1ff602012-04-16 03:54:45 +0000732 LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
733 LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000734
Eli Friedman6ae63022012-02-14 02:15:49 +0000735 // Copy the aggregate.
736 CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType,
Chad Rosier615ed1a2012-03-29 17:37:10 +0000737 LHS.isVolatileQualified());
Alexey Bataev5d49b832015-07-08 07:31:02 +0000738 // Ensure that we destroy the objects if an exception is thrown later in
739 // the constructor.
740 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
741 if (CGF.needsEHCleanup(dtorKind))
742 CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
Eli Friedman6ae63022012-02-14 02:15:49 +0000743 return;
744 }
745 }
746
747 ArrayRef<VarDecl *> ArrayIndexes;
748 if (MemberInit->getNumArrayIndices())
749 ArrayIndexes = MemberInit->getArrayIndexes();
David Blaikie66e41972015-01-14 07:38:27 +0000750 CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit(), ArrayIndexes);
Eli Friedman6ae63022012-02-14 02:15:49 +0000751}
752
John McCall7f416cc2015-09-08 08:05:57 +0000753void CodeGenFunction::EmitInitializerForField(FieldDecl *Field, LValue LHS,
754 Expr *Init, ArrayRef<VarDecl *> ArrayIndexes) {
Eli Friedman6ae63022012-02-14 02:15:49 +0000755 QualType FieldType = Field->getType();
John McCall47fb9502013-03-07 21:37:08 +0000756 switch (getEvaluationKind(FieldType)) {
757 case TEK_Scalar:
John McCall31168b02011-06-15 23:02:42 +0000758 if (LHS.isSimple()) {
David Blaikie66e41972015-01-14 07:38:27 +0000759 EmitExprAsInit(Init, Field, LHS, false);
John McCall31168b02011-06-15 23:02:42 +0000760 } else {
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000761 RValue RHS = RValue::get(EmitScalarExpr(Init));
762 EmitStoreThroughLValue(RHS, LHS);
John McCall31168b02011-06-15 23:02:42 +0000763 }
John McCall47fb9502013-03-07 21:37:08 +0000764 break;
765 case TEK_Complex:
David Blaikie66e41972015-01-14 07:38:27 +0000766 EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true);
John McCall47fb9502013-03-07 21:37:08 +0000767 break;
768 case TEK_Aggregate: {
John McCall7f416cc2015-09-08 08:05:57 +0000769 Address ArrayIndexVar = Address::invalid();
Eli Friedman6ae63022012-02-14 02:15:49 +0000770 if (ArrayIndexes.size()) {
Douglas Gregor94f9a482010-05-05 05:51:00 +0000771 // The LHS is a pointer to the first object we'll be constructing, as
772 // a flat array.
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000773 QualType BaseElementTy = getContext().getBaseElementType(FieldType);
774 llvm::Type *BasePtr = ConvertType(BaseElementTy);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000775 BasePtr = llvm::PointerType::getUnqual(BasePtr);
John McCall7f416cc2015-09-08 08:05:57 +0000776 Address BaseAddrPtr = Builder.CreateBitCast(LHS.getAddress(), BasePtr);
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000777 LHS = MakeAddrLValue(BaseAddrPtr, BaseElementTy);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000778
Douglas Gregor94f9a482010-05-05 05:51:00 +0000779 // Create an array index that will be used to walk over all of the
780 // objects we're constructing.
John McCall7f416cc2015-09-08 08:05:57 +0000781 ArrayIndexVar = CreateMemTemp(getContext().getSizeType(), "object.index");
782 llvm::Value *Zero =
783 llvm::Constant::getNullValue(ArrayIndexVar.getElementType());
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000784 Builder.CreateStore(Zero, ArrayIndexVar);
Justin Bogner1cd11f12015-05-20 15:53:59 +0000785
Douglas Gregor94f9a482010-05-05 05:51:00 +0000786 // Emit the block variables for the array indices, if any.
Eli Friedman6ae63022012-02-14 02:15:49 +0000787 for (unsigned I = 0, N = ArrayIndexes.size(); I != N; ++I)
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000788 EmitAutoVarDecl(*ArrayIndexes[I]);
Douglas Gregor94f9a482010-05-05 05:51:00 +0000789 }
Justin Bogner1cd11f12015-05-20 15:53:59 +0000790
Eli Friedman5f1a04f2012-02-14 02:31:03 +0000791 EmitAggMemberInitializer(*this, LHS, Init, ArrayIndexVar, FieldType,
Eli Friedman6ae63022012-02-14 02:15:49 +0000792 ArrayIndexes, 0);
Anders Carlssonfb404882009-12-24 22:46:43 +0000793 }
John McCall47fb9502013-03-07 21:37:08 +0000794 }
John McCall12cc42a2013-02-01 05:11:40 +0000795
796 // Ensure that we destroy this object if an exception is thrown
797 // later in the constructor.
798 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
799 if (needsEHCleanup(dtorKind))
800 pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
Anders Carlssonfb404882009-12-24 22:46:43 +0000801}
802
John McCallf8ff7b92010-02-23 00:48:20 +0000803/// Checks whether the given constructor is a valid subject for the
804/// complete-to-base constructor delegation optimization, i.e.
805/// emitting the complete constructor as a simple call to the base
806/// constructor.
807static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) {
808
809 // Currently we disable the optimization for classes with virtual
810 // bases because (1) the addresses of parameter variables need to be
811 // consistent across all initializers but (2) the delegate function
812 // call necessarily creates a second copy of the parameter variable.
813 //
814 // The limiting example (purely theoretical AFAIK):
815 // struct A { A(int &c) { c++; } };
816 // struct B : virtual A {
817 // B(int count) : A(count) { printf("%d\n", count); }
818 // };
819 // ...although even this example could in principle be emitted as a
820 // delegation since the address of the parameter doesn't escape.
821 if (Ctor->getParent()->getNumVBases()) {
822 // TODO: white-list trivial vbase initializers. This case wouldn't
823 // be subject to the restrictions below.
824
825 // TODO: white-list cases where:
826 // - there are no non-reference parameters to the constructor
827 // - the initializers don't access any non-reference parameters
828 // - the initializers don't take the address of non-reference
829 // parameters
830 // - etc.
831 // If we ever add any of the above cases, remember that:
832 // - function-try-blocks will always blacklist this optimization
833 // - we need to perform the constructor prologue and cleanup in
834 // EmitConstructorBody.
835
836 return false;
837 }
838
839 // We also disable the optimization for variadic functions because
840 // it's impossible to "re-pass" varargs.
841 if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic())
842 return false;
843
Alexis Hunt61bc1732011-05-01 07:04:31 +0000844 // FIXME: Decide if we can do a delegation of a delegating constructor.
845 if (Ctor->isDelegatingConstructor())
846 return false;
847
John McCallf8ff7b92010-02-23 00:48:20 +0000848 return true;
849}
850
Kostya Serebryany293dc9b2014-10-16 20:54:52 +0000851// Emit code in ctor (Prologue==true) or dtor (Prologue==false)
852// to poison the extra field paddings inserted under
853// -fsanitize-address-field-padding=1|2.
854void CodeGenFunction::EmitAsanPrologueOrEpilogue(bool Prologue) {
855 ASTContext &Context = getContext();
856 const CXXRecordDecl *ClassDecl =
857 Prologue ? cast<CXXConstructorDecl>(CurGD.getDecl())->getParent()
858 : cast<CXXDestructorDecl>(CurGD.getDecl())->getParent();
859 if (!ClassDecl->mayInsertExtraPadding()) return;
860
861 struct SizeAndOffset {
862 uint64_t Size;
863 uint64_t Offset;
864 };
865
866 unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits();
867 const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl);
868
869 // Populate sizes and offsets of fields.
870 SmallVector<SizeAndOffset, 16> SSV(Info.getFieldCount());
871 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i)
872 SSV[i].Offset =
873 Context.toCharUnitsFromBits(Info.getFieldOffset(i)).getQuantity();
874
875 size_t NumFields = 0;
876 for (const auto *Field : ClassDecl->fields()) {
877 const FieldDecl *D = Field;
878 std::pair<CharUnits, CharUnits> FieldInfo =
879 Context.getTypeInfoInChars(D->getType());
880 CharUnits FieldSize = FieldInfo.first;
881 assert(NumFields < SSV.size());
882 SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity();
883 NumFields++;
884 }
885 assert(NumFields == SSV.size());
886 if (SSV.size() <= 1) return;
887
888 // We will insert calls to __asan_* run-time functions.
889 // LLVM AddressSanitizer pass may decide to inline them later.
890 llvm::Type *Args[2] = {IntPtrTy, IntPtrTy};
891 llvm::FunctionType *FTy =
892 llvm::FunctionType::get(CGM.VoidTy, Args, false);
893 llvm::Constant *F = CGM.CreateRuntimeFunction(
894 FTy, Prologue ? "__asan_poison_intra_object_redzone"
895 : "__asan_unpoison_intra_object_redzone");
896
897 llvm::Value *ThisPtr = LoadCXXThis();
898 ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy);
Kostya Serebryany64449212014-10-17 21:02:13 +0000899 uint64_t TypeSize = Info.getNonVirtualSize().getQuantity();
Kostya Serebryany293dc9b2014-10-16 20:54:52 +0000900 // For each field check if it has sufficient padding,
901 // if so (un)poison it with a call.
902 for (size_t i = 0; i < SSV.size(); i++) {
903 uint64_t AsanAlignment = 8;
904 uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset;
905 uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size;
906 uint64_t EndOffset = SSV[i].Offset + SSV[i].Size;
907 if (PoisonSize < AsanAlignment || !SSV[i].Size ||
908 (NextField % AsanAlignment) != 0)
909 continue;
David Blaikie43f9bb72015-05-18 22:14:03 +0000910 Builder.CreateCall(
911 F, {Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)),
912 Builder.getIntN(PtrSize, PoisonSize)});
Kostya Serebryany293dc9b2014-10-16 20:54:52 +0000913 }
914}
915
John McCallb81884d2010-02-19 09:25:03 +0000916/// EmitConstructorBody - Emits the body of the current constructor.
917void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) {
Kostya Serebryany293dc9b2014-10-16 20:54:52 +0000918 EmitAsanPrologueOrEpilogue(true);
John McCallb81884d2010-02-19 09:25:03 +0000919 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
920 CXXCtorType CtorType = CurGD.getCtorType();
921
Reid Kleckner340ad862014-01-13 22:57:31 +0000922 assert((CGM.getTarget().getCXXABI().hasConstructorVariants() ||
923 CtorType == Ctor_Complete) &&
924 "can only generate complete ctor for this ABI");
925
John McCallf8ff7b92010-02-23 00:48:20 +0000926 // Before we go any further, try the complete->base constructor
927 // delegation optimization.
Timur Iskhodzhanovf32a3772012-04-20 08:05:00 +0000928 if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
John McCallc8e01702013-04-16 22:48:15 +0000929 CGM.getTarget().getCXXABI().hasConstructorVariants()) {
Nick Lewycky2d84e842013-10-02 02:29:49 +0000930 EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getLocEnd());
John McCallf8ff7b92010-02-23 00:48:20 +0000931 return;
932 }
933
Hans Wennborgdcfba332015-10-06 23:40:43 +0000934 const FunctionDecl *Definition = nullptr;
Richard Smith46bb5812014-08-01 01:56:39 +0000935 Stmt *Body = Ctor->getBody(Definition);
936 assert(Definition == Ctor && "emitting wrong constructor body");
John McCallb81884d2010-02-19 09:25:03 +0000937
John McCallf8ff7b92010-02-23 00:48:20 +0000938 // Enter the function-try-block before the constructor prologue if
939 // applicable.
John McCallf8ff7b92010-02-23 00:48:20 +0000940 bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
John McCallf8ff7b92010-02-23 00:48:20 +0000941 if (IsTryBody)
John McCallb609d3f2010-07-07 06:56:46 +0000942 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
John McCallb81884d2010-02-19 09:25:03 +0000943
Justin Bogner66242d62015-04-23 23:06:47 +0000944 incrementProfileCounter(Body);
Justin Bogner81c22c22014-01-23 02:54:27 +0000945
Richard Smithcc1b96d2013-06-12 22:31:48 +0000946 RunCleanupsScope RunCleanups(*this);
John McCallb81884d2010-02-19 09:25:03 +0000947
John McCall88313032012-03-30 04:25:03 +0000948 // TODO: in restricted cases, we can emit the vbase initializers of
949 // a complete ctor and then delegate to the base ctor.
950
John McCallf8ff7b92010-02-23 00:48:20 +0000951 // Emit the constructor prologue, i.e. the base and member
952 // initializers.
Douglas Gregor94f9a482010-05-05 05:51:00 +0000953 EmitCtorPrologue(Ctor, CtorType, Args);
John McCallb81884d2010-02-19 09:25:03 +0000954
955 // Emit the body of the statement.
John McCallf8ff7b92010-02-23 00:48:20 +0000956 if (IsTryBody)
John McCallb81884d2010-02-19 09:25:03 +0000957 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
958 else if (Body)
959 EmitStmt(Body);
John McCallb81884d2010-02-19 09:25:03 +0000960
961 // Emit any cleanup blocks associated with the member or base
962 // initializers, which includes (along the exceptional path) the
963 // destructors for those members and bases that were fully
964 // constructed.
Richard Smithcc1b96d2013-06-12 22:31:48 +0000965 RunCleanups.ForceCleanup();
John McCallb81884d2010-02-19 09:25:03 +0000966
John McCallf8ff7b92010-02-23 00:48:20 +0000967 if (IsTryBody)
John McCallb609d3f2010-07-07 06:56:46 +0000968 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
John McCallb81884d2010-02-19 09:25:03 +0000969}
970
Lang Hamesbf122742013-02-17 07:22:09 +0000971namespace {
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000972 /// RAII object to indicate that codegen is copying the value representation
973 /// instead of the object representation. Useful when copying a struct or
974 /// class which has uninitialized members and we're only performing
975 /// lvalue-to-rvalue conversion on the object but not its members.
976 class CopyingValueRepresentation {
977 public:
978 explicit CopyingValueRepresentation(CodeGenFunction &CGF)
Alexey Samsonov035462c2014-10-30 19:33:44 +0000979 : CGF(CGF), OldSanOpts(CGF.SanOpts) {
Alexey Samsonovedf99a92014-11-07 22:29:38 +0000980 CGF.SanOpts.set(SanitizerKind::Bool, false);
981 CGF.SanOpts.set(SanitizerKind::Enum, false);
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000982 }
983 ~CopyingValueRepresentation() {
984 CGF.SanOpts = OldSanOpts;
985 }
986 private:
987 CodeGenFunction &CGF;
Alexey Samsonova0416102014-11-11 01:26:14 +0000988 SanitizerSet OldSanOpts;
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000989 };
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000990} // end anonymous namespace
Hans Wennborgdcfba332015-10-06 23:40:43 +0000991
Nick Lewycky8b4e3792013-09-11 02:03:20 +0000992namespace {
Lang Hamesbf122742013-02-17 07:22:09 +0000993 class FieldMemcpyizer {
994 public:
995 FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl,
996 const VarDecl *SrcRec)
Justin Bogner1cd11f12015-05-20 15:53:59 +0000997 : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec),
Lang Hamesbf122742013-02-17 07:22:09 +0000998 RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)),
Craig Topper8a13c412014-05-21 05:09:00 +0000999 FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0),
1000 LastFieldOffset(0), LastAddedFieldIndex(0) {}
Lang Hamesbf122742013-02-17 07:22:09 +00001001
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00001002 bool isMemcpyableField(FieldDecl *F) const {
1003 // Never memcpy fields when we are adding poisoned paddings.
Alexey Samsonova0416102014-11-11 01:26:14 +00001004 if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding)
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00001005 return false;
Lang Hamesbf122742013-02-17 07:22:09 +00001006 Qualifiers Qual = F->getType().getQualifiers();
1007 if (Qual.hasVolatile() || Qual.hasObjCLifetime())
1008 return false;
1009 return true;
1010 }
1011
1012 void addMemcpyableField(FieldDecl *F) {
Craig Topper8a13c412014-05-21 05:09:00 +00001013 if (!FirstField)
Lang Hamesbf122742013-02-17 07:22:09 +00001014 addInitialField(F);
1015 else
1016 addNextField(F);
1017 }
1018
David Majnemera586eb22014-10-10 18:57:10 +00001019 CharUnits getMemcpySize(uint64_t FirstByteOffset) const {
Lang Hamesbf122742013-02-17 07:22:09 +00001020 unsigned LastFieldSize =
1021 LastField->isBitField() ?
1022 LastField->getBitWidthValue(CGF.getContext()) :
Justin Bogner1cd11f12015-05-20 15:53:59 +00001023 CGF.getContext().getTypeSize(LastField->getType());
Lang Hamesbf122742013-02-17 07:22:09 +00001024 uint64_t MemcpySizeBits =
David Majnemera586eb22014-10-10 18:57:10 +00001025 LastFieldOffset + LastFieldSize - FirstByteOffset +
Lang Hamesbf122742013-02-17 07:22:09 +00001026 CGF.getContext().getCharWidth() - 1;
1027 CharUnits MemcpySize =
1028 CGF.getContext().toCharUnitsFromBits(MemcpySizeBits);
1029 return MemcpySize;
1030 }
1031
1032 void emitMemcpy() {
1033 // Give the subclass a chance to bail out if it feels the memcpy isn't
1034 // worth it (e.g. Hasn't aggregated enough data).
Craig Topper8a13c412014-05-21 05:09:00 +00001035 if (!FirstField) {
Lang Hamesbf122742013-02-17 07:22:09 +00001036 return;
1037 }
1038
David Majnemera586eb22014-10-10 18:57:10 +00001039 uint64_t FirstByteOffset;
Lang Hamesbf122742013-02-17 07:22:09 +00001040 if (FirstField->isBitField()) {
1041 const CGRecordLayout &RL =
1042 CGF.getTypes().getCGRecordLayout(FirstField->getParent());
1043 const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField);
David Majnemera586eb22014-10-10 18:57:10 +00001044 // FirstFieldOffset is not appropriate for bitfields,
Ulrich Weigand73263d72015-07-13 11:52:14 +00001045 // we need to use the storage offset instead.
Ulrich Weigand03ce2a12015-07-10 17:30:00 +00001046 FirstByteOffset = CGF.getContext().toBits(BFInfo.StorageOffset);
Lang Hames1694e0d2013-02-27 04:14:49 +00001047 } else {
David Majnemera586eb22014-10-10 18:57:10 +00001048 FirstByteOffset = FirstFieldOffset;
Lang Hames1694e0d2013-02-27 04:14:49 +00001049 }
Lang Hamesbf122742013-02-17 07:22:09 +00001050
David Majnemera586eb22014-10-10 18:57:10 +00001051 CharUnits MemcpySize = getMemcpySize(FirstByteOffset);
Lang Hamesbf122742013-02-17 07:22:09 +00001052 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
John McCall7f416cc2015-09-08 08:05:57 +00001053 Address ThisPtr = CGF.LoadCXXThisAddress();
1054 LValue DestLV = CGF.MakeAddrLValue(ThisPtr, RecordTy);
Lang Hamesbf122742013-02-17 07:22:09 +00001055 LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField);
1056 llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec));
1057 LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
1058 LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField);
1059
John McCall7f416cc2015-09-08 08:05:57 +00001060 emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddress() : Dest.getAddress(),
1061 Src.isBitField() ? Src.getBitFieldAddress() : Src.getAddress(),
1062 MemcpySize);
Lang Hamesbf122742013-02-17 07:22:09 +00001063 reset();
1064 }
1065
1066 void reset() {
Craig Topper8a13c412014-05-21 05:09:00 +00001067 FirstField = nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001068 }
1069
1070 protected:
1071 CodeGenFunction &CGF;
1072 const CXXRecordDecl *ClassDecl;
1073
1074 private:
John McCall7f416cc2015-09-08 08:05:57 +00001075 void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) {
1076 llvm::PointerType *DPT = DestPtr.getType();
Lang Hamesbf122742013-02-17 07:22:09 +00001077 llvm::Type *DBP =
1078 llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace());
1079 DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP);
1080
John McCall7f416cc2015-09-08 08:05:57 +00001081 llvm::PointerType *SPT = SrcPtr.getType();
Lang Hamesbf122742013-02-17 07:22:09 +00001082 llvm::Type *SBP =
1083 llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace());
1084 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP);
1085
John McCall7f416cc2015-09-08 08:05:57 +00001086 CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity());
Lang Hamesbf122742013-02-17 07:22:09 +00001087 }
1088
1089 void addInitialField(FieldDecl *F) {
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001090 FirstField = F;
1091 LastField = F;
1092 FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex());
1093 LastFieldOffset = FirstFieldOffset;
1094 LastAddedFieldIndex = F->getFieldIndex();
1095 }
Lang Hamesbf122742013-02-17 07:22:09 +00001096
1097 void addNextField(FieldDecl *F) {
John McCall6054d5a2013-05-07 05:20:46 +00001098 // For the most part, the following invariant will hold:
1099 // F->getFieldIndex() == LastAddedFieldIndex + 1
1100 // The one exception is that Sema won't add a copy-initializer for an
1101 // unnamed bitfield, which will show up here as a gap in the sequence.
1102 assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 &&
1103 "Cannot aggregate fields out of order.");
Lang Hamesbf122742013-02-17 07:22:09 +00001104 LastAddedFieldIndex = F->getFieldIndex();
1105
1106 // The 'first' and 'last' fields are chosen by offset, rather than field
1107 // index. This allows the code to support bitfields, as well as regular
1108 // fields.
1109 uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex());
1110 if (FOffset < FirstFieldOffset) {
1111 FirstField = F;
1112 FirstFieldOffset = FOffset;
1113 } else if (FOffset > LastFieldOffset) {
1114 LastField = F;
1115 LastFieldOffset = FOffset;
1116 }
1117 }
1118
1119 const VarDecl *SrcRec;
1120 const ASTRecordLayout &RecLayout;
1121 FieldDecl *FirstField;
1122 FieldDecl *LastField;
1123 uint64_t FirstFieldOffset, LastFieldOffset;
1124 unsigned LastAddedFieldIndex;
1125 };
1126
1127 class ConstructorMemcpyizer : public FieldMemcpyizer {
1128 private:
Lang Hamesbf122742013-02-17 07:22:09 +00001129 /// Get source argument for copy constructor. Returns null if not a copy
David Majnemer196ac332014-09-11 23:05:02 +00001130 /// constructor.
1131 static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF,
1132 const CXXConstructorDecl *CD,
Lang Hamesbf122742013-02-17 07:22:09 +00001133 FunctionArgList &Args) {
Jordan Rose54533f72013-08-07 16:16:48 +00001134 if (CD->isCopyOrMoveConstructor() && CD->isDefaulted())
David Majnemer196ac332014-09-11 23:05:02 +00001135 return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)];
Craig Topper8a13c412014-05-21 05:09:00 +00001136 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001137 }
1138
1139 // Returns true if a CXXCtorInitializer represents a member initialization
1140 // that can be rolled into a memcpy.
1141 bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
1142 if (!MemcpyableCtor)
1143 return false;
1144 FieldDecl *Field = MemberInit->getMember();
Craig Topper8a13c412014-05-21 05:09:00 +00001145 assert(Field && "No field for member init.");
Lang Hamesbf122742013-02-17 07:22:09 +00001146 QualType FieldType = Field->getType();
1147 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
1148
Richard Smith419bd092015-04-29 19:26:57 +00001149 // Bail out on non-memcpyable, not-trivially-copyable members.
1150 if (!(CE && isMemcpyEquivalentSpecialMember(CE->getConstructor())) &&
Lang Hamesbf122742013-02-17 07:22:09 +00001151 !(FieldType.isTriviallyCopyableType(CGF.getContext()) ||
1152 FieldType->isReferenceType()))
1153 return false;
1154
1155 // Bail out on volatile fields.
1156 if (!isMemcpyableField(Field))
1157 return false;
1158
1159 // Otherwise we're good.
1160 return true;
1161 }
1162
1163 public:
1164 ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD,
1165 FunctionArgList &Args)
David Majnemer196ac332014-09-11 23:05:02 +00001166 : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)),
Lang Hamesbf122742013-02-17 07:22:09 +00001167 ConstructorDecl(CD),
Jordan Rose54533f72013-08-07 16:16:48 +00001168 MemcpyableCtor(CD->isDefaulted() &&
Lang Hamesbf122742013-02-17 07:22:09 +00001169 CD->isCopyOrMoveConstructor() &&
1170 CGF.getLangOpts().getGC() == LangOptions::NonGC),
1171 Args(Args) { }
1172
1173 void addMemberInitializer(CXXCtorInitializer *MemberInit) {
1174 if (isMemberInitMemcpyable(MemberInit)) {
1175 AggregatedInits.push_back(MemberInit);
1176 addMemcpyableField(MemberInit->getMember());
1177 } else {
1178 emitAggregatedInits();
1179 EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit,
1180 ConstructorDecl, Args);
1181 }
1182 }
1183
1184 void emitAggregatedInits() {
1185 if (AggregatedInits.size() <= 1) {
1186 // This memcpy is too small to be worthwhile. Fall back on default
1187 // codegen.
Nick Lewycky8b4e3792013-09-11 02:03:20 +00001188 if (!AggregatedInits.empty()) {
1189 CopyingValueRepresentation CVR(CGF);
Lang Hamesbf122742013-02-17 07:22:09 +00001190 EmitMemberInitializer(CGF, ConstructorDecl->getParent(),
Nick Lewycky8b4e3792013-09-11 02:03:20 +00001191 AggregatedInits[0], ConstructorDecl, Args);
Alexey Bataev152c71f2015-07-14 07:55:48 +00001192 AggregatedInits.clear();
Lang Hamesbf122742013-02-17 07:22:09 +00001193 }
1194 reset();
1195 return;
1196 }
1197
1198 pushEHDestructors();
1199 emitMemcpy();
1200 AggregatedInits.clear();
1201 }
1202
1203 void pushEHDestructors() {
John McCall7f416cc2015-09-08 08:05:57 +00001204 Address ThisPtr = CGF.LoadCXXThisAddress();
Lang Hamesbf122742013-02-17 07:22:09 +00001205 QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
John McCall7f416cc2015-09-08 08:05:57 +00001206 LValue LHS = CGF.MakeAddrLValue(ThisPtr, RecordTy);
Lang Hamesbf122742013-02-17 07:22:09 +00001207
1208 for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
Alexey Bataev152c71f2015-07-14 07:55:48 +00001209 CXXCtorInitializer *MemberInit = AggregatedInits[i];
1210 QualType FieldType = MemberInit->getAnyMember()->getType();
Lang Hamesbf122742013-02-17 07:22:09 +00001211 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
Alexey Bataev152c71f2015-07-14 07:55:48 +00001212 if (!CGF.needsEHCleanup(dtorKind))
1213 continue;
1214 LValue FieldLHS = LHS;
1215 EmitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS);
1216 CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(), FieldType);
Lang Hamesbf122742013-02-17 07:22:09 +00001217 }
1218 }
1219
1220 void finish() {
1221 emitAggregatedInits();
1222 }
1223
1224 private:
1225 const CXXConstructorDecl *ConstructorDecl;
1226 bool MemcpyableCtor;
1227 FunctionArgList &Args;
1228 SmallVector<CXXCtorInitializer*, 16> AggregatedInits;
1229 };
1230
1231 class AssignmentMemcpyizer : public FieldMemcpyizer {
1232 private:
Lang Hamesbf122742013-02-17 07:22:09 +00001233 // Returns the memcpyable field copied by the given statement, if one
Nick Lewycky8b4e3792013-09-11 02:03:20 +00001234 // exists. Otherwise returns null.
1235 FieldDecl *getMemcpyableField(Stmt *S) {
Lang Hamesbf122742013-02-17 07:22:09 +00001236 if (!AssignmentsMemcpyable)
Craig Topper8a13c412014-05-21 05:09:00 +00001237 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001238 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
1239 // Recognise trivial assignments.
1240 if (BO->getOpcode() != BO_Assign)
Craig Topper8a13c412014-05-21 05:09:00 +00001241 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001242 MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS());
1243 if (!ME)
Craig Topper8a13c412014-05-21 05:09:00 +00001244 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001245 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1246 if (!Field || !isMemcpyableField(Field))
Craig Topper8a13c412014-05-21 05:09:00 +00001247 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001248 Stmt *RHS = BO->getRHS();
1249 if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS))
1250 RHS = EC->getSubExpr();
1251 if (!RHS)
Craig Topper8a13c412014-05-21 05:09:00 +00001252 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001253 MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS);
1254 if (dyn_cast<FieldDecl>(ME2->getMemberDecl()) != Field)
Craig Topper8a13c412014-05-21 05:09:00 +00001255 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001256 return Field;
1257 } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) {
1258 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl());
Richard Smith419bd092015-04-29 19:26:57 +00001259 if (!(MD && isMemcpyEquivalentSpecialMember(MD)))
Craig Topper8a13c412014-05-21 05:09:00 +00001260 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001261 MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument());
1262 if (!IOA)
Craig Topper8a13c412014-05-21 05:09:00 +00001263 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001264 FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl());
1265 if (!Field || !isMemcpyableField(Field))
Craig Topper8a13c412014-05-21 05:09:00 +00001266 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001267 MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0));
1268 if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl()))
Craig Topper8a13c412014-05-21 05:09:00 +00001269 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001270 return Field;
1271 } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
1272 FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1273 if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
Craig Topper8a13c412014-05-21 05:09:00 +00001274 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001275 Expr *DstPtr = CE->getArg(0);
1276 if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr))
1277 DstPtr = DC->getSubExpr();
1278 UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr);
1279 if (!DUO || DUO->getOpcode() != UO_AddrOf)
Craig Topper8a13c412014-05-21 05:09:00 +00001280 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001281 MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr());
1282 if (!ME)
Craig Topper8a13c412014-05-21 05:09:00 +00001283 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001284 FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1285 if (!Field || !isMemcpyableField(Field))
Craig Topper8a13c412014-05-21 05:09:00 +00001286 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001287 Expr *SrcPtr = CE->getArg(1);
1288 if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr))
1289 SrcPtr = SC->getSubExpr();
1290 UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr);
1291 if (!SUO || SUO->getOpcode() != UO_AddrOf)
Craig Topper8a13c412014-05-21 05:09:00 +00001292 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001293 MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr());
1294 if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl()))
Craig Topper8a13c412014-05-21 05:09:00 +00001295 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001296 return Field;
1297 }
1298
Craig Topper8a13c412014-05-21 05:09:00 +00001299 return nullptr;
Lang Hamesbf122742013-02-17 07:22:09 +00001300 }
1301
1302 bool AssignmentsMemcpyable;
1303 SmallVector<Stmt*, 16> AggregatedStmts;
1304
1305 public:
Lang Hamesbf122742013-02-17 07:22:09 +00001306 AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD,
1307 FunctionArgList &Args)
1308 : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
1309 AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
1310 assert(Args.size() == 2);
1311 }
1312
1313 void emitAssignment(Stmt *S) {
1314 FieldDecl *F = getMemcpyableField(S);
1315 if (F) {
1316 addMemcpyableField(F);
1317 AggregatedStmts.push_back(S);
Justin Bogner1cd11f12015-05-20 15:53:59 +00001318 } else {
Lang Hamesbf122742013-02-17 07:22:09 +00001319 emitAggregatedStmts();
1320 CGF.EmitStmt(S);
1321 }
1322 }
1323
1324 void emitAggregatedStmts() {
1325 if (AggregatedStmts.size() <= 1) {
Nick Lewycky8b4e3792013-09-11 02:03:20 +00001326 if (!AggregatedStmts.empty()) {
1327 CopyingValueRepresentation CVR(CGF);
1328 CGF.EmitStmt(AggregatedStmts[0]);
1329 }
Lang Hamesbf122742013-02-17 07:22:09 +00001330 reset();
1331 }
1332
1333 emitMemcpy();
1334 AggregatedStmts.clear();
1335 }
1336
1337 void finish() {
1338 emitAggregatedStmts();
1339 }
1340 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00001341} // end anonymous namespace
Lang Hamesbf122742013-02-17 07:22:09 +00001342
Piotr Padlewski338c9d02015-09-15 21:46:47 +00001343static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit) {
1344 const Type *BaseType = BaseInit->getBaseClass();
1345 const auto *BaseClassDecl =
1346 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1347 return BaseClassDecl->isDynamicClass();
1348}
1349
Anders Carlssonfb404882009-12-24 22:46:43 +00001350/// EmitCtorPrologue - This routine generates necessary code to initialize
1351/// base classes and non-static data members belonging to this constructor.
Anders Carlssonfb404882009-12-24 22:46:43 +00001352void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
Douglas Gregor94f9a482010-05-05 05:51:00 +00001353 CXXCtorType CtorType,
1354 FunctionArgList &Args) {
Alexis Hunt61bc1732011-05-01 07:04:31 +00001355 if (CD->isDelegatingConstructor())
1356 return EmitDelegatingCXXConstructorCall(CD, Args);
1357
Anders Carlssonfb404882009-12-24 22:46:43 +00001358 const CXXRecordDecl *ClassDecl = CD->getParent();
Anders Carlsson5dc86332010-02-02 19:58:43 +00001359
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001360 CXXConstructorDecl::init_const_iterator B = CD->init_begin(),
1361 E = CD->init_end();
1362
Craig Topper8a13c412014-05-21 05:09:00 +00001363 llvm::BasicBlock *BaseCtorContinueBB = nullptr;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001364 if (ClassDecl->getNumVBases() &&
1365 !CGM.getTarget().getCXXABI().hasConstructorVariants()) {
1366 // The ABIs that don't have constructor variants need to put a branch
1367 // before the virtual base initialization code.
Reid Kleckner7810af02013-06-19 15:20:38 +00001368 BaseCtorContinueBB =
1369 CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001370 assert(BaseCtorContinueBB);
1371 }
1372
Piotr Padlewski276a78d2015-10-02 22:12:40 +00001373 llvm::Value *const OldThis = CXXThisValue;
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001374 // Virtual base initializers first.
1375 for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) {
Piotr Padlewski276a78d2015-10-02 22:12:40 +00001376 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1377 CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1378 isInitializerOfDynamicClass(*B))
1379 CXXThisValue = Builder.CreateInvariantGroupBarrier(LoadCXXThis());
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001380 EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1381 }
1382
1383 if (BaseCtorContinueBB) {
1384 // Complete object handler should continue to the remaining initializers.
1385 Builder.CreateBr(BaseCtorContinueBB);
1386 EmitBlock(BaseCtorContinueBB);
1387 }
1388
1389 // Then, non-virtual base initializers.
1390 for (; B != E && (*B)->isBaseInitializer(); B++) {
1391 assert(!(*B)->isBaseVirtual());
Piotr Padlewski276a78d2015-10-02 22:12:40 +00001392
1393 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1394 CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1395 isInitializerOfDynamicClass(*B))
1396 CXXThisValue = Builder.CreateInvariantGroupBarrier(LoadCXXThis());
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001397 EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
Anders Carlssonfb404882009-12-24 22:46:43 +00001398 }
1399
Piotr Padlewski276a78d2015-10-02 22:12:40 +00001400 CXXThisValue = OldThis;
Piotr Padlewski338c9d02015-09-15 21:46:47 +00001401
Anders Carlssond5895932010-03-28 21:07:49 +00001402 InitializeVTablePointers(ClassDecl);
Anders Carlsson5dc86332010-02-02 19:58:43 +00001403
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001404 // And finally, initialize class members.
John McCall7f416cc2015-09-08 08:05:57 +00001405 FieldConstructionScope FCS(*this, LoadCXXThisAddress());
Lang Hamesbf122742013-02-17 07:22:09 +00001406 ConstructorMemcpyizer CM(*this, CD, Args);
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00001407 for (; B != E; B++) {
1408 CXXCtorInitializer *Member = (*B);
1409 assert(!Member->isBaseInitializer());
1410 assert(Member->isAnyMemberInitializer() &&
1411 "Delegating initializer on non-delegating constructor");
1412 CM.addMemberInitializer(Member);
1413 }
Lang Hamesbf122742013-02-17 07:22:09 +00001414 CM.finish();
Anders Carlssonfb404882009-12-24 22:46:43 +00001415}
1416
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001417static bool
1418FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field);
1419
1420static bool
Justin Bogner1cd11f12015-05-20 15:53:59 +00001421HasTrivialDestructorBody(ASTContext &Context,
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001422 const CXXRecordDecl *BaseClassDecl,
1423 const CXXRecordDecl *MostDerivedClassDecl)
1424{
1425 // If the destructor is trivial we don't have to check anything else.
1426 if (BaseClassDecl->hasTrivialDestructor())
1427 return true;
1428
1429 if (!BaseClassDecl->getDestructor()->hasTrivialBody())
1430 return false;
1431
1432 // Check fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001433 for (const auto *Field : BaseClassDecl->fields())
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001434 if (!FieldHasTrivialDestructorBody(Context, Field))
1435 return false;
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001436
1437 // Check non-virtual bases.
Aaron Ballman574705e2014-03-13 15:41:46 +00001438 for (const auto &I : BaseClassDecl->bases()) {
1439 if (I.isVirtual())
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001440 continue;
1441
1442 const CXXRecordDecl *NonVirtualBase =
Aaron Ballman574705e2014-03-13 15:41:46 +00001443 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001444 if (!HasTrivialDestructorBody(Context, NonVirtualBase,
1445 MostDerivedClassDecl))
1446 return false;
1447 }
1448
1449 if (BaseClassDecl == MostDerivedClassDecl) {
1450 // Check virtual bases.
Aaron Ballman445a9392014-03-13 16:15:17 +00001451 for (const auto &I : BaseClassDecl->vbases()) {
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001452 const CXXRecordDecl *VirtualBase =
Aaron Ballman445a9392014-03-13 16:15:17 +00001453 cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001454 if (!HasTrivialDestructorBody(Context, VirtualBase,
1455 MostDerivedClassDecl))
Justin Bogner1cd11f12015-05-20 15:53:59 +00001456 return false;
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001457 }
1458 }
1459
1460 return true;
1461}
1462
1463static bool
1464FieldHasTrivialDestructorBody(ASTContext &Context,
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001465 const FieldDecl *Field)
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001466{
1467 QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
1468
1469 const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
1470 if (!RT)
1471 return true;
Justin Bogner1cd11f12015-05-20 15:53:59 +00001472
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001473 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
Davide Italiano982bbf42015-06-26 00:18:35 +00001474
1475 // The destructor for an implicit anonymous union member is never invoked.
1476 if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
1477 return false;
1478
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001479 return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
1480}
1481
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001482/// CanSkipVTablePointerInitialization - Check whether we need to initialize
1483/// any vtable pointers before calling this destructor.
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001484static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF,
Anders Carlssond6f15182011-05-16 04:08:36 +00001485 const CXXDestructorDecl *Dtor) {
Piotr Padlewski338c9d02015-09-15 21:46:47 +00001486 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1487 if (!ClassDecl->isDynamicClass())
1488 return true;
1489
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001490 if (!Dtor->hasTrivialBody())
1491 return false;
1492
1493 // Check the fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001494 for (const auto *Field : ClassDecl->fields())
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001495 if (!FieldHasTrivialDestructorBody(CGF.getContext(), Field))
Anders Carlsson49c0bd22011-05-15 17:36:21 +00001496 return false;
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001497
1498 return true;
1499}
1500
John McCallb81884d2010-02-19 09:25:03 +00001501/// EmitDestructorBody - Emits the body of the current destructor.
1502void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
1503 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
1504 CXXDtorType DtorType = CurGD.getDtorType();
1505
Justin Bognerfb298222015-05-20 16:16:23 +00001506 Stmt *Body = Dtor->getBody();
1507 if (Body)
1508 incrementProfileCounter(Body);
1509
John McCallf99a6312010-07-21 05:30:47 +00001510 // The call to operator delete in a deleting destructor happens
1511 // outside of the function-try-block, which means it's always
1512 // possible to delegate the destructor body to the complete
1513 // destructor. Do so.
1514 if (DtorType == Dtor_Deleting) {
1515 EnterDtorCleanups(Dtor, Dtor_Deleting);
1516 EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
John McCall7f416cc2015-09-08 08:05:57 +00001517 /*Delegating=*/false, LoadCXXThisAddress());
John McCallf99a6312010-07-21 05:30:47 +00001518 PopCleanupBlock();
1519 return;
1520 }
1521
John McCallb81884d2010-02-19 09:25:03 +00001522 // If the body is a function-try-block, enter the try before
John McCallf99a6312010-07-21 05:30:47 +00001523 // anything else.
1524 bool isTryBody = (Body && isa<CXXTryStmt>(Body));
John McCallb81884d2010-02-19 09:25:03 +00001525 if (isTryBody)
John McCallb609d3f2010-07-07 06:56:46 +00001526 EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
Kostya Serebryany293dc9b2014-10-16 20:54:52 +00001527 EmitAsanPrologueOrEpilogue(false);
John McCallb81884d2010-02-19 09:25:03 +00001528
John McCallf99a6312010-07-21 05:30:47 +00001529 // Enter the epilogue cleanups.
1530 RunCleanupsScope DtorEpilogue(*this);
Justin Bogner1cd11f12015-05-20 15:53:59 +00001531
John McCallb81884d2010-02-19 09:25:03 +00001532 // If this is the complete variant, just invoke the base variant;
1533 // the epilogue will destruct the virtual bases. But we can't do
1534 // this optimization if the body is a function-try-block, because
Justin Bogner1cd11f12015-05-20 15:53:59 +00001535 // we'd introduce *two* handler blocks. In the Microsoft ABI, we
Reid Klecknere7de47e2013-07-22 13:51:44 +00001536 // always delegate because we might not have a definition in this TU.
John McCallf99a6312010-07-21 05:30:47 +00001537 switch (DtorType) {
Rafael Espindola1e4df922014-09-16 15:18:21 +00001538 case Dtor_Comdat:
1539 llvm_unreachable("not expecting a COMDAT");
1540
John McCallf99a6312010-07-21 05:30:47 +00001541 case Dtor_Deleting: llvm_unreachable("already handled deleting case");
1542
1543 case Dtor_Complete:
Reid Klecknere7de47e2013-07-22 13:51:44 +00001544 assert((Body || getTarget().getCXXABI().isMicrosoft()) &&
1545 "can't emit a dtor without a body for non-Microsoft ABIs");
1546
John McCallf99a6312010-07-21 05:30:47 +00001547 // Enter the cleanup scopes for virtual bases.
1548 EnterDtorCleanups(Dtor, Dtor_Complete);
1549
Reid Klecknere7de47e2013-07-22 13:51:44 +00001550 if (!isTryBody) {
John McCallf99a6312010-07-21 05:30:47 +00001551 EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
John McCall7f416cc2015-09-08 08:05:57 +00001552 /*Delegating=*/false, LoadCXXThisAddress());
John McCallf99a6312010-07-21 05:30:47 +00001553 break;
1554 }
1555 // Fallthrough: act like we're in the base variant.
Justin Bogner1cd11f12015-05-20 15:53:59 +00001556
John McCallf99a6312010-07-21 05:30:47 +00001557 case Dtor_Base:
Reid Klecknere7de47e2013-07-22 13:51:44 +00001558 assert(Body);
1559
John McCallf99a6312010-07-21 05:30:47 +00001560 // Enter the cleanup scopes for fields and non-virtual bases.
1561 EnterDtorCleanups(Dtor, Dtor_Base);
1562
1563 // Initialize the vtable pointers before entering the body.
Piotr Padlewski338c9d02015-09-15 21:46:47 +00001564 if (!CanSkipVTablePointerInitialization(*this, Dtor)) {
1565 // Insert the llvm.invariant.group.barrier intrinsic before initializing
1566 // the vptrs to cancel any previous assumptions we might have made.
1567 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1568 CGM.getCodeGenOpts().OptimizationLevel > 0)
1569 CXXThisValue = Builder.CreateInvariantGroupBarrier(LoadCXXThis());
1570 InitializeVTablePointers(Dtor->getParent());
1571 }
John McCallf99a6312010-07-21 05:30:47 +00001572
1573 if (isTryBody)
1574 EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
1575 else if (Body)
1576 EmitStmt(Body);
1577 else {
1578 assert(Dtor->isImplicit() && "bodyless dtor not implicit");
1579 // nothing to do besides what's in the epilogue
1580 }
Fariborz Jahanian0c12ed12011-02-02 23:12:46 +00001581 // -fapple-kext must inline any call to this dtor into
1582 // the caller's body.
Richard Smith9c6890a2012-11-01 22:30:59 +00001583 if (getLangOpts().AppleKext)
Evgeniy Stepanov6b2a61d2015-09-14 21:35:16 +00001584 CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
Naomi Musgravee50cb9b2015-08-13 18:35:11 +00001585
John McCallf99a6312010-07-21 05:30:47 +00001586 break;
John McCallb81884d2010-02-19 09:25:03 +00001587 }
1588
John McCallf99a6312010-07-21 05:30:47 +00001589 // Jump out through the epilogue cleanups.
1590 DtorEpilogue.ForceCleanup();
John McCallb81884d2010-02-19 09:25:03 +00001591
1592 // Exit the try if applicable.
1593 if (isTryBody)
John McCallb609d3f2010-07-07 06:56:46 +00001594 ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
John McCallb81884d2010-02-19 09:25:03 +00001595}
1596
Lang Hamesbf122742013-02-17 07:22:09 +00001597void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) {
1598 const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl());
1599 const Stmt *RootS = AssignOp->getBody();
1600 assert(isa<CompoundStmt>(RootS) &&
1601 "Body of an implicit assignment operator should be compound stmt.");
1602 const CompoundStmt *RootCS = cast<CompoundStmt>(RootS);
1603
1604 LexicalScope Scope(*this, RootCS->getSourceRange());
1605
Xinliang David Lia951e8e2016-02-09 20:02:59 +00001606 incrementProfileCounter(RootCS);
Lang Hamesbf122742013-02-17 07:22:09 +00001607 AssignmentMemcpyizer AM(*this, AssignOp, Args);
Aaron Ballmanc7e4e212014-03-17 14:19:37 +00001608 for (auto *I : RootCS->body())
Justin Bogner1cd11f12015-05-20 15:53:59 +00001609 AM.emitAssignment(I);
Lang Hamesbf122742013-02-17 07:22:09 +00001610 AM.finish();
1611}
1612
John McCallf99a6312010-07-21 05:30:47 +00001613namespace {
1614 /// Call the operator delete associated with the current destructor.
David Blaikie7e70d682015-08-18 22:40:54 +00001615 struct CallDtorDelete final : EHScopeStack::Cleanup {
John McCallf99a6312010-07-21 05:30:47 +00001616 CallDtorDelete() {}
1617
Craig Topper4f12f102014-03-12 06:41:41 +00001618 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCallf99a6312010-07-21 05:30:47 +00001619 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1620 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1621 CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1622 CGF.getContext().getTagDeclType(ClassDecl));
1623 }
1624 };
1625
David Blaikie7e70d682015-08-18 22:40:54 +00001626 struct CallDtorDeleteConditional final : EHScopeStack::Cleanup {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001627 llvm::Value *ShouldDeleteCondition;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001628
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001629 public:
1630 CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition)
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001631 : ShouldDeleteCondition(ShouldDeleteCondition) {
Craig Topper8a13c412014-05-21 05:09:00 +00001632 assert(ShouldDeleteCondition != nullptr);
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001633 }
1634
Craig Topper4f12f102014-03-12 06:41:41 +00001635 void Emit(CodeGenFunction &CGF, Flags flags) override {
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001636 llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete");
1637 llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue");
1638 llvm::Value *ShouldCallDelete
1639 = CGF.Builder.CreateIsNull(ShouldDeleteCondition);
1640 CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB);
1641
1642 CGF.EmitBlock(callDeleteBB);
1643 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1644 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1645 CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1646 CGF.getContext().getTagDeclType(ClassDecl));
1647 CGF.Builder.CreateBr(continueBB);
1648
1649 CGF.EmitBlock(continueBB);
1650 }
1651 };
1652
David Blaikie7e70d682015-08-18 22:40:54 +00001653 class DestroyField final : public EHScopeStack::Cleanup {
John McCall4bd0fb12011-07-12 16:41:08 +00001654 const FieldDecl *field;
Peter Collingbourne1425b452012-01-26 03:33:36 +00001655 CodeGenFunction::Destroyer *destroyer;
John McCall4bd0fb12011-07-12 16:41:08 +00001656 bool useEHCleanupForArray;
John McCallf99a6312010-07-21 05:30:47 +00001657
John McCall4bd0fb12011-07-12 16:41:08 +00001658 public:
1659 DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
1660 bool useEHCleanupForArray)
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001661 : field(field), destroyer(destroyer),
1662 useEHCleanupForArray(useEHCleanupForArray) {}
John McCallf99a6312010-07-21 05:30:47 +00001663
Craig Topper4f12f102014-03-12 06:41:41 +00001664 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall4bd0fb12011-07-12 16:41:08 +00001665 // Find the address of the field.
John McCall7f416cc2015-09-08 08:05:57 +00001666 Address thisValue = CGF.LoadCXXThisAddress();
Eli Friedman7f1ff602012-04-16 03:54:45 +00001667 QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent());
1668 LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy);
1669 LValue LV = CGF.EmitLValueForField(ThisLV, field);
John McCall4bd0fb12011-07-12 16:41:08 +00001670 assert(LV.isSimple());
Justin Bogner1cd11f12015-05-20 15:53:59 +00001671
John McCall4bd0fb12011-07-12 16:41:08 +00001672 CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer,
John McCall30317fd2011-07-12 20:27:29 +00001673 flags.isForNormalCleanup() && useEHCleanupForArray);
John McCallf99a6312010-07-21 05:30:47 +00001674 }
1675 };
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001676
Naomi Musgrave703835c2015-09-16 00:38:22 +00001677 static void EmitSanitizerDtorCallback(CodeGenFunction &CGF, llvm::Value *Ptr,
1678 CharUnits::QuantityType PoisonSize) {
1679 // Pass in void pointer and size of region as arguments to runtime
1680 // function
1681 llvm::Value *Args[] = {CGF.Builder.CreateBitCast(Ptr, CGF.VoidPtrTy),
1682 llvm::ConstantInt::get(CGF.SizeTy, PoisonSize)};
1683
1684 llvm::Type *ArgTypes[] = {CGF.VoidPtrTy, CGF.SizeTy};
1685
1686 llvm::FunctionType *FnType =
1687 llvm::FunctionType::get(CGF.VoidTy, ArgTypes, false);
1688 llvm::Value *Fn =
1689 CGF.CGM.CreateRuntimeFunction(FnType, "__sanitizer_dtor_callback");
1690 CGF.EmitNounwindRuntimeCall(Fn, Args);
1691 }
1692
1693 class SanitizeDtorMembers final : public EHScopeStack::Cleanup {
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001694 const CXXDestructorDecl *Dtor;
1695
1696 public:
Naomi Musgrave703835c2015-09-16 00:38:22 +00001697 SanitizeDtorMembers(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {}
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001698
1699 // Generate function call for handling object poisoning.
1700 // Disables tail call elimination, to prevent the current stack frame
1701 // from disappearing from the stack trace.
1702 void Emit(CodeGenFunction &CGF, Flags flags) override {
1703 const ASTRecordLayout &Layout =
1704 CGF.getContext().getASTRecordLayout(Dtor->getParent());
1705
1706 // Nothing to poison.
1707 if (Layout.getFieldCount() == 0)
1708 return;
1709
1710 // Prevent the current stack frame from disappearing from the stack trace.
1711 CGF.CurFn->addFnAttr("disable-tail-calls", "true");
1712
1713 // Construct pointer to region to begin poisoning, and calculate poison
1714 // size, so that only members declared in this class are poisoned.
1715 ASTContext &Context = CGF.getContext();
1716 unsigned fieldIndex = 0;
1717 int startIndex = -1;
1718 // RecordDecl::field_iterator Field;
1719 for (const FieldDecl *Field : Dtor->getParent()->fields()) {
1720 // Poison field if it is trivial
1721 if (FieldHasTrivialDestructorBody(Context, Field)) {
1722 // Start sanitizing at this field
1723 if (startIndex < 0)
1724 startIndex = fieldIndex;
1725
1726 // Currently on the last field, and it must be poisoned with the
1727 // current block.
1728 if (fieldIndex == Layout.getFieldCount() - 1) {
Naomi Musgrave703835c2015-09-16 00:38:22 +00001729 PoisonMembers(CGF, startIndex, Layout.getFieldCount());
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001730 }
1731 } else if (startIndex >= 0) {
1732 // No longer within a block of memory to poison, so poison the block
Naomi Musgrave703835c2015-09-16 00:38:22 +00001733 PoisonMembers(CGF, startIndex, fieldIndex);
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001734 // Re-set the start index
1735 startIndex = -1;
1736 }
1737 fieldIndex += 1;
1738 }
1739 }
1740
1741 private:
NAKAMURA Takumif6cef72f2015-09-04 05:19:31 +00001742 /// \param layoutStartOffset index of the ASTRecordLayout field to
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001743 /// start poisoning (inclusive)
NAKAMURA Takumif6cef72f2015-09-04 05:19:31 +00001744 /// \param layoutEndOffset index of the ASTRecordLayout field to
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001745 /// end poisoning (exclusive)
Naomi Musgrave703835c2015-09-16 00:38:22 +00001746 void PoisonMembers(CodeGenFunction &CGF, unsigned layoutStartOffset,
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001747 unsigned layoutEndOffset) {
1748 ASTContext &Context = CGF.getContext();
1749 const ASTRecordLayout &Layout =
1750 Context.getASTRecordLayout(Dtor->getParent());
1751
1752 llvm::ConstantInt *OffsetSizePtr = llvm::ConstantInt::get(
1753 CGF.SizeTy,
1754 Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutStartOffset))
1755 .getQuantity());
1756
1757 llvm::Value *OffsetPtr = CGF.Builder.CreateGEP(
1758 CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.Int8PtrTy),
1759 OffsetSizePtr);
1760
1761 CharUnits::QuantityType PoisonSize;
1762 if (layoutEndOffset >= Layout.getFieldCount()) {
1763 PoisonSize = Layout.getNonVirtualSize().getQuantity() -
1764 Context.toCharUnitsFromBits(
1765 Layout.getFieldOffset(layoutStartOffset))
1766 .getQuantity();
1767 } else {
1768 PoisonSize = Context.toCharUnitsFromBits(
1769 Layout.getFieldOffset(layoutEndOffset) -
1770 Layout.getFieldOffset(layoutStartOffset))
1771 .getQuantity();
1772 }
1773
1774 if (PoisonSize == 0)
1775 return;
1776
Naomi Musgrave703835c2015-09-16 00:38:22 +00001777 EmitSanitizerDtorCallback(CGF, OffsetPtr, PoisonSize);
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001778 }
1779 };
Naomi Musgrave703835c2015-09-16 00:38:22 +00001780
1781 class SanitizeDtorVTable final : public EHScopeStack::Cleanup {
1782 const CXXDestructorDecl *Dtor;
1783
1784 public:
1785 SanitizeDtorVTable(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {}
1786
1787 // Generate function call for handling vtable pointer poisoning.
1788 void Emit(CodeGenFunction &CGF, Flags flags) override {
1789 assert(Dtor->getParent()->isDynamicClass());
NAKAMURA Takumiee82b492015-09-16 06:26:56 +00001790 (void)Dtor;
Naomi Musgrave703835c2015-09-16 00:38:22 +00001791 ASTContext &Context = CGF.getContext();
1792 // Poison vtable and vtable ptr if they exist for this class.
1793 llvm::Value *VTablePtr = CGF.LoadCXXThis();
1794
1795 CharUnits::QuantityType PoisonSize =
1796 Context.toCharUnitsFromBits(CGF.PointerWidthInBits).getQuantity();
1797 // Pass in void pointer and size of region as arguments to runtime
1798 // function
1799 EmitSanitizerDtorCallback(CGF, VTablePtr, PoisonSize);
1800 }
1801 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00001802} // end anonymous namespace
John McCallf99a6312010-07-21 05:30:47 +00001803
Hans Wennborgdeff7032013-12-18 01:39:59 +00001804/// \brief Emit all code that comes at the end of class's
Anders Carlssonfb404882009-12-24 22:46:43 +00001805/// destructor. This is to call destructors on members and base classes
1806/// in reverse order of their construction.
John McCallf99a6312010-07-21 05:30:47 +00001807void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD,
1808 CXXDtorType DtorType) {
Hans Wennborg853ae942014-05-30 16:59:42 +00001809 assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) &&
1810 "Should not emit dtor epilogue for non-exported trivial dtor!");
Anders Carlssonfb404882009-12-24 22:46:43 +00001811
John McCallf99a6312010-07-21 05:30:47 +00001812 // The deleting-destructor phase just needs to call the appropriate
1813 // operator delete that Sema picked up.
John McCall5c60a6f2010-02-18 19:59:28 +00001814 if (DtorType == Dtor_Deleting) {
Justin Bogner1cd11f12015-05-20 15:53:59 +00001815 assert(DD->getOperatorDelete() &&
Hans Wennborgdeff7032013-12-18 01:39:59 +00001816 "operator delete missing - EnterDtorCleanups");
Timur Iskhodzhanovee6bc532013-02-13 08:37:51 +00001817 if (CXXStructorImplicitParamValue) {
1818 // If there is an implicit param to the deleting dtor, it's a boolean
1819 // telling whether we should call delete at the end of the dtor.
1820 EHStack.pushCleanup<CallDtorDeleteConditional>(
1821 NormalAndEHCleanup, CXXStructorImplicitParamValue);
1822 } else {
1823 EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1824 }
John McCall5c60a6f2010-02-18 19:59:28 +00001825 return;
1826 }
1827
John McCallf99a6312010-07-21 05:30:47 +00001828 const CXXRecordDecl *ClassDecl = DD->getParent();
1829
Richard Smith20104042011-09-18 12:11:43 +00001830 // Unions have no bases and do not call field destructors.
1831 if (ClassDecl->isUnion())
1832 return;
1833
John McCallf99a6312010-07-21 05:30:47 +00001834 // The complete-destructor phase just destructs all the virtual bases.
John McCall5c60a6f2010-02-18 19:59:28 +00001835 if (DtorType == Dtor_Complete) {
Naomi Musgrave703835c2015-09-16 00:38:22 +00001836 // Poison the vtable pointer such that access after the base
1837 // and member destructors are invoked is invalid.
1838 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1839 SanOpts.has(SanitizerKind::Memory) && ClassDecl->getNumVBases() &&
1840 ClassDecl->isPolymorphic())
1841 EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD);
John McCallf99a6312010-07-21 05:30:47 +00001842
1843 // We push them in the forward order so that they'll be popped in
1844 // the reverse order.
Aaron Ballman445a9392014-03-13 16:15:17 +00001845 for (const auto &Base : ClassDecl->vbases()) {
John McCall5c60a6f2010-02-18 19:59:28 +00001846 CXXRecordDecl *BaseClassDecl
1847 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
Justin Bogner1cd11f12015-05-20 15:53:59 +00001848
John McCall5c60a6f2010-02-18 19:59:28 +00001849 // Ignore trivial destructors.
1850 if (BaseClassDecl->hasTrivialDestructor())
1851 continue;
John McCallf99a6312010-07-21 05:30:47 +00001852
John McCallcda666c2010-07-21 07:22:38 +00001853 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1854 BaseClassDecl,
1855 /*BaseIsVirtual*/ true);
John McCall5c60a6f2010-02-18 19:59:28 +00001856 }
John McCallf99a6312010-07-21 05:30:47 +00001857
John McCall5c60a6f2010-02-18 19:59:28 +00001858 return;
1859 }
1860
1861 assert(DtorType == Dtor_Base);
Naomi Musgrave703835c2015-09-16 00:38:22 +00001862 // Poison the vtable pointer if it has no virtual bases, but inherits
1863 // virtual functions.
1864 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1865 SanOpts.has(SanitizerKind::Memory) && !ClassDecl->getNumVBases() &&
1866 ClassDecl->isPolymorphic())
1867 EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD);
Justin Bogner1cd11f12015-05-20 15:53:59 +00001868
John McCallf99a6312010-07-21 05:30:47 +00001869 // Destroy non-virtual bases.
Aaron Ballman574705e2014-03-13 15:41:46 +00001870 for (const auto &Base : ClassDecl->bases()) {
John McCallf99a6312010-07-21 05:30:47 +00001871 // Ignore virtual bases.
1872 if (Base.isVirtual())
1873 continue;
Justin Bogner1cd11f12015-05-20 15:53:59 +00001874
John McCallf99a6312010-07-21 05:30:47 +00001875 CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
Justin Bogner1cd11f12015-05-20 15:53:59 +00001876
John McCallf99a6312010-07-21 05:30:47 +00001877 // Ignore trivial destructors.
1878 if (BaseClassDecl->hasTrivialDestructor())
1879 continue;
John McCall5c60a6f2010-02-18 19:59:28 +00001880
John McCallcda666c2010-07-21 07:22:38 +00001881 EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1882 BaseClassDecl,
1883 /*BaseIsVirtual*/ false);
John McCallf99a6312010-07-21 05:30:47 +00001884 }
1885
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001886 // Poison fields such that access after their destructors are
1887 // invoked, and before the base class destructor runs, is invalid.
1888 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1889 SanOpts.has(SanitizerKind::Memory))
Naomi Musgrave703835c2015-09-16 00:38:22 +00001890 EHStack.pushCleanup<SanitizeDtorMembers>(NormalAndEHCleanup, DD);
Naomi Musgrave866af2d2015-09-03 23:02:30 +00001891
John McCallf99a6312010-07-21 05:30:47 +00001892 // Destroy direct fields.
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001893 for (const auto *Field : ClassDecl->fields()) {
1894 QualType type = Field->getType();
John McCall4bd0fb12011-07-12 16:41:08 +00001895 QualType::DestructionKind dtorKind = type.isDestructedType();
1896 if (!dtorKind) continue;
John McCallf99a6312010-07-21 05:30:47 +00001897
Richard Smith921bd202012-02-26 09:11:52 +00001898 // Anonymous union members do not have their destructors called.
1899 const RecordType *RT = type->getAsUnionType();
1900 if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue;
1901
John McCall4bd0fb12011-07-12 16:41:08 +00001902 CleanupKind cleanupKind = getCleanupKind(dtorKind);
Aaron Ballmane8a8bae2014-03-08 20:12:42 +00001903 EHStack.pushCleanup<DestroyField>(cleanupKind, Field,
John McCall4bd0fb12011-07-12 16:41:08 +00001904 getDestroyer(dtorKind),
1905 cleanupKind & EHCleanup);
Anders Carlssonfb404882009-12-24 22:46:43 +00001906 }
Anders Carlssonfb404882009-12-24 22:46:43 +00001907}
1908
John McCallf677a8e2011-07-13 06:10:41 +00001909/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1910/// constructor for each of several members of an array.
Douglas Gregor05fc5be2010-07-21 01:10:17 +00001911///
John McCallf677a8e2011-07-13 06:10:41 +00001912/// \param ctor the constructor to call for each element
John McCallf677a8e2011-07-13 06:10:41 +00001913/// \param arrayType the type of the array to initialize
1914/// \param arrayBegin an arrayType*
1915/// \param zeroInitialize true if each element should be
1916/// zero-initialized before it is constructed
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001917void CodeGenFunction::EmitCXXAggrConstructorCall(
1918 const CXXConstructorDecl *ctor, const ConstantArrayType *arrayType,
John McCall7f416cc2015-09-08 08:05:57 +00001919 Address arrayBegin, const CXXConstructExpr *E, bool zeroInitialize) {
John McCallf677a8e2011-07-13 06:10:41 +00001920 QualType elementType;
1921 llvm::Value *numElements =
1922 emitArrayLength(arrayType, elementType, arrayBegin);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001923
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001924 EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, zeroInitialize);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001925}
1926
John McCallf677a8e2011-07-13 06:10:41 +00001927/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1928/// constructor for each of several members of an array.
1929///
1930/// \param ctor the constructor to call for each element
1931/// \param numElements the number of elements in the array;
John McCall6549b312011-07-13 07:37:11 +00001932/// may be zero
NAKAMURA Takumiff7a9252015-09-08 09:42:41 +00001933/// \param arrayBase a T*, where T is the type constructed by ctor
John McCallf677a8e2011-07-13 06:10:41 +00001934/// \param zeroInitialize true if each element should be
1935/// zero-initialized before it is constructed
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001936void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
1937 llvm::Value *numElements,
John McCall7f416cc2015-09-08 08:05:57 +00001938 Address arrayBase,
Alexey Samsonov70b9c012014-08-21 20:26:47 +00001939 const CXXConstructExpr *E,
1940 bool zeroInitialize) {
John McCall6549b312011-07-13 07:37:11 +00001941 // It's legal for numElements to be zero. This can happen both
1942 // dynamically, because x can be zero in 'new A[x]', and statically,
1943 // because of GCC extensions that permit zero-length arrays. There
1944 // are probably legitimate places where we could assume that this
1945 // doesn't happen, but it's not clear that it's worth it.
Craig Topper8a13c412014-05-21 05:09:00 +00001946 llvm::BranchInst *zeroCheckBranch = nullptr;
John McCall6549b312011-07-13 07:37:11 +00001947
1948 // Optimize for a constant count.
1949 llvm::ConstantInt *constantCount
1950 = dyn_cast<llvm::ConstantInt>(numElements);
1951 if (constantCount) {
1952 // Just skip out if the constant count is zero.
1953 if (constantCount->isZero()) return;
1954
1955 // Otherwise, emit the check.
1956 } else {
1957 llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
1958 llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
1959 zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
1960 EmitBlock(loopBB);
1961 }
Justin Bogner1cd11f12015-05-20 15:53:59 +00001962
John McCallf677a8e2011-07-13 06:10:41 +00001963 // Find the end of the array.
John McCall7f416cc2015-09-08 08:05:57 +00001964 llvm::Value *arrayBegin = arrayBase.getPointer();
John McCallf677a8e2011-07-13 06:10:41 +00001965 llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements,
1966 "arrayctor.end");
Anders Carlsson27da15b2010-01-01 20:29:01 +00001967
John McCallf677a8e2011-07-13 06:10:41 +00001968 // Enter the loop, setting up a phi for the current location to initialize.
1969 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1970 llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
1971 EmitBlock(loopBB);
1972 llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
1973 "arrayctor.cur");
1974 cur->addIncoming(arrayBegin, entryBB);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001975
Anders Carlsson27da15b2010-01-01 20:29:01 +00001976 // Inside the loop body, emit the constructor call on the array element.
John McCallf677a8e2011-07-13 06:10:41 +00001977
John McCall7f416cc2015-09-08 08:05:57 +00001978 // The alignment of the base, adjusted by the size of a single element,
1979 // provides a conservative estimate of the alignment of every element.
1980 // (This assumes we never start tracking offsetted alignments.)
1981 //
1982 // Note that these are complete objects and so we don't need to
1983 // use the non-virtual size or alignment.
John McCallf677a8e2011-07-13 06:10:41 +00001984 QualType type = getContext().getTypeDeclType(ctor->getParent());
John McCall7f416cc2015-09-08 08:05:57 +00001985 CharUnits eltAlignment =
1986 arrayBase.getAlignment()
1987 .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
1988 Address curAddr = Address(cur, eltAlignment);
Anders Carlsson27da15b2010-01-01 20:29:01 +00001989
Douglas Gregor05fc5be2010-07-21 01:10:17 +00001990 // Zero initialize the storage, if requested.
John McCallf677a8e2011-07-13 06:10:41 +00001991 if (zeroInitialize)
John McCall7f416cc2015-09-08 08:05:57 +00001992 EmitNullInitialization(curAddr, type);
Justin Bogner1cd11f12015-05-20 15:53:59 +00001993
1994 // C++ [class.temporary]p4:
Anders Carlsson27da15b2010-01-01 20:29:01 +00001995 // There are two contexts in which temporaries are destroyed at a different
1996 // point than the end of the full-expression. The first context is when a
Justin Bogner1cd11f12015-05-20 15:53:59 +00001997 // default constructor is called to initialize an element of an array.
1998 // If the constructor has one or more default arguments, the destruction of
1999 // every temporary created in a default argument expression is sequenced
Anders Carlsson27da15b2010-01-01 20:29:01 +00002000 // before the construction of the next array element, if any.
Justin Bogner1cd11f12015-05-20 15:53:59 +00002001
Anders Carlssonb9fd57f2010-03-30 03:14:41 +00002002 {
John McCallbd309292010-07-06 01:34:17 +00002003 RunCleanupsScope Scope(*this);
Anders Carlsson27da15b2010-01-01 20:29:01 +00002004
John McCallf677a8e2011-07-13 06:10:41 +00002005 // Evaluate the constructor and its arguments in a regular
2006 // partial-destroy cleanup.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002007 if (getLangOpts().Exceptions &&
John McCallf677a8e2011-07-13 06:10:41 +00002008 !ctor->getParent()->hasTrivialDestructor()) {
2009 Destroyer *destroyer = destroyCXXObject;
John McCall7f416cc2015-09-08 08:05:57 +00002010 pushRegularPartialArrayCleanup(arrayBegin, cur, type, eltAlignment,
2011 *destroyer);
John McCallf677a8e2011-07-13 06:10:41 +00002012 }
2013
Alexey Samsonov70b9c012014-08-21 20:26:47 +00002014 EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false,
John McCall7f416cc2015-09-08 08:05:57 +00002015 /*Delegating=*/false, curAddr, E);
Anders Carlssonb9fd57f2010-03-30 03:14:41 +00002016 }
Anders Carlsson27da15b2010-01-01 20:29:01 +00002017
John McCallf677a8e2011-07-13 06:10:41 +00002018 // Go to the next element.
2019 llvm::Value *next =
2020 Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1),
2021 "arrayctor.next");
2022 cur->addIncoming(next, Builder.GetInsertBlock());
Anders Carlsson27da15b2010-01-01 20:29:01 +00002023
John McCallf677a8e2011-07-13 06:10:41 +00002024 // Check whether that's the end of the loop.
2025 llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
2026 llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
2027 Builder.CreateCondBr(done, contBB, loopBB);
Anders Carlsson27da15b2010-01-01 20:29:01 +00002028
John McCall6549b312011-07-13 07:37:11 +00002029 // Patch the earlier check to skip over the loop.
2030 if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
2031
John McCallf677a8e2011-07-13 06:10:41 +00002032 EmitBlock(contBB);
Anders Carlsson27da15b2010-01-01 20:29:01 +00002033}
2034
John McCall82fe67b2011-07-09 01:37:26 +00002035void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002036 Address addr,
John McCall82fe67b2011-07-09 01:37:26 +00002037 QualType type) {
2038 const RecordType *rtype = type->castAs<RecordType>();
2039 const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
2040 const CXXDestructorDecl *dtor = record->getDestructor();
2041 assert(!dtor->isTrivial());
2042 CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
Douglas Gregor61535002013-01-31 05:50:40 +00002043 /*Delegating=*/false, addr);
John McCall82fe67b2011-07-09 01:37:26 +00002044}
2045
Alexey Samsonov70b9c012014-08-21 20:26:47 +00002046void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D,
2047 CXXCtorType Type,
2048 bool ForVirtualBase,
John McCall7f416cc2015-09-08 08:05:57 +00002049 bool Delegating, Address This,
Alexey Samsonov70b9c012014-08-21 20:26:47 +00002050 const CXXConstructExpr *E) {
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002051 const CXXRecordDecl *ClassDecl = D->getParent();
2052
Richard Smith419bd092015-04-29 19:26:57 +00002053 // C++11 [class.mfct.non-static]p2:
2054 // If a non-static member function of a class X is called for an object that
2055 // is not of type X, or of a type derived from X, the behavior is undefined.
2056 // FIXME: Provide a source location here.
John McCall7f416cc2015-09-08 08:05:57 +00002057 EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, SourceLocation(),
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002058 This.getPointer(), getContext().getRecordType(ClassDecl));
John McCallca972cd2010-02-06 00:25:16 +00002059
Richard Smith419bd092015-04-29 19:26:57 +00002060 if (D->isTrivial() && D->isDefaultConstructor()) {
2061 assert(E->getNumArgs() == 0 && "trivial default ctor with args");
2062 return;
2063 }
2064
2065 // If this is a trivial constructor, just emit what's needed. If this is a
2066 // union copy constructor, we must emit a memcpy, because the AST does not
2067 // model that copy.
2068 if (isMemcpyEquivalentSpecialMember(D)) {
Alexey Samsonov70b9c012014-08-21 20:26:47 +00002069 assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
John McCallca972cd2010-02-06 00:25:16 +00002070
Alexey Samsonov70b9c012014-08-21 20:26:47 +00002071 const Expr *Arg = E->getArg(0);
David Majnemerfd1e7392015-02-03 23:04:06 +00002072 QualType SrcTy = Arg->getType();
John McCall7f416cc2015-09-08 08:05:57 +00002073 Address Src = EmitLValue(Arg).getAddress();
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002074 QualType DestTy = getContext().getTypeDeclType(ClassDecl);
David Majnemerfd1e7392015-02-03 23:04:06 +00002075 EmitAggregateCopyCtor(This, Src, DestTy, SrcTy);
Anders Carlsson27da15b2010-01-01 20:29:01 +00002076 return;
2077 }
2078
Reid Kleckner89077a12013-12-17 19:46:40 +00002079 CallArgList Args;
2080
2081 // Push the this ptr.
John McCall7f416cc2015-09-08 08:05:57 +00002082 Args.add(RValue::get(This.getPointer()), D->getThisType(getContext()));
Reid Kleckner89077a12013-12-17 19:46:40 +00002083
2084 // Add the rest of the user-supplied arguments.
2085 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
David Blaikief05779e2015-07-21 18:37:18 +00002086 EmitCallArgs(Args, FPT, E->arguments(), E->getConstructor());
Reid Kleckner89077a12013-12-17 19:46:40 +00002087
2088 // Insert any ABI-specific implicit constructor arguments.
2089 unsigned ExtraArgs = CGM.getCXXABI().addImplicitConstructorArgs(
2090 *this, D, Type, ForVirtualBase, Delegating, Args);
2091
2092 // Emit the call.
Rafael Espindola1ac0ec82014-09-11 15:42:06 +00002093 llvm::Value *Callee = CGM.getAddrOfCXXStructor(D, getFromCtorType(Type));
Reid Kleckner89077a12013-12-17 19:46:40 +00002094 const CGFunctionInfo &Info =
Reid Kleckner314ef7b2014-02-01 00:04:45 +00002095 CGM.getTypes().arrangeCXXConstructorCall(Args, D, Type, ExtraArgs);
Reid Kleckner89077a12013-12-17 19:46:40 +00002096 EmitCall(Info, Callee, ReturnValueSlot(), Args, D);
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002097
2098 // Generate vtable assumptions if we're constructing a complete object
2099 // with a vtable. We don't do this for base subobjects for two reasons:
2100 // first, it's incorrect for classes with virtual bases, and second, we're
2101 // about to overwrite the vptrs anyway.
2102 // We also have to make sure if we can refer to vtable:
2103 // - Otherwise we can refer to vtable if it's safe to speculatively emit.
2104 // FIXME: If vtable is used by ctor/dtor, or if vtable is external and we are
2105 // sure that definition of vtable is not hidden,
2106 // then we are always safe to refer to it.
Piotr Padlewski69dc9712015-09-28 20:30:22 +00002107 // FIXME: It looks like InstCombine is very inefficient on dealing with
2108 // assumes. Make assumption loads require -fstrict-vtable-pointers temporarily.
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002109 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2110 ClassDecl->isDynamicClass() && Type != Ctor_Base &&
Piotr Padlewski69dc9712015-09-28 20:30:22 +00002111 CGM.getCXXABI().canSpeculativelyEmitVTable(ClassDecl) &&
2112 CGM.getCodeGenOpts().StrictVTablePointers)
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002113 EmitVTableAssumptionLoads(ClassDecl, This);
2114}
2115
2116void CodeGenFunction::EmitVTableAssumptionLoad(const VPtr &Vptr, Address This) {
2117 llvm::Value *VTableGlobal =
2118 CGM.getCXXABI().getVTableAddressPoint(Vptr.Base, Vptr.VTableClass);
2119 if (!VTableGlobal)
2120 return;
2121
2122 // We can just use the base offset in the complete class.
2123 CharUnits NonVirtualOffset = Vptr.Base.getBaseOffset();
2124
2125 if (!NonVirtualOffset.isZero())
2126 This =
2127 ApplyNonVirtualAndVirtualOffset(*this, This, NonVirtualOffset, nullptr,
2128 Vptr.VTableClass, Vptr.NearestVBase);
2129
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00002130 llvm::Value *VPtrValue =
2131 GetVTablePtr(This, VTableGlobal->getType(), Vptr.VTableClass);
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002132 llvm::Value *Cmp =
2133 Builder.CreateICmpEQ(VPtrValue, VTableGlobal, "cmp.vtables");
2134 Builder.CreateAssumption(Cmp);
2135}
2136
2137void CodeGenFunction::EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl,
2138 Address This) {
2139 if (CGM.getCXXABI().doStructorsInitializeVPtrs(ClassDecl))
2140 for (const VPtr &Vptr : getVTablePointers(ClassDecl))
2141 EmitVTableAssumptionLoad(Vptr, This);
Anders Carlsson27da15b2010-01-01 20:29:01 +00002142}
2143
John McCallf8ff7b92010-02-23 00:48:20 +00002144void
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002145CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
John McCall7f416cc2015-09-08 08:05:57 +00002146 Address This, Address Src,
2147 const CXXConstructExpr *E) {
Richard Smith419bd092015-04-29 19:26:57 +00002148 if (isMemcpyEquivalentSpecialMember(D)) {
Alexey Samsonov96fd0a42014-08-26 20:18:26 +00002149 assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
Sebastian Redl22653ba2011-08-30 19:58:05 +00002150 assert(D->isCopyOrMoveConstructor() &&
2151 "trivial 1-arg ctor not a copy/move ctor");
David Majnemerfd1e7392015-02-03 23:04:06 +00002152 EmitAggregateCopyCtor(This, Src,
2153 getContext().getTypeDeclType(D->getParent()),
Benjamin Kramerf48ee442015-07-18 14:35:53 +00002154 (*E->arg_begin())->getType());
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002155 return;
2156 }
Rafael Espindola1ac0ec82014-09-11 15:42:06 +00002157 llvm::Value *Callee = CGM.getAddrOfCXXStructor(D, StructorType::Complete);
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002158 assert(D->isInstance() &&
2159 "Trying to emit a member call expr on a static method!");
Justin Bogner1cd11f12015-05-20 15:53:59 +00002160
Reid Kleckner739756c2013-12-04 19:23:12 +00002161 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
Justin Bogner1cd11f12015-05-20 15:53:59 +00002162
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002163 CallArgList Args;
Justin Bogner1cd11f12015-05-20 15:53:59 +00002164
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002165 // Push the this ptr.
John McCall7f416cc2015-09-08 08:05:57 +00002166 Args.add(RValue::get(This.getPointer()), D->getThisType(getContext()));
Justin Bogner1cd11f12015-05-20 15:53:59 +00002167
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002168 // Push the src ptr.
Alp Toker9cacbab2014-01-20 20:26:09 +00002169 QualType QT = *(FPT->param_type_begin());
Chris Lattner2192fe52011-07-18 04:24:23 +00002170 llvm::Type *t = CGM.getTypes().ConvertType(QT);
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002171 Src = Builder.CreateBitCast(Src, t);
John McCall7f416cc2015-09-08 08:05:57 +00002172 Args.add(RValue::get(Src.getPointer()), QT);
Reid Kleckner739756c2013-12-04 19:23:12 +00002173
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002174 // Skip over first argument (Src).
David Blaikief05779e2015-07-21 18:37:18 +00002175 EmitCallArgs(Args, FPT, drop_begin(E->arguments(), 1), E->getConstructor(),
Alexey Samsonov8e1162c2014-09-08 17:22:45 +00002176 /*ParamsToSkip*/ 1);
Reid Kleckner739756c2013-12-04 19:23:12 +00002177
John McCall8dda7b22012-07-07 06:41:13 +00002178 EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, RequiredArgs::All),
2179 Callee, ReturnValueSlot(), Args, D);
Fariborz Jahaniane988bda2010-11-13 21:53:34 +00002180}
2181
2182void
John McCallf8ff7b92010-02-23 00:48:20 +00002183CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
2184 CXXCtorType CtorType,
Nick Lewycky2d84e842013-10-02 02:29:49 +00002185 const FunctionArgList &Args,
2186 SourceLocation Loc) {
John McCallf8ff7b92010-02-23 00:48:20 +00002187 CallArgList DelegateArgs;
2188
2189 FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
2190 assert(I != E && "no parameters to constructor");
2191
2192 // this
Eli Friedman43dca6a2011-05-02 17:57:46 +00002193 DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType());
John McCallf8ff7b92010-02-23 00:48:20 +00002194 ++I;
2195
2196 // vtt
Timur Iskhodzhanov57cbe5c2013-02-27 13:46:31 +00002197 if (llvm::Value *VTT = GetVTTParameter(GlobalDecl(Ctor, CtorType),
Douglas Gregor61535002013-01-31 05:50:40 +00002198 /*ForVirtualBase=*/false,
2199 /*Delegating=*/true)) {
John McCallf8ff7b92010-02-23 00:48:20 +00002200 QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00002201 DelegateArgs.add(RValue::get(VTT), VoidPP);
John McCallf8ff7b92010-02-23 00:48:20 +00002202
Peter Collingbourne66f82e62013-06-28 20:45:28 +00002203 if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) {
John McCallf8ff7b92010-02-23 00:48:20 +00002204 assert(I != E && "cannot skip vtt parameter, already done with args");
John McCalla738c252011-03-09 04:27:21 +00002205 assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type");
John McCallf8ff7b92010-02-23 00:48:20 +00002206 ++I;
2207 }
2208 }
2209
2210 // Explicit arguments.
2211 for (; I != E; ++I) {
John McCall32ea9692011-03-11 20:59:21 +00002212 const VarDecl *param = *I;
Nick Lewycky2d84e842013-10-02 02:29:49 +00002213 // FIXME: per-argument source location
2214 EmitDelegateCallArg(DelegateArgs, param, Loc);
John McCallf8ff7b92010-02-23 00:48:20 +00002215 }
2216
Rafael Espindola1ac0ec82014-09-11 15:42:06 +00002217 llvm::Value *Callee =
2218 CGM.getAddrOfCXXStructor(Ctor, getFromCtorType(CtorType));
Rafael Espindola8d2a19b2014-09-08 16:01:27 +00002219 EmitCall(CGM.getTypes()
2220 .arrangeCXXStructorDeclaration(Ctor, getFromCtorType(CtorType)),
Manman Ren01754612013-03-20 16:59:38 +00002221 Callee, ReturnValueSlot(), DelegateArgs, Ctor);
John McCallf8ff7b92010-02-23 00:48:20 +00002222}
2223
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002224namespace {
David Blaikie7e70d682015-08-18 22:40:54 +00002225 struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup {
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002226 const CXXDestructorDecl *Dtor;
John McCall7f416cc2015-09-08 08:05:57 +00002227 Address Addr;
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002228 CXXDtorType Type;
2229
John McCall7f416cc2015-09-08 08:05:57 +00002230 CallDelegatingCtorDtor(const CXXDestructorDecl *D, Address Addr,
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002231 CXXDtorType Type)
2232 : Dtor(D), Addr(Addr), Type(Type) {}
2233
Craig Topper4f12f102014-03-12 06:41:41 +00002234 void Emit(CodeGenFunction &CGF, Flags flags) override {
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002235 CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
Douglas Gregor61535002013-01-31 05:50:40 +00002236 /*Delegating=*/true, Addr);
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002237 }
2238 };
Hans Wennborgdcfba332015-10-06 23:40:43 +00002239} // end anonymous namespace
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002240
Alexis Hunt61bc1732011-05-01 07:04:31 +00002241void
2242CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
2243 const FunctionArgList &Args) {
2244 assert(Ctor->isDelegatingConstructor());
2245
John McCall7f416cc2015-09-08 08:05:57 +00002246 Address ThisPtr = LoadCXXThisAddress();
Alexis Hunt61bc1732011-05-01 07:04:31 +00002247
John McCall31168b02011-06-15 23:02:42 +00002248 AggValueSlot AggSlot =
John McCall7f416cc2015-09-08 08:05:57 +00002249 AggValueSlot::forAddr(ThisPtr, Qualifiers(),
John McCall8d6fc952011-08-25 20:40:09 +00002250 AggValueSlot::IsDestructed,
John McCalla5efa732011-08-25 23:04:34 +00002251 AggValueSlot::DoesNotNeedGCBarriers,
Chad Rosier615ed1a2012-03-29 17:37:10 +00002252 AggValueSlot::IsNotAliased);
Alexis Hunt61bc1732011-05-01 07:04:31 +00002253
2254 EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
Alexis Hunt61bc1732011-05-01 07:04:31 +00002255
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002256 const CXXRecordDecl *ClassDecl = Ctor->getParent();
David Blaikiebbafb8a2012-03-11 07:00:24 +00002257 if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
Alexis Hunt9d47faf2011-05-03 23:05:34 +00002258 CXXDtorType Type =
2259 CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
2260
2261 EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
2262 ClassDecl->getDestructor(),
2263 ThisPtr, Type);
2264 }
2265}
Alexis Hunt61bc1732011-05-01 07:04:31 +00002266
Anders Carlsson27da15b2010-01-01 20:29:01 +00002267void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
2268 CXXDtorType Type,
Anders Carlssonf8a71f02010-05-02 23:29:11 +00002269 bool ForVirtualBase,
Douglas Gregor61535002013-01-31 05:50:40 +00002270 bool Delegating,
John McCall7f416cc2015-09-08 08:05:57 +00002271 Address This) {
Reid Kleckner6fe771a2013-12-13 00:53:54 +00002272 CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase,
2273 Delegating, This);
Anders Carlsson27da15b2010-01-01 20:29:01 +00002274}
2275
John McCall53cad2e2010-07-21 01:41:18 +00002276namespace {
David Blaikie7e70d682015-08-18 22:40:54 +00002277 struct CallLocalDtor final : EHScopeStack::Cleanup {
John McCall53cad2e2010-07-21 01:41:18 +00002278 const CXXDestructorDecl *Dtor;
John McCall7f416cc2015-09-08 08:05:57 +00002279 Address Addr;
John McCall53cad2e2010-07-21 01:41:18 +00002280
John McCall7f416cc2015-09-08 08:05:57 +00002281 CallLocalDtor(const CXXDestructorDecl *D, Address Addr)
John McCall53cad2e2010-07-21 01:41:18 +00002282 : Dtor(D), Addr(Addr) {}
2283
Craig Topper4f12f102014-03-12 06:41:41 +00002284 void Emit(CodeGenFunction &CGF, Flags flags) override {
John McCall53cad2e2010-07-21 01:41:18 +00002285 CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete,
Douglas Gregor61535002013-01-31 05:50:40 +00002286 /*ForVirtualBase=*/false,
2287 /*Delegating=*/false, Addr);
John McCall53cad2e2010-07-21 01:41:18 +00002288 }
2289 };
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00002290} // end anonymous namespace
John McCall53cad2e2010-07-21 01:41:18 +00002291
John McCall8680f872010-07-21 06:29:51 +00002292void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D,
John McCall7f416cc2015-09-08 08:05:57 +00002293 Address Addr) {
John McCallcda666c2010-07-21 07:22:38 +00002294 EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
John McCall8680f872010-07-21 06:29:51 +00002295}
2296
John McCall7f416cc2015-09-08 08:05:57 +00002297void CodeGenFunction::PushDestructorCleanup(QualType T, Address Addr) {
John McCallbd309292010-07-06 01:34:17 +00002298 CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
2299 if (!ClassDecl) return;
2300 if (ClassDecl->hasTrivialDestructor()) return;
2301
2302 const CXXDestructorDecl *D = ClassDecl->getDestructor();
John McCalla85af562011-04-28 02:15:35 +00002303 assert(D && D->isUsed() && "destructor not marked as used!");
John McCall8680f872010-07-21 06:29:51 +00002304 PushDestructorCleanup(D, Addr);
John McCallbd309292010-07-06 01:34:17 +00002305}
2306
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002307void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) {
Anders Carlssone87fae92010-03-28 19:40:00 +00002308 // Compute the address point.
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002309 llvm::Value *VTableAddressPoint =
2310 CGM.getCXXABI().getVTableAddressPointInStructor(
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002311 *this, Vptr.VTableClass, Vptr.Base, Vptr.NearestVBase);
2312
Timur Iskhodzhanov8b5987e2013-09-27 14:48:01 +00002313 if (!VTableAddressPoint)
2314 return;
Anders Carlssone87fae92010-03-28 19:40:00 +00002315
Anders Carlsson6a0227d2010-04-20 16:22:16 +00002316 // Compute where to store the address point.
Craig Topper8a13c412014-05-21 05:09:00 +00002317 llvm::Value *VirtualOffset = nullptr;
Ken Dyckcfc332c2011-03-23 00:45:26 +00002318 CharUnits NonVirtualOffset = CharUnits::Zero();
Justin Bogner1cd11f12015-05-20 15:53:59 +00002319
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002320 if (CGM.getCXXABI().isVirtualOffsetNeededForVTableField(*this, Vptr)) {
Anders Carlsson91baecf2010-04-20 18:05:10 +00002321 // We need to use the virtual base offset offset because the virtual base
2322 // might have a different offset in the most derived class.
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002323
2324 VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(
2325 *this, LoadCXXThisAddress(), Vptr.VTableClass, Vptr.NearestVBase);
2326 NonVirtualOffset = Vptr.OffsetFromNearestVBase;
Anders Carlsson91baecf2010-04-20 18:05:10 +00002327 } else {
Anders Carlssonc58fb552010-05-03 00:29:58 +00002328 // We can just use the base offset in the complete class.
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002329 NonVirtualOffset = Vptr.Base.getBaseOffset();
Anders Carlsson91baecf2010-04-20 18:05:10 +00002330 }
Justin Bogner1cd11f12015-05-20 15:53:59 +00002331
Anders Carlssonc58fb552010-05-03 00:29:58 +00002332 // Apply the offsets.
John McCall7f416cc2015-09-08 08:05:57 +00002333 Address VTableField = LoadCXXThisAddress();
Justin Bogner1cd11f12015-05-20 15:53:59 +00002334
Ken Dyckcfc332c2011-03-23 00:45:26 +00002335 if (!NonVirtualOffset.isZero() || VirtualOffset)
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002336 VTableField = ApplyNonVirtualAndVirtualOffset(
2337 *this, VTableField, NonVirtualOffset, VirtualOffset, Vptr.VTableClass,
2338 Vptr.NearestVBase);
Anders Carlsson6a0227d2010-04-20 16:22:16 +00002339
Reid Kleckner8d585132014-12-03 21:00:21 +00002340 // Finally, store the address point. Use the same LLVM types as the field to
2341 // support optimization.
2342 llvm::Type *VTablePtrTy =
2343 llvm::FunctionType::get(CGM.Int32Ty, /*isVarArg=*/true)
2344 ->getPointerTo()
2345 ->getPointerTo();
2346 VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo());
2347 VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy);
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002348
Kostya Serebryany141e46f2012-03-26 17:03:51 +00002349 llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00002350 CGM.DecorateInstructionWithTBAA(Store, CGM.getTBAAInfoForVTablePtr());
2351 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2352 CGM.getCodeGenOpts().StrictVTablePointers)
2353 CGM.DecorateInstructionWithInvariantGroup(Store, Vptr.VTableClass);
Anders Carlssone87fae92010-03-28 19:40:00 +00002354}
2355
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002356CodeGenFunction::VPtrsVector
2357CodeGenFunction::getVTablePointers(const CXXRecordDecl *VTableClass) {
2358 CodeGenFunction::VPtrsVector VPtrsResult;
2359 VisitedVirtualBasesSetTy VBases;
2360 getVTablePointers(BaseSubobject(VTableClass, CharUnits::Zero()),
2361 /*NearestVBase=*/nullptr,
2362 /*OffsetFromNearestVBase=*/CharUnits::Zero(),
2363 /*BaseIsNonVirtualPrimaryBase=*/false, VTableClass, VBases,
2364 VPtrsResult);
2365 return VPtrsResult;
2366}
2367
2368void CodeGenFunction::getVTablePointers(BaseSubobject Base,
2369 const CXXRecordDecl *NearestVBase,
2370 CharUnits OffsetFromNearestVBase,
2371 bool BaseIsNonVirtualPrimaryBase,
2372 const CXXRecordDecl *VTableClass,
2373 VisitedVirtualBasesSetTy &VBases,
2374 VPtrsVector &Vptrs) {
Anders Carlssond5895932010-03-28 21:07:49 +00002375 // If this base is a non-virtual primary base the address point has already
2376 // been set.
2377 if (!BaseIsNonVirtualPrimaryBase) {
2378 // Initialize the vtable pointer for this base.
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002379 VPtr Vptr = {Base, NearestVBase, OffsetFromNearestVBase, VTableClass};
2380 Vptrs.push_back(Vptr);
Anders Carlssond5895932010-03-28 21:07:49 +00002381 }
Justin Bogner1cd11f12015-05-20 15:53:59 +00002382
Anders Carlssond5895932010-03-28 21:07:49 +00002383 const CXXRecordDecl *RD = Base.getBase();
2384
2385 // Traverse bases.
Aaron Ballman574705e2014-03-13 15:41:46 +00002386 for (const auto &I : RD->bases()) {
Anders Carlssond5895932010-03-28 21:07:49 +00002387 CXXRecordDecl *BaseDecl
Aaron Ballman574705e2014-03-13 15:41:46 +00002388 = cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
Anders Carlssond5895932010-03-28 21:07:49 +00002389
2390 // Ignore classes without a vtable.
2391 if (!BaseDecl->isDynamicClass())
2392 continue;
2393
Ken Dyck3fb4c892011-03-23 01:04:18 +00002394 CharUnits BaseOffset;
2395 CharUnits BaseOffsetFromNearestVBase;
Anders Carlsson948d3f42010-03-29 01:16:41 +00002396 bool BaseDeclIsNonVirtualPrimaryBase;
Anders Carlssond5895932010-03-28 21:07:49 +00002397
Aaron Ballman574705e2014-03-13 15:41:46 +00002398 if (I.isVirtual()) {
Anders Carlssond5895932010-03-28 21:07:49 +00002399 // Check if we've visited this virtual base before.
David Blaikie82e95a32014-11-19 07:49:47 +00002400 if (!VBases.insert(BaseDecl).second)
Anders Carlssond5895932010-03-28 21:07:49 +00002401 continue;
2402
Justin Bogner1cd11f12015-05-20 15:53:59 +00002403 const ASTRecordLayout &Layout =
Anders Carlssond5895932010-03-28 21:07:49 +00002404 getContext().getASTRecordLayout(VTableClass);
2405
Ken Dyck3fb4c892011-03-23 01:04:18 +00002406 BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
2407 BaseOffsetFromNearestVBase = CharUnits::Zero();
Anders Carlsson948d3f42010-03-29 01:16:41 +00002408 BaseDeclIsNonVirtualPrimaryBase = false;
Anders Carlssond5895932010-03-28 21:07:49 +00002409 } else {
2410 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2411
Ken Dyck16ffcac2011-03-24 01:21:01 +00002412 BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
Justin Bogner1cd11f12015-05-20 15:53:59 +00002413 BaseOffsetFromNearestVBase =
Ken Dyck3fb4c892011-03-23 01:04:18 +00002414 OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
Anders Carlsson948d3f42010-03-29 01:16:41 +00002415 BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
Anders Carlssond5895932010-03-28 21:07:49 +00002416 }
Justin Bogner1cd11f12015-05-20 15:53:59 +00002417
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002418 getVTablePointers(
2419 BaseSubobject(BaseDecl, BaseOffset),
2420 I.isVirtual() ? BaseDecl : NearestVBase, BaseOffsetFromNearestVBase,
2421 BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, Vptrs);
Anders Carlssond5895932010-03-28 21:07:49 +00002422 }
2423}
2424
2425void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
2426 // Ignore classes without a vtable.
Anders Carlsson1f9348c2010-03-26 04:39:42 +00002427 if (!RD->isDynamicClass())
Anders Carlsson27da15b2010-01-01 20:29:01 +00002428 return;
2429
Anders Carlssond5895932010-03-28 21:07:49 +00002430 // Initialize the vtable pointers for this class and all of its bases.
Piotr Padlewskid679d7e2015-09-15 00:37:06 +00002431 if (CGM.getCXXABI().doStructorsInitializeVPtrs(RD))
2432 for (const VPtr &Vptr : getVTablePointers(RD))
2433 InitializeVTablePointer(Vptr);
Timur Iskhodzhanovb6487322013-10-09 18:16:58 +00002434
2435 if (RD->getNumVBases())
2436 CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD);
Anders Carlsson27da15b2010-01-01 20:29:01 +00002437}
Dan Gohman8fc50c22010-10-26 18:44:08 +00002438
John McCall7f416cc2015-09-08 08:05:57 +00002439llvm::Value *CodeGenFunction::GetVTablePtr(Address This,
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00002440 llvm::Type *VTableTy,
2441 const CXXRecordDecl *RD) {
2442 Address VTablePtrSrc = Builder.CreateElementBitCast(This, VTableTy);
Kostya Serebryany141e46f2012-03-26 17:03:51 +00002443 llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00002444 CGM.DecorateInstructionWithTBAA(VTable, CGM.getTBAAInfoForVTablePtr());
2445
2446 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2447 CGM.getCodeGenOpts().StrictVTablePointers)
2448 CGM.DecorateInstructionWithInvariantGroup(VTable, RD);
2449
Kostya Serebryany141e46f2012-03-26 17:03:51 +00002450 return VTable;
Dan Gohman8fc50c22010-10-26 18:44:08 +00002451}
Anders Carlssonc36783e2011-05-08 20:32:23 +00002452
Peter Collingbourned2926c92015-03-14 02:42:25 +00002453// If a class has a single non-virtual base and does not introduce or override
2454// virtual member functions or fields, it will have the same layout as its base.
2455// This function returns the least derived such class.
2456//
2457// Casting an instance of a base class to such a derived class is technically
2458// undefined behavior, but it is a relatively common hack for introducing member
2459// functions on class instances with specific properties (e.g. llvm::Operator)
2460// that works under most compilers and should not have security implications, so
2461// we allow it by default. It can be disabled with -fsanitize=cfi-cast-strict.
2462static const CXXRecordDecl *
2463LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD) {
2464 if (!RD->field_empty())
2465 return RD;
2466
2467 if (RD->getNumVBases() != 0)
2468 return RD;
2469
2470 if (RD->getNumBases() != 1)
2471 return RD;
2472
2473 for (const CXXMethodDecl *MD : RD->methods()) {
2474 if (MD->isVirtual()) {
2475 // Virtual member functions are only ok if they are implicit destructors
2476 // because the implicit destructor will have the same semantics as the
2477 // base class's destructor if no fields are added.
2478 if (isa<CXXDestructorDecl>(MD) && MD->isImplicit())
2479 continue;
2480 return RD;
2481 }
2482 }
2483
2484 return LeastDerivedClassWithSameLayout(
2485 RD->bases_begin()->getType()->getAsCXXRecordDecl());
2486}
2487
Peter Collingbourne1a7488a2015-04-02 00:23:30 +00002488void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXMethodDecl *MD,
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002489 llvm::Value *VTable,
2490 CFITypeCheckKind TCK,
2491 SourceLocation Loc) {
Peter Collingbourne1a7488a2015-04-02 00:23:30 +00002492 const CXXRecordDecl *ClassDecl = MD->getParent();
2493 if (!SanOpts.has(SanitizerKind::CFICastStrict))
2494 ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
2495
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002496 EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc);
Peter Collingbourne1a7488a2015-04-02 00:23:30 +00002497}
2498
Peter Collingbourned2926c92015-03-14 02:42:25 +00002499void CodeGenFunction::EmitVTablePtrCheckForCast(QualType T,
2500 llvm::Value *Derived,
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002501 bool MayBeNull,
2502 CFITypeCheckKind TCK,
2503 SourceLocation Loc) {
Peter Collingbourned2926c92015-03-14 02:42:25 +00002504 if (!getLangOpts().CPlusPlus)
2505 return;
2506
2507 auto *ClassTy = T->getAs<RecordType>();
2508 if (!ClassTy)
2509 return;
2510
2511 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassTy->getDecl());
2512
2513 if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass())
2514 return;
2515
Peter Collingbourned2926c92015-03-14 02:42:25 +00002516 if (!SanOpts.has(SanitizerKind::CFICastStrict))
2517 ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
2518
Hans Wennborgdcfba332015-10-06 23:40:43 +00002519 llvm::BasicBlock *ContBlock = nullptr;
Peter Collingbourned2926c92015-03-14 02:42:25 +00002520
2521 if (MayBeNull) {
2522 llvm::Value *DerivedNotNull =
2523 Builder.CreateIsNotNull(Derived, "cast.nonnull");
2524
2525 llvm::BasicBlock *CheckBlock = createBasicBlock("cast.check");
2526 ContBlock = createBasicBlock("cast.cont");
2527
2528 Builder.CreateCondBr(DerivedNotNull, CheckBlock, ContBlock);
2529
2530 EmitBlock(CheckBlock);
2531 }
2532
John McCall7f416cc2015-09-08 08:05:57 +00002533 llvm::Value *VTable =
Piotr Padlewski4b1ac722015-09-15 21:46:55 +00002534 GetVTablePtr(Address(Derived, getPointerAlign()), Int8PtrTy, ClassDecl);
2535
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002536 EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc);
Peter Collingbourned2926c92015-03-14 02:42:25 +00002537
2538 if (MayBeNull) {
2539 Builder.CreateBr(ContBlock);
2540 EmitBlock(ContBlock);
2541 }
2542}
2543
2544void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002545 llvm::Value *VTable,
2546 CFITypeCheckKind TCK,
2547 SourceLocation Loc) {
Peter Collingbournee5706442015-07-09 19:56:14 +00002548 if (CGM.IsCFIBlacklistedRecord(RD))
Peter Collingbournea4ccff32015-02-20 20:30:56 +00002549 return;
2550
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002551 SanitizerScope SanScope(this);
Peter Collingbournedc134532016-01-16 00:31:22 +00002552 llvm::SanitizerStatKind SSK;
2553 switch (TCK) {
2554 case CFITCK_VCall:
2555 SSK = llvm::SanStat_CFI_VCall;
2556 break;
2557 case CFITCK_NVCall:
2558 SSK = llvm::SanStat_CFI_NVCall;
2559 break;
2560 case CFITCK_DerivedCast:
2561 SSK = llvm::SanStat_CFI_DerivedCast;
2562 break;
2563 case CFITCK_UnrelatedCast:
2564 SSK = llvm::SanStat_CFI_UnrelatedCast;
2565 break;
Evgeniy Stepanov3fd61df2016-01-25 23:34:52 +00002566 case CFITCK_ICall:
2567 llvm_unreachable("not expecting CFITCK_ICall");
Peter Collingbournedc134532016-01-16 00:31:22 +00002568 }
2569 EmitSanitizerStatReport(SSK);
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002570
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +00002571 llvm::Metadata *MD =
2572 CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
2573 llvm::Value *BitSetName = llvm::MetadataAsValue::get(getLLVMContext(), MD);
Peter Collingbournea4ccff32015-02-20 20:30:56 +00002574
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002575 llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
2576 llvm::Value *BitSetTest =
2577 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::bitset_test),
2578 {CastedVTable, BitSetName});
Peter Collingbournea4ccff32015-02-20 20:30:56 +00002579
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002580 SanitizerMask M;
2581 switch (TCK) {
2582 case CFITCK_VCall:
2583 M = SanitizerKind::CFIVCall;
2584 break;
2585 case CFITCK_NVCall:
2586 M = SanitizerKind::CFINVCall;
2587 break;
2588 case CFITCK_DerivedCast:
2589 M = SanitizerKind::CFIDerivedCast;
2590 break;
2591 case CFITCK_UnrelatedCast:
2592 M = SanitizerKind::CFIUnrelatedCast;
2593 break;
Evgeniy Stepanov3fd61df2016-01-25 23:34:52 +00002594 case CFITCK_ICall:
2595 llvm_unreachable("not expecting CFITCK_ICall");
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002596 }
Peter Collingbournea4ccff32015-02-20 20:30:56 +00002597
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002598 llvm::Constant *StaticData[] = {
Evgeniy Stepanov3fd61df2016-01-25 23:34:52 +00002599 llvm::ConstantInt::get(Int8Ty, TCK),
Evgeniy Stepanovfd6f92d2015-12-15 23:00:20 +00002600 EmitCheckSourceLocation(Loc),
2601 EmitCheckTypeDescriptor(QualType(RD->getTypeForDecl(), 0)),
Peter Collingbourne6708c4a2015-06-19 01:51:54 +00002602 };
Evgeniy Stepanov3fd61df2016-01-25 23:34:52 +00002603
2604 auto TypeId = CGM.CreateCfiIdForTypeMetadata(MD);
2605 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && TypeId) {
2606 EmitCfiSlowPathCheck(M, BitSetTest, TypeId, CastedVTable, StaticData);
Evgeniy Stepanovf31ea302016-02-03 22:18:55 +00002607 return;
Evgeniy Stepanov3fd61df2016-01-25 23:34:52 +00002608 }
Evgeniy Stepanovf31ea302016-02-03 22:18:55 +00002609
2610 if (CGM.getCodeGenOpts().SanitizeTrap.has(M)) {
2611 EmitTrapCheck(BitSetTest);
2612 return;
2613 }
2614
2615 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
2616 CGM.getLLVMContext(),
2617 llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
2618 llvm::Value *ValidVtable =
2619 Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::bitset_test),
2620 {CastedVTable, AllVtables});
2621 EmitCheck(std::make_pair(BitSetTest, M), "cfi_check_fail", StaticData,
2622 {CastedVTable, ValidVtable});
Peter Collingbournea4ccff32015-02-20 20:30:56 +00002623}
Anders Carlssonc36783e2011-05-08 20:32:23 +00002624
2625// FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
2626// quite what we want.
2627static const Expr *skipNoOpCastsAndParens(const Expr *E) {
2628 while (true) {
2629 if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
2630 E = PE->getSubExpr();
2631 continue;
2632 }
2633
2634 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
2635 if (CE->getCastKind() == CK_NoOp) {
2636 E = CE->getSubExpr();
2637 continue;
2638 }
2639 }
2640 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
2641 if (UO->getOpcode() == UO_Extension) {
2642 E = UO->getSubExpr();
2643 continue;
2644 }
2645 }
2646 return E;
2647 }
2648}
2649
Benjamin Kramer7463ed72013-08-25 22:46:27 +00002650bool
2651CodeGenFunction::CanDevirtualizeMemberFunctionCall(const Expr *Base,
2652 const CXXMethodDecl *MD) {
2653 // When building with -fapple-kext, all calls must go through the vtable since
2654 // the kernel linker can do runtime patching of vtables.
2655 if (getLangOpts().AppleKext)
2656 return false;
2657
Anders Carlssonc36783e2011-05-08 20:32:23 +00002658 // If the most derived class is marked final, we know that no subclass can
2659 // override this member function and so we can devirtualize it. For example:
2660 //
2661 // struct A { virtual void f(); }
2662 // struct B final : A { };
2663 //
2664 // void f(B *b) {
2665 // b->f();
2666 // }
2667 //
Benjamin Kramer7463ed72013-08-25 22:46:27 +00002668 const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
Anders Carlssonc36783e2011-05-08 20:32:23 +00002669 if (MostDerivedClassDecl->hasAttr<FinalAttr>())
2670 return true;
2671
2672 // If the member function is marked 'final', we know that it can't be
2673 // overridden and can therefore devirtualize it.
2674 if (MD->hasAttr<FinalAttr>())
2675 return true;
2676
2677 // Similarly, if the class itself is marked 'final' it can't be overridden
2678 // and we can therefore devirtualize the member function call.
2679 if (MD->getParent()->hasAttr<FinalAttr>())
2680 return true;
2681
2682 Base = skipNoOpCastsAndParens(Base);
2683 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
2684 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
2685 // This is a record decl. We know the type and can devirtualize it.
2686 return VD->getType()->isRecordType();
2687 }
Justin Bogner1cd11f12015-05-20 15:53:59 +00002688
Anders Carlssonc36783e2011-05-08 20:32:23 +00002689 return false;
2690 }
Benjamin Kramer7463ed72013-08-25 22:46:27 +00002691
2692 // We can devirtualize calls on an object accessed by a class member access
2693 // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2694 // a derived class object constructed in the same location.
2695 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base))
2696 if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl()))
2697 return VD->getType()->isRecordType();
2698
Anders Carlssonc36783e2011-05-08 20:32:23 +00002699 // We can always devirtualize calls on temporary object expressions.
2700 if (isa<CXXConstructExpr>(Base))
2701 return true;
Justin Bogner1cd11f12015-05-20 15:53:59 +00002702
Anders Carlssonc36783e2011-05-08 20:32:23 +00002703 // And calls on bound temporaries.
2704 if (isa<CXXBindTemporaryExpr>(Base))
2705 return true;
Justin Bogner1cd11f12015-05-20 15:53:59 +00002706
Anders Carlssonc36783e2011-05-08 20:32:23 +00002707 // Check if this is a call expr that returns a record type.
2708 if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
David Majnemerced8bdf2015-02-25 17:36:15 +00002709 return CE->getCallReturnType(getContext())->isRecordType();
Anders Carlssonc36783e2011-05-08 20:32:23 +00002710
2711 // We can't devirtualize the call.
2712 return false;
2713}
2714
Faisal Vali571df122013-09-29 08:45:24 +00002715void CodeGenFunction::EmitForwardingCallToLambda(
2716 const CXXMethodDecl *callOperator,
2717 CallArgList &callArgs) {
Eli Friedman5b446882012-02-16 03:47:28 +00002718 // Get the address of the call operator.
John McCall8dda7b22012-07-07 06:41:13 +00002719 const CGFunctionInfo &calleeFnInfo =
2720 CGM.getTypes().arrangeCXXMethodDeclaration(callOperator);
2721 llvm::Value *callee =
2722 CGM.GetAddrOfFunction(GlobalDecl(callOperator),
2723 CGM.getTypes().GetFunctionType(calleeFnInfo));
Eli Friedman5b446882012-02-16 03:47:28 +00002724
John McCall8dda7b22012-07-07 06:41:13 +00002725 // Prepare the return slot.
2726 const FunctionProtoType *FPT =
2727 callOperator->getType()->castAs<FunctionProtoType>();
Alp Toker314cc812014-01-25 16:55:45 +00002728 QualType resultType = FPT->getReturnType();
John McCall8dda7b22012-07-07 06:41:13 +00002729 ReturnValueSlot returnSlot;
2730 if (!resultType->isVoidType() &&
2731 calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
John McCall47fb9502013-03-07 21:37:08 +00002732 !hasScalarEvaluationKind(calleeFnInfo.getReturnType()))
John McCall8dda7b22012-07-07 06:41:13 +00002733 returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified());
2734
2735 // We don't need to separately arrange the call arguments because
2736 // the call can't be variadic anyway --- it's impossible to forward
2737 // variadic arguments.
Justin Bogner1cd11f12015-05-20 15:53:59 +00002738
Eli Friedman5b446882012-02-16 03:47:28 +00002739 // Now emit our call.
John McCall8dda7b22012-07-07 06:41:13 +00002740 RValue RV = EmitCall(calleeFnInfo, callee, returnSlot,
2741 callArgs, callOperator);
Eli Friedman5b446882012-02-16 03:47:28 +00002742
John McCall8dda7b22012-07-07 06:41:13 +00002743 // If necessary, copy the returned value into the slot.
2744 if (!resultType->isVoidType() && returnSlot.isNull())
2745 EmitReturnOfRValue(RV, resultType);
Eli Friedmanf5f4d2f2012-12-13 23:37:17 +00002746 else
2747 EmitBranchThroughCleanup(ReturnBlock);
Eli Friedman5b446882012-02-16 03:47:28 +00002748}
2749
Eli Friedman2495ab02012-02-25 02:48:22 +00002750void CodeGenFunction::EmitLambdaBlockInvokeBody() {
2751 const BlockDecl *BD = BlockInfo->getBlockDecl();
2752 const VarDecl *variable = BD->capture_begin()->getVariable();
2753 const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
2754
2755 // Start building arguments for forwarding call
2756 CallArgList CallArgs;
2757
2758 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
John McCall7f416cc2015-09-08 08:05:57 +00002759 Address ThisPtr = GetAddrOfBlockDecl(variable, false);
2760 CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType);
Eli Friedman2495ab02012-02-25 02:48:22 +00002761
2762 // Add the rest of the parameters.
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00002763 for (auto param : BD->params())
Nick Lewycky2d84e842013-10-02 02:29:49 +00002764 EmitDelegateCallArg(CallArgs, param, param->getLocStart());
Aaron Ballmanb2b8b1d2014-03-07 16:09:59 +00002765
Justin Bogner1cd11f12015-05-20 15:53:59 +00002766 assert(!Lambda->isGenericLambda() &&
Faisal Vali571df122013-09-29 08:45:24 +00002767 "generic lambda interconversion to block not implemented");
2768 EmitForwardingCallToLambda(Lambda->getLambdaCallOperator(), CallArgs);
Eli Friedman2495ab02012-02-25 02:48:22 +00002769}
2770
2771void CodeGenFunction::EmitLambdaToBlockPointerBody(FunctionArgList &Args) {
John McCalldec348f72013-05-03 07:33:41 +00002772 if (cast<CXXMethodDecl>(CurCodeDecl)->isVariadic()) {
Eli Friedman2495ab02012-02-25 02:48:22 +00002773 // FIXME: Making this work correctly is nasty because it requires either
2774 // cloning the body of the call operator or making the call operator forward.
John McCalldec348f72013-05-03 07:33:41 +00002775 CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function");
Eli Friedman2495ab02012-02-25 02:48:22 +00002776 return;
2777 }
2778
Richard Smithb47c36f2013-11-05 09:12:18 +00002779 EmitFunctionBody(Args, cast<FunctionDecl>(CurGD.getDecl())->getBody());
Eli Friedman2495ab02012-02-25 02:48:22 +00002780}
2781
2782void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) {
2783 const CXXRecordDecl *Lambda = MD->getParent();
2784
2785 // Start building arguments for forwarding call
2786 CallArgList CallArgs;
2787
2788 QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2789 llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType));
2790 CallArgs.add(RValue::get(ThisPtr), ThisType);
2791
2792 // Add the rest of the parameters.
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002793 for (auto Param : MD->params())
2794 EmitDelegateCallArg(CallArgs, Param, Param->getLocStart());
2795
Faisal Vali571df122013-09-29 08:45:24 +00002796 const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
2797 // For a generic lambda, find the corresponding call operator specialization
2798 // to which the call to the static-invoker shall be forwarded.
2799 if (Lambda->isGenericLambda()) {
2800 assert(MD->isFunctionTemplateSpecialization());
2801 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
2802 FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate();
Craig Topper8a13c412014-05-21 05:09:00 +00002803 void *InsertPos = nullptr;
Justin Bogner1cd11f12015-05-20 15:53:59 +00002804 FunctionDecl *CorrespondingCallOpSpecialization =
Craig Topper7e0daca2014-06-26 04:58:53 +00002805 CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
Faisal Vali571df122013-09-29 08:45:24 +00002806 assert(CorrespondingCallOpSpecialization);
2807 CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
2808 }
2809 EmitForwardingCallToLambda(CallOp, CallArgs);
Eli Friedman2495ab02012-02-25 02:48:22 +00002810}
2811
Douglas Gregor355efbb2012-02-17 03:02:34 +00002812void CodeGenFunction::EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD) {
2813 if (MD->isVariadic()) {
Eli Friedman5b446882012-02-16 03:47:28 +00002814 // FIXME: Making this work correctly is nasty because it requires either
2815 // cloning the body of the call operator or making the call operator forward.
2816 CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
Eli Friedman2495ab02012-02-25 02:48:22 +00002817 return;
Eli Friedman5b446882012-02-16 03:47:28 +00002818 }
2819
Douglas Gregor355efbb2012-02-17 03:02:34 +00002820 EmitLambdaDelegatingInvokeBody(MD);
Eli Friedman5a6d5072012-02-16 01:37:33 +00002821}