clang  7.0.0svn
CGClass.cpp
Go to the documentation of this file.
1 //===--- CGClass.cpp - Emit LLVM Code for C++ classes -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code dealing with C++ code generation of classes
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGBlocks.h"
15 #include "CGCXXABI.h"
16 #include "CGDebugInfo.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenFunction.h"
20 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/RecordLayout.h"
23 #include "clang/AST/StmtCXX.h"
27 #include "llvm/IR/Intrinsics.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/Transforms/Utils/SanitizerStats.h"
30 
31 using namespace clang;
32 using namespace CodeGen;
33 
34 /// Return the best known alignment for an unknown pointer to a
35 /// particular class.
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.
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 
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
98  // under-aligned and then safely access its fields and vtables.
99  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 
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
128 Address
130  llvm::Value *memberPtr,
131  const MemberPointerType *memberPtrType,
132  LValueBaseInfo *BaseInfo,
133  TBAAAccessInfo *TBAAInfo) {
134  // Ask the ABI to compute the actual address.
135  llvm::Value *ptr =
136  CGM.getCXXABI().EmitMemberDataPointerAddress(*this, E, base,
137  memberPtr, memberPtrType);
138 
139  QualType memberType = memberPtrType->getPointeeType();
140  CharUnits memberAlign = getNaturalTypeAlignment(memberType, BaseInfo,
141  TBAAInfo);
142  memberAlign =
143  CGM.getDynamicOffsetAlignment(base.getAlignment(),
144  memberPtrType->getClass()->getAsCXXRecordDecl(),
145  memberAlign);
146  return Address(ptr, memberAlign);
147 }
148 
150  const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start,
153 
154  const ASTContext &Context = getContext();
155  const CXXRecordDecl *RD = DerivedClass;
156 
157  for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
158  const CXXBaseSpecifier *Base = *I;
159  assert(!Base->isVirtual() && "Should not see virtual bases here!");
160 
161  // Get the layout.
162  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
163 
164  const CXXRecordDecl *BaseDecl =
165  cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
166 
167  // Add the offset.
168  Offset += Layout.getBaseClassOffset(BaseDecl);
169 
170  RD = BaseDecl;
171  }
172 
173  return Offset;
174 }
175 
176 llvm::Constant *
180  assert(PathBegin != PathEnd && "Base path should not be empty!");
181 
182  CharUnits Offset =
183  computeNonVirtualBaseClassOffset(ClassDecl, PathBegin, PathEnd);
184  if (Offset.isZero())
185  return nullptr;
186 
188  Types.ConvertType(getContext().getPointerDiffType());
189 
190  return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
191 }
192 
193 /// Gets the address of a direct base class within a complete object.
194 /// This should only be used for (1) non-virtual bases or (2) virtual bases
195 /// when the type is known to be complete (e.g. in complete destructors).
196 ///
197 /// The object pointed to by 'This' is assumed to be non-null.
198 Address
200  const CXXRecordDecl *Derived,
201  const CXXRecordDecl *Base,
202  bool BaseIsVirtual) {
203  // 'this' must be a pointer (in some address space) to Derived.
204  assert(This.getElementType() == ConvertType(Derived));
205 
206  // Compute the offset of the virtual base.
208  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
209  if (BaseIsVirtual)
210  Offset = Layout.getVBaseClassOffset(Base);
211  else
212  Offset = Layout.getBaseClassOffset(Base);
213 
214  // Shift and cast down to the base type.
215  // TODO: for complete types, this should be possible with a GEP.
216  Address V = This;
217  if (!Offset.isZero()) {
218  V = Builder.CreateElementBitCast(V, Int8Ty);
219  V = Builder.CreateConstInBoundsByteGEP(V, Offset);
220  }
221  V = Builder.CreateElementBitCast(V, ConvertType(Base));
222 
223  return V;
224 }
225 
226 static Address
228  CharUnits nonVirtualOffset,
229  llvm::Value *virtualOffset,
230  const CXXRecordDecl *derivedClass,
231  const CXXRecordDecl *nearestVBase) {
232  // Assert that we have something to do.
233  assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
234 
235  // Compute the offset from the static and dynamic components.
236  llvm::Value *baseOffset;
237  if (!nonVirtualOffset.isZero()) {
238  baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy,
239  nonVirtualOffset.getQuantity());
240  if (virtualOffset) {
241  baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset);
242  }
243  } else {
244  baseOffset = virtualOffset;
245  }
246 
247  // Apply the base offset.
248  llvm::Value *ptr = addr.getPointer();
249  ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
250  ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr");
251 
252  // If we have a virtual component, the alignment of the result will
253  // be relative only to the known alignment of that vbase.
254  CharUnits alignment;
255  if (virtualOffset) {
256  assert(nearestVBase && "virtual offset without vbase?");
257  alignment = CGF.CGM.getVBaseAlignment(addr.getAlignment(),
258  derivedClass, nearestVBase);
259  } else {
260  alignment = addr.getAlignment();
261  }
262  alignment = alignment.alignmentAtOffset(nonVirtualOffset);
263 
264  return Address(ptr, alignment);
265 }
266 
268  Address Value, const CXXRecordDecl *Derived,
270  CastExpr::path_const_iterator PathEnd, bool NullCheckValue,
271  SourceLocation Loc) {
272  assert(PathBegin != PathEnd && "Base path should not be empty!");
273 
274  CastExpr::path_const_iterator Start = PathBegin;
275  const CXXRecordDecl *VBase = nullptr;
276 
277  // Sema has done some convenient canonicalization here: if the
278  // access path involved any virtual steps, the conversion path will
279  // *start* with a step down to the correct virtual base subobject,
280  // and hence will not require any further steps.
281  if ((*Start)->isVirtual()) {
282  VBase =
283  cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl());
284  ++Start;
285  }
286 
287  // Compute the static offset of the ultimate destination within its
288  // allocating subobject (the virtual base, if there is one, or else
289  // the "complete" object that we see).
290  CharUnits NonVirtualOffset = CGM.computeNonVirtualBaseClassOffset(
291  VBase ? VBase : Derived, Start, PathEnd);
292 
293  // If there's a virtual step, we can sometimes "devirtualize" it.
294  // For now, that's limited to when the derived type is final.
295  // TODO: "devirtualize" this for accesses to known-complete objects.
296  if (VBase && Derived->hasAttr<FinalAttr>()) {
297  const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived);
298  CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase);
299  NonVirtualOffset += vBaseOffset;
300  VBase = nullptr; // we no longer have a virtual step
301  }
302 
303  // Get the base pointer type.
304  llvm::Type *BasePtrTy =
305  ConvertType((PathEnd[-1])->getType())->getPointerTo();
306 
307  QualType DerivedTy = getContext().getRecordType(Derived);
308  CharUnits DerivedAlign = CGM.getClassPointerAlignment(Derived);
309 
310  // If the static offset is zero and we don't have a virtual step,
311  // just do a bitcast; null checks are unnecessary.
312  if (NonVirtualOffset.isZero() && !VBase) {
313  if (sanitizePerformTypeCheck()) {
314  SanitizerSet SkippedChecks;
315  SkippedChecks.set(SanitizerKind::Null, !NullCheckValue);
316  EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(),
317  DerivedTy, DerivedAlign, SkippedChecks);
318  }
319  return Builder.CreateBitCast(Value, BasePtrTy);
320  }
321 
322  llvm::BasicBlock *origBB = nullptr;
323  llvm::BasicBlock *endBB = nullptr;
324 
325  // Skip over the offset (and the vtable load) if we're supposed to
326  // null-check the pointer.
327  if (NullCheckValue) {
328  origBB = Builder.GetInsertBlock();
329  llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull");
330  endBB = createBasicBlock("cast.end");
331 
332  llvm::Value *isNull = Builder.CreateIsNull(Value.getPointer());
333  Builder.CreateCondBr(isNull, endBB, notNullBB);
334  EmitBlock(notNullBB);
335  }
336 
337  if (sanitizePerformTypeCheck()) {
338  SanitizerSet SkippedChecks;
339  SkippedChecks.set(SanitizerKind::Null, true);
340  EmitTypeCheck(VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc,
341  Value.getPointer(), DerivedTy, DerivedAlign, SkippedChecks);
342  }
343 
344  // Compute the virtual offset.
345  llvm::Value *VirtualOffset = nullptr;
346  if (VBase) {
347  VirtualOffset =
348  CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase);
349  }
350 
351  // Apply both offsets.
352  Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset,
353  VirtualOffset, Derived, VBase);
354 
355  // Cast to the destination type.
356  Value = Builder.CreateBitCast(Value, BasePtrTy);
357 
358  // Build a phi if we needed a null check.
359  if (NullCheckValue) {
360  llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
361  Builder.CreateBr(endBB);
362  EmitBlock(endBB);
363 
364  llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result");
365  PHI->addIncoming(Value.getPointer(), notNullBB);
366  PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB);
367  Value = Address(PHI, Value.getAlignment());
368  }
369 
370  return Value;
371 }
372 
373 Address
375  const CXXRecordDecl *Derived,
378  bool NullCheckValue) {
379  assert(PathBegin != PathEnd && "Base path should not be empty!");
380 
381  QualType DerivedTy =
382  getContext().getCanonicalType(getContext().getTagDeclType(Derived));
383  llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
384 
385  llvm::Value *NonVirtualOffset =
386  CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
387 
388  if (!NonVirtualOffset) {
389  // No offset, we can just cast back.
390  return Builder.CreateBitCast(BaseAddr, DerivedPtrTy);
391  }
392 
393  llvm::BasicBlock *CastNull = nullptr;
394  llvm::BasicBlock *CastNotNull = nullptr;
395  llvm::BasicBlock *CastEnd = nullptr;
396 
397  if (NullCheckValue) {
398  CastNull = createBasicBlock("cast.null");
399  CastNotNull = createBasicBlock("cast.notnull");
400  CastEnd = createBasicBlock("cast.end");
401 
402  llvm::Value *IsNull = Builder.CreateIsNull(BaseAddr.getPointer());
403  Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
404  EmitBlock(CastNotNull);
405  }
406 
407  // Apply the offset.
408  llvm::Value *Value = Builder.CreateBitCast(BaseAddr.getPointer(), Int8PtrTy);
409  Value = Builder.CreateInBoundsGEP(Value, Builder.CreateNeg(NonVirtualOffset),
410  "sub.ptr");
411 
412  // Just cast.
413  Value = Builder.CreateBitCast(Value, DerivedPtrTy);
414 
415  // Produce a PHI if we had a null-check.
416  if (NullCheckValue) {
417  Builder.CreateBr(CastEnd);
418  EmitBlock(CastNull);
419  Builder.CreateBr(CastEnd);
420  EmitBlock(CastEnd);
421 
422  llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
423  PHI->addIncoming(Value, CastNotNull);
424  PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull);
425  Value = PHI;
426  }
427 
428  return Address(Value, CGM.getClassPointerAlignment(Derived));
429 }
430 
432  bool ForVirtualBase,
433  bool Delegating) {
434  if (!CGM.getCXXABI().NeedsVTTParameter(GD)) {
435  // This constructor/destructor does not need a VTT parameter.
436  return nullptr;
437  }
438 
439  const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent();
440  const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
441 
442  llvm::Value *VTT;
443 
444  uint64_t SubVTTIndex;
445 
446  if (Delegating) {
447  // If this is a delegating constructor call, just load the VTT.
448  return LoadCXXVTT();
449  } else if (RD == Base) {
450  // If the record matches the base, this is the complete ctor/dtor
451  // variant calling the base variant in a class with virtual bases.
452  assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) &&
453  "doing no-op VTT offset in base dtor/ctor?");
454  assert(!ForVirtualBase && "Can't have same class as virtual base!");
455  SubVTTIndex = 0;
456  } else {
457  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
458  CharUnits BaseOffset = ForVirtualBase ?
459  Layout.getVBaseClassOffset(Base) :
460  Layout.getBaseClassOffset(Base);
461 
462  SubVTTIndex =
463  CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
464  assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
465  }
466 
467  if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) {
468  // A VTT parameter was passed to the constructor, use it.
469  VTT = LoadCXXVTT();
470  VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex);
471  } else {
472  // We're the complete constructor, so get the VTT by name.
473  VTT = CGM.getVTables().GetAddrOfVTT(RD);
474  VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex);
475  }
476 
477  return VTT;
478 }
479 
480 namespace {
481  /// Call the destructor for a direct base class.
482  struct CallBaseDtor final : EHScopeStack::Cleanup {
483  const CXXRecordDecl *BaseClass;
484  bool BaseIsVirtual;
485  CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
486  : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
487 
488  void Emit(CodeGenFunction &CGF, Flags flags) override {
489  const CXXRecordDecl *DerivedClass =
490  cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
491 
492  const CXXDestructorDecl *D = BaseClass->getDestructor();
493  Address Addr =
495  DerivedClass, BaseClass,
496  BaseIsVirtual);
497  CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual,
498  /*Delegating=*/false, Addr);
499  }
500  };
501 
502  /// A visitor which checks whether an initializer uses 'this' in a
503  /// way which requires the vtable to be properly set.
504  struct DynamicThisUseChecker : ConstEvaluatedExprVisitor<DynamicThisUseChecker> {
506 
507  bool UsesThis;
508 
509  DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {}
510 
511  // Black-list all explicit and implicit references to 'this'.
512  //
513  // Do we need to worry about external references to 'this' derived
514  // from arbitrary code? If so, then anything which runs arbitrary
515  // external code might potentially access the vtable.
516  void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; }
517  };
518 } // end anonymous namespace
519 
520 static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
521  DynamicThisUseChecker Checker(C);
522  Checker.Visit(Init);
523  return Checker.UsesThis;
524 }
525 
527  const CXXRecordDecl *ClassDecl,
528  CXXCtorInitializer *BaseInit,
529  CXXCtorType CtorType) {
530  assert(BaseInit->isBaseInitializer() &&
531  "Must have base initializer!");
532 
533  Address ThisPtr = CGF.LoadCXXThisAddress();
534 
535  const Type *BaseType = BaseInit->getBaseClass();
536  CXXRecordDecl *BaseClassDecl =
537  cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
538 
539  bool isBaseVirtual = BaseInit->isBaseVirtual();
540 
541  // The base constructor doesn't construct virtual bases.
542  if (CtorType == Ctor_Base && isBaseVirtual)
543  return;
544 
545  // If the initializer for the base (other than the constructor
546  // itself) accesses 'this' in any way, we need to initialize the
547  // vtables.
548  if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
549  CGF.InitializeVTablePointers(ClassDecl);
550 
551  // We can pretend to be a complete class because it only matters for
552  // virtual bases, and we only do virtual bases for complete ctors.
553  Address V =
554  CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
555  BaseClassDecl,
556  isBaseVirtual);
557  AggValueSlot AggSlot =
559  V, Qualifiers(),
563  CGF.overlapForBaseInit(ClassDecl, BaseClassDecl, isBaseVirtual));
564 
565  CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
566 
567  if (CGF.CGM.getLangOpts().Exceptions &&
568  !BaseClassDecl->hasTrivialDestructor())
569  CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
570  isBaseVirtual);
571 }
572 
574  auto *CD = dyn_cast<CXXConstructorDecl>(D);
575  if (!(CD && CD->isCopyOrMoveConstructor()) &&
577  return false;
578 
579  // We can emit a memcpy for a trivial copy or move constructor/assignment.
580  if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding())
581  return true;
582 
583  // We *must* emit a memcpy for a defaulted union copy or move op.
584  if (D->getParent()->isUnion() && D->isDefaulted())
585  return true;
586 
587  return false;
588 }
589 
591  CXXCtorInitializer *MemberInit,
592  LValue &LHS) {
593  FieldDecl *Field = MemberInit->getAnyMember();
594  if (MemberInit->isIndirectMemberInitializer()) {
595  // If we are initializing an anonymous union field, drill down to the field.
596  IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
597  for (const auto *I : IndirectField->chain())
598  LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I));
599  } else {
600  LHS = CGF.EmitLValueForFieldInitialization(LHS, Field);
601  }
602 }
603 
605  const CXXRecordDecl *ClassDecl,
606  CXXCtorInitializer *MemberInit,
607  const CXXConstructorDecl *Constructor,
608  FunctionArgList &Args) {
609  ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation());
610  assert(MemberInit->isAnyMemberInitializer() &&
611  "Must have member initializer!");
612  assert(MemberInit->getInit() && "Must have initializer!");
613 
614  // non-static data member initializers.
615  FieldDecl *Field = MemberInit->getAnyMember();
616  QualType FieldType = Field->getType();
617 
618  llvm::Value *ThisPtr = CGF.LoadCXXThis();
619  QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
620  LValue LHS;
621 
622  // If a base constructor is being emitted, create an LValue that has the
623  // non-virtual alignment.
624  if (CGF.CurGD.getCtorType() == Ctor_Base)
625  LHS = CGF.MakeNaturalAlignPointeeAddrLValue(ThisPtr, RecordTy);
626  else
627  LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
628 
629  EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS);
630 
631  // Special case: if we are in a copy or move constructor, and we are copying
632  // an array of PODs or classes with trivial copy constructors, ignore the
633  // AST and perform the copy we know is equivalent.
634  // FIXME: This is hacky at best... if we had a bit more explicit information
635  // in the AST, we could generalize it more easily.
636  const ConstantArrayType *Array
637  = CGF.getContext().getAsConstantArrayType(FieldType);
638  if (Array && Constructor->isDefaulted() &&
639  Constructor->isCopyOrMoveConstructor()) {
640  QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
641  CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
642  if (BaseElementTy.isPODType(CGF.getContext()) ||
644  unsigned SrcArgIndex =
645  CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args);
646  llvm::Value *SrcPtr
647  = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
648  LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
649  LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field);
650 
651  // Copy the aggregate.
652  CGF.EmitAggregateCopy(LHS, Src, FieldType, CGF.overlapForFieldInit(Field),
653  LHS.isVolatileQualified());
654  // Ensure that we destroy the objects if an exception is thrown later in
655  // the constructor.
656  QualType::DestructionKind dtorKind = FieldType.isDestructedType();
657  if (CGF.needsEHCleanup(dtorKind))
658  CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
659  return;
660  }
661  }
662 
663  CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit());
664 }
665 
667  Expr *Init) {
668  QualType FieldType = Field->getType();
669  switch (getEvaluationKind(FieldType)) {
670  case TEK_Scalar:
671  if (LHS.isSimple()) {
672  EmitExprAsInit(Init, Field, LHS, false);
673  } else {
674  RValue RHS = RValue::get(EmitScalarExpr(Init));
675  EmitStoreThroughLValue(RHS, LHS);
676  }
677  break;
678  case TEK_Complex:
679  EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true);
680  break;
681  case TEK_Aggregate: {
682  AggValueSlot Slot =
684  LHS,
688  overlapForFieldInit(Field));
689  EmitAggExpr(Init, Slot);
690  break;
691  }
692  }
693 
694  // Ensure that we destroy this object if an exception is thrown
695  // later in the constructor.
696  QualType::DestructionKind dtorKind = FieldType.isDestructedType();
697  if (needsEHCleanup(dtorKind))
698  pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
699 }
700 
701 /// Checks whether the given constructor is a valid subject for the
702 /// complete-to-base constructor delegation optimization, i.e.
703 /// emitting the complete constructor as a simple call to the base
704 /// constructor.
706  const CXXConstructorDecl *Ctor) {
707 
708  // Currently we disable the optimization for classes with virtual
709  // bases because (1) the addresses of parameter variables need to be
710  // consistent across all initializers but (2) the delegate function
711  // call necessarily creates a second copy of the parameter variable.
712  //
713  // The limiting example (purely theoretical AFAIK):
714  // struct A { A(int &c) { c++; } };
715  // struct B : virtual A {
716  // B(int count) : A(count) { printf("%d\n", count); }
717  // };
718  // ...although even this example could in principle be emitted as a
719  // delegation since the address of the parameter doesn't escape.
720  if (Ctor->getParent()->getNumVBases()) {
721  // TODO: white-list trivial vbase initializers. This case wouldn't
722  // be subject to the restrictions below.
723 
724  // TODO: white-list cases where:
725  // - there are no non-reference parameters to the constructor
726  // - the initializers don't access any non-reference parameters
727  // - the initializers don't take the address of non-reference
728  // parameters
729  // - etc.
730  // If we ever add any of the above cases, remember that:
731  // - function-try-blocks will always blacklist this optimization
732  // - we need to perform the constructor prologue and cleanup in
733  // EmitConstructorBody.
734 
735  return false;
736  }
737 
738  // We also disable the optimization for variadic functions because
739  // it's impossible to "re-pass" varargs.
740  if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic())
741  return false;
742 
743  // FIXME: Decide if we can do a delegation of a delegating constructor.
744  if (Ctor->isDelegatingConstructor())
745  return false;
746 
747  return true;
748 }
749 
750 // Emit code in ctor (Prologue==true) or dtor (Prologue==false)
751 // to poison the extra field paddings inserted under
752 // -fsanitize-address-field-padding=1|2.
754  ASTContext &Context = getContext();
755  const CXXRecordDecl *ClassDecl =
756  Prologue ? cast<CXXConstructorDecl>(CurGD.getDecl())->getParent()
757  : cast<CXXDestructorDecl>(CurGD.getDecl())->getParent();
758  if (!ClassDecl->mayInsertExtraPadding()) return;
759 
760  struct SizeAndOffset {
761  uint64_t Size;
762  uint64_t Offset;
763  };
764 
765  unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits();
766  const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl);
767 
768  // Populate sizes and offsets of fields.
770  for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i)
771  SSV[i].Offset =
772  Context.toCharUnitsFromBits(Info.getFieldOffset(i)).getQuantity();
773 
774  size_t NumFields = 0;
775  for (const auto *Field : ClassDecl->fields()) {
776  const FieldDecl *D = Field;
777  std::pair<CharUnits, CharUnits> FieldInfo =
778  Context.getTypeInfoInChars(D->getType());
779  CharUnits FieldSize = FieldInfo.first;
780  assert(NumFields < SSV.size());
781  SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity();
782  NumFields++;
783  }
784  assert(NumFields == SSV.size());
785  if (SSV.size() <= 1) return;
786 
787  // We will insert calls to __asan_* run-time functions.
788  // LLVM AddressSanitizer pass may decide to inline them later.
789  llvm::Type *Args[2] = {IntPtrTy, IntPtrTy};
790  llvm::FunctionType *FTy =
791  llvm::FunctionType::get(CGM.VoidTy, Args, false);
792  llvm::Constant *F = CGM.CreateRuntimeFunction(
793  FTy, Prologue ? "__asan_poison_intra_object_redzone"
794  : "__asan_unpoison_intra_object_redzone");
795 
796  llvm::Value *ThisPtr = LoadCXXThis();
797  ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy);
798  uint64_t TypeSize = Info.getNonVirtualSize().getQuantity();
799  // For each field check if it has sufficient padding,
800  // if so (un)poison it with a call.
801  for (size_t i = 0; i < SSV.size(); i++) {
802  uint64_t AsanAlignment = 8;
803  uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset;
804  uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size;
805  uint64_t EndOffset = SSV[i].Offset + SSV[i].Size;
806  if (PoisonSize < AsanAlignment || !SSV[i].Size ||
807  (NextField % AsanAlignment) != 0)
808  continue;
809  Builder.CreateCall(
810  F, {Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)),
811  Builder.getIntN(PtrSize, PoisonSize)});
812  }
813 }
814 
815 /// EmitConstructorBody - Emits the body of the current constructor.
817  EmitAsanPrologueOrEpilogue(true);
818  const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
819  CXXCtorType CtorType = CurGD.getCtorType();
820 
821  assert((CGM.getTarget().getCXXABI().hasConstructorVariants() ||
822  CtorType == Ctor_Complete) &&
823  "can only generate complete ctor for this ABI");
824 
825  // Before we go any further, try the complete->base constructor
826  // delegation optimization.
827  if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
828  CGM.getTarget().getCXXABI().hasConstructorVariants()) {
829  EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getLocEnd());
830  return;
831  }
832 
833  const FunctionDecl *Definition = nullptr;
834  Stmt *Body = Ctor->getBody(Definition);
835  assert(Definition == Ctor && "emitting wrong constructor body");
836 
837  // Enter the function-try-block before the constructor prologue if
838  // applicable.
839  bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
840  if (IsTryBody)
841  EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
842 
843  incrementProfileCounter(Body);
844 
845  RunCleanupsScope RunCleanups(*this);
846 
847  // TODO: in restricted cases, we can emit the vbase initializers of
848  // a complete ctor and then delegate to the base ctor.
849 
850  // Emit the constructor prologue, i.e. the base and member
851  // initializers.
852  EmitCtorPrologue(Ctor, CtorType, Args);
853 
854  // Emit the body of the statement.
855  if (IsTryBody)
856  EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
857  else if (Body)
858  EmitStmt(Body);
859 
860  // Emit any cleanup blocks associated with the member or base
861  // initializers, which includes (along the exceptional path) the
862  // destructors for those members and bases that were fully
863  // constructed.
864  RunCleanups.ForceCleanup();
865 
866  if (IsTryBody)
867  ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
868 }
869 
870 namespace {
871  /// RAII object to indicate that codegen is copying the value representation
872  /// instead of the object representation. Useful when copying a struct or
873  /// class which has uninitialized members and we're only performing
874  /// lvalue-to-rvalue conversion on the object but not its members.
875  class CopyingValueRepresentation {
876  public:
877  explicit CopyingValueRepresentation(CodeGenFunction &CGF)
878  : CGF(CGF), OldSanOpts(CGF.SanOpts) {
879  CGF.SanOpts.set(SanitizerKind::Bool, false);
880  CGF.SanOpts.set(SanitizerKind::Enum, false);
881  }
882  ~CopyingValueRepresentation() {
883  CGF.SanOpts = OldSanOpts;
884  }
885  private:
886  CodeGenFunction &CGF;
887  SanitizerSet OldSanOpts;
888  };
889 } // end anonymous namespace
890 
891 namespace {
892  class FieldMemcpyizer {
893  public:
894  FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl,
895  const VarDecl *SrcRec)
896  : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec),
897  RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)),
898  FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0),
899  LastFieldOffset(0), LastAddedFieldIndex(0) {}
900 
901  bool isMemcpyableField(FieldDecl *F) const {
902  // Never memcpy fields when we are adding poisoned paddings.
903  if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding)
904  return false;
905  Qualifiers Qual = F->getType().getQualifiers();
906  if (Qual.hasVolatile() || Qual.hasObjCLifetime())
907  return false;
908  return true;
909  }
910 
911  void addMemcpyableField(FieldDecl *F) {
912  if (!FirstField)
913  addInitialField(F);
914  else
915  addNextField(F);
916  }
917 
918  CharUnits getMemcpySize(uint64_t FirstByteOffset) const {
919  ASTContext &Ctx = CGF.getContext();
920  unsigned LastFieldSize =
921  LastField->isBitField()
922  ? LastField->getBitWidthValue(Ctx)
923  : Ctx.toBits(
924  Ctx.getTypeInfoDataSizeInChars(LastField->getType()).first);
925  uint64_t MemcpySizeBits = LastFieldOffset + LastFieldSize -
926  FirstByteOffset + Ctx.getCharWidth() - 1;
927  CharUnits MemcpySize = Ctx.toCharUnitsFromBits(MemcpySizeBits);
928  return MemcpySize;
929  }
930 
931  void emitMemcpy() {
932  // Give the subclass a chance to bail out if it feels the memcpy isn't
933  // worth it (e.g. Hasn't aggregated enough data).
934  if (!FirstField) {
935  return;
936  }
937 
938  uint64_t FirstByteOffset;
939  if (FirstField->isBitField()) {
940  const CGRecordLayout &RL =
941  CGF.getTypes().getCGRecordLayout(FirstField->getParent());
942  const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField);
943  // FirstFieldOffset is not appropriate for bitfields,
944  // we need to use the storage offset instead.
945  FirstByteOffset = CGF.getContext().toBits(BFInfo.StorageOffset);
946  } else {
947  FirstByteOffset = FirstFieldOffset;
948  }
949 
950  CharUnits MemcpySize = getMemcpySize(FirstByteOffset);
951  QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
952  Address ThisPtr = CGF.LoadCXXThisAddress();
953  LValue DestLV = CGF.MakeAddrLValue(ThisPtr, RecordTy);
954  LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField);
955  llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec));
956  LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
957  LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField);
958 
959  emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddress() : Dest.getAddress(),
960  Src.isBitField() ? Src.getBitFieldAddress() : Src.getAddress(),
961  MemcpySize);
962  reset();
963  }
964 
965  void reset() {
966  FirstField = nullptr;
967  }
968 
969  protected:
970  CodeGenFunction &CGF;
971  const CXXRecordDecl *ClassDecl;
972 
973  private:
974  void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) {
975  llvm::PointerType *DPT = DestPtr.getType();
976  llvm::Type *DBP =
977  llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace());
978  DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP);
979 
980  llvm::PointerType *SPT = SrcPtr.getType();
981  llvm::Type *SBP =
982  llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace());
983  SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP);
984 
985  CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity());
986  }
987 
988  void addInitialField(FieldDecl *F) {
989  FirstField = F;
990  LastField = F;
991  FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex());
992  LastFieldOffset = FirstFieldOffset;
993  LastAddedFieldIndex = F->getFieldIndex();
994  }
995 
996  void addNextField(FieldDecl *F) {
997  // For the most part, the following invariant will hold:
998  // F->getFieldIndex() == LastAddedFieldIndex + 1
999  // The one exception is that Sema won't add a copy-initializer for an
1000  // unnamed bitfield, which will show up here as a gap in the sequence.
1001  assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 &&
1002  "Cannot aggregate fields out of order.");
1003  LastAddedFieldIndex = F->getFieldIndex();
1004 
1005  // The 'first' and 'last' fields are chosen by offset, rather than field
1006  // index. This allows the code to support bitfields, as well as regular
1007  // fields.
1008  uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex());
1009  if (FOffset < FirstFieldOffset) {
1010  FirstField = F;
1011  FirstFieldOffset = FOffset;
1012  } else if (FOffset > LastFieldOffset) {
1013  LastField = F;
1014  LastFieldOffset = FOffset;
1015  }
1016  }
1017 
1018  const VarDecl *SrcRec;
1019  const ASTRecordLayout &RecLayout;
1020  FieldDecl *FirstField;
1021  FieldDecl *LastField;
1022  uint64_t FirstFieldOffset, LastFieldOffset;
1023  unsigned LastAddedFieldIndex;
1024  };
1025 
1026  class ConstructorMemcpyizer : public FieldMemcpyizer {
1027  private:
1028  /// Get source argument for copy constructor. Returns null if not a copy
1029  /// constructor.
1030  static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF,
1031  const CXXConstructorDecl *CD,
1032  FunctionArgList &Args) {
1033  if (CD->isCopyOrMoveConstructor() && CD->isDefaulted())
1034  return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)];
1035  return nullptr;
1036  }
1037 
1038  // Returns true if a CXXCtorInitializer represents a member initialization
1039  // that can be rolled into a memcpy.
1040  bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
1041  if (!MemcpyableCtor)
1042  return false;
1043  FieldDecl *Field = MemberInit->getMember();
1044  assert(Field && "No field for member init.");
1045  QualType FieldType = Field->getType();
1046  CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
1047 
1048  // Bail out on non-memcpyable, not-trivially-copyable members.
1049  if (!(CE && isMemcpyEquivalentSpecialMember(CE->getConstructor())) &&
1050  !(FieldType.isTriviallyCopyableType(CGF.getContext()) ||
1051  FieldType->isReferenceType()))
1052  return false;
1053 
1054  // Bail out on volatile fields.
1055  if (!isMemcpyableField(Field))
1056  return false;
1057 
1058  // Otherwise we're good.
1059  return true;
1060  }
1061 
1062  public:
1063  ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD,
1064  FunctionArgList &Args)
1065  : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)),
1066  ConstructorDecl(CD),
1067  MemcpyableCtor(CD->isDefaulted() &&
1068  CD->isCopyOrMoveConstructor() &&
1069  CGF.getLangOpts().getGC() == LangOptions::NonGC),
1070  Args(Args) { }
1071 
1072  void addMemberInitializer(CXXCtorInitializer *MemberInit) {
1073  if (isMemberInitMemcpyable(MemberInit)) {
1074  AggregatedInits.push_back(MemberInit);
1075  addMemcpyableField(MemberInit->getMember());
1076  } else {
1077  emitAggregatedInits();
1078  EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit,
1079  ConstructorDecl, Args);
1080  }
1081  }
1082 
1083  void emitAggregatedInits() {
1084  if (AggregatedInits.size() <= 1) {
1085  // This memcpy is too small to be worthwhile. Fall back on default
1086  // codegen.
1087  if (!AggregatedInits.empty()) {
1088  CopyingValueRepresentation CVR(CGF);
1089  EmitMemberInitializer(CGF, ConstructorDecl->getParent(),
1090  AggregatedInits[0], ConstructorDecl, Args);
1091  AggregatedInits.clear();
1092  }
1093  reset();
1094  return;
1095  }
1096 
1097  pushEHDestructors();
1098  emitMemcpy();
1099  AggregatedInits.clear();
1100  }
1101 
1102  void pushEHDestructors() {
1103  Address ThisPtr = CGF.LoadCXXThisAddress();
1104  QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
1105  LValue LHS = CGF.MakeAddrLValue(ThisPtr, RecordTy);
1106 
1107  for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
1108  CXXCtorInitializer *MemberInit = AggregatedInits[i];
1109  QualType FieldType = MemberInit->getAnyMember()->getType();
1110  QualType::DestructionKind dtorKind = FieldType.isDestructedType();
1111  if (!CGF.needsEHCleanup(dtorKind))
1112  continue;
1113  LValue FieldLHS = LHS;
1114  EmitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS);
1115  CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(), FieldType);
1116  }
1117  }
1118 
1119  void finish() {
1120  emitAggregatedInits();
1121  }
1122 
1123  private:
1124  const CXXConstructorDecl *ConstructorDecl;
1125  bool MemcpyableCtor;
1126  FunctionArgList &Args;
1127  SmallVector<CXXCtorInitializer*, 16> AggregatedInits;
1128  };
1129 
1130  class AssignmentMemcpyizer : public FieldMemcpyizer {
1131  private:
1132  // Returns the memcpyable field copied by the given statement, if one
1133  // exists. Otherwise returns null.
1134  FieldDecl *getMemcpyableField(Stmt *S) {
1135  if (!AssignmentsMemcpyable)
1136  return nullptr;
1137  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
1138  // Recognise trivial assignments.
1139  if (BO->getOpcode() != BO_Assign)
1140  return nullptr;
1141  MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS());
1142  if (!ME)
1143  return nullptr;
1144  FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1145  if (!Field || !isMemcpyableField(Field))
1146  return nullptr;
1147  Stmt *RHS = BO->getRHS();
1148  if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS))
1149  RHS = EC->getSubExpr();
1150  if (!RHS)
1151  return nullptr;
1152  if (MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS)) {
1153  if (ME2->getMemberDecl() == Field)
1154  return Field;
1155  }
1156  return nullptr;
1157  } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) {
1158  CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl());
1159  if (!(MD && isMemcpyEquivalentSpecialMember(MD)))
1160  return nullptr;
1161  MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument());
1162  if (!IOA)
1163  return nullptr;
1164  FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl());
1165  if (!Field || !isMemcpyableField(Field))
1166  return nullptr;
1167  MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0));
1168  if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl()))
1169  return nullptr;
1170  return Field;
1171  } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
1172  FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1173  if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
1174  return nullptr;
1175  Expr *DstPtr = CE->getArg(0);
1176  if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr))
1177  DstPtr = DC->getSubExpr();
1178  UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr);
1179  if (!DUO || DUO->getOpcode() != UO_AddrOf)
1180  return nullptr;
1181  MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr());
1182  if (!ME)
1183  return nullptr;
1184  FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1185  if (!Field || !isMemcpyableField(Field))
1186  return nullptr;
1187  Expr *SrcPtr = CE->getArg(1);
1188  if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr))
1189  SrcPtr = SC->getSubExpr();
1190  UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr);
1191  if (!SUO || SUO->getOpcode() != UO_AddrOf)
1192  return nullptr;
1193  MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr());
1194  if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl()))
1195  return nullptr;
1196  return Field;
1197  }
1198 
1199  return nullptr;
1200  }
1201 
1202  bool AssignmentsMemcpyable;
1203  SmallVector<Stmt*, 16> AggregatedStmts;
1204 
1205  public:
1206  AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD,
1207  FunctionArgList &Args)
1208  : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
1209  AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
1210  assert(Args.size() == 2);
1211  }
1212 
1213  void emitAssignment(Stmt *S) {
1214  FieldDecl *F = getMemcpyableField(S);
1215  if (F) {
1216  addMemcpyableField(F);
1217  AggregatedStmts.push_back(S);
1218  } else {
1219  emitAggregatedStmts();
1220  CGF.EmitStmt(S);
1221  }
1222  }
1223 
1224  void emitAggregatedStmts() {
1225  if (AggregatedStmts.size() <= 1) {
1226  if (!AggregatedStmts.empty()) {
1227  CopyingValueRepresentation CVR(CGF);
1228  CGF.EmitStmt(AggregatedStmts[0]);
1229  }
1230  reset();
1231  }
1232 
1233  emitMemcpy();
1234  AggregatedStmts.clear();
1235  }
1236 
1237  void finish() {
1238  emitAggregatedStmts();
1239  }
1240  };
1241 } // end anonymous namespace
1242 
1243 static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit) {
1244  const Type *BaseType = BaseInit->getBaseClass();
1245  const auto *BaseClassDecl =
1246  cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
1247  return BaseClassDecl->isDynamicClass();
1248 }
1249 
1250 /// EmitCtorPrologue - This routine generates necessary code to initialize
1251 /// base classes and non-static data members belonging to this constructor.
1253  CXXCtorType CtorType,
1254  FunctionArgList &Args) {
1255  if (CD->isDelegatingConstructor())
1256  return EmitDelegatingCXXConstructorCall(CD, Args);
1257 
1258  const CXXRecordDecl *ClassDecl = CD->getParent();
1259 
1261  E = CD->init_end();
1262 
1263  llvm::BasicBlock *BaseCtorContinueBB = nullptr;
1264  if (ClassDecl->getNumVBases() &&
1265  !CGM.getTarget().getCXXABI().hasConstructorVariants()) {
1266  // The ABIs that don't have constructor variants need to put a branch
1267  // before the virtual base initialization code.
1268  BaseCtorContinueBB =
1269  CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl);
1270  assert(BaseCtorContinueBB);
1271  }
1272 
1273  llvm::Value *const OldThis = CXXThisValue;
1274  // Virtual base initializers first.
1275  for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) {
1276  if (CGM.getCodeGenOpts().StrictVTablePointers &&
1277  CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1279  CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis());
1280  EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1281  }
1282 
1283  if (BaseCtorContinueBB) {
1284  // Complete object handler should continue to the remaining initializers.
1285  Builder.CreateBr(BaseCtorContinueBB);
1286  EmitBlock(BaseCtorContinueBB);
1287  }
1288 
1289  // Then, non-virtual base initializers.
1290  for (; B != E && (*B)->isBaseInitializer(); B++) {
1291  assert(!(*B)->isBaseVirtual());
1292 
1293  if (CGM.getCodeGenOpts().StrictVTablePointers &&
1294  CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1296  CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis());
1297  EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1298  }
1299 
1300  CXXThisValue = OldThis;
1301 
1302  InitializeVTablePointers(ClassDecl);
1303 
1304  // And finally, initialize class members.
1305  FieldConstructionScope FCS(*this, LoadCXXThisAddress());
1306  ConstructorMemcpyizer CM(*this, CD, Args);
1307  for (; B != E; B++) {
1308  CXXCtorInitializer *Member = (*B);
1309  assert(!Member->isBaseInitializer());
1310  assert(Member->isAnyMemberInitializer() &&
1311  "Delegating initializer on non-delegating constructor");
1312  CM.addMemberInitializer(Member);
1313  }
1314  CM.finish();
1315 }
1316 
1317 static bool
1318 FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field);
1319 
1320 static bool
1322  const CXXRecordDecl *BaseClassDecl,
1323  const CXXRecordDecl *MostDerivedClassDecl)
1324 {
1325  // If the destructor is trivial we don't have to check anything else.
1326  if (BaseClassDecl->hasTrivialDestructor())
1327  return true;
1328 
1329  if (!BaseClassDecl->getDestructor()->hasTrivialBody())
1330  return false;
1331 
1332  // Check fields.
1333  for (const auto *Field : BaseClassDecl->fields())
1334  if (!FieldHasTrivialDestructorBody(Context, Field))
1335  return false;
1336 
1337  // Check non-virtual bases.
1338  for (const auto &I : BaseClassDecl->bases()) {
1339  if (I.isVirtual())
1340  continue;
1341 
1342  const CXXRecordDecl *NonVirtualBase =
1343  cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1344  if (!HasTrivialDestructorBody(Context, NonVirtualBase,
1345  MostDerivedClassDecl))
1346  return false;
1347  }
1348 
1349  if (BaseClassDecl == MostDerivedClassDecl) {
1350  // Check virtual bases.
1351  for (const auto &I : BaseClassDecl->vbases()) {
1352  const CXXRecordDecl *VirtualBase =
1353  cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1354  if (!HasTrivialDestructorBody(Context, VirtualBase,
1355  MostDerivedClassDecl))
1356  return false;
1357  }
1358  }
1359 
1360  return true;
1361 }
1362 
1363 static bool
1365  const FieldDecl *Field)
1366 {
1367  QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
1368 
1369  const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
1370  if (!RT)
1371  return true;
1372 
1373  CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1374 
1375  // The destructor for an implicit anonymous union member is never invoked.
1376  if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
1377  return false;
1378 
1379  return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
1380 }
1381 
1382 /// CanSkipVTablePointerInitialization - Check whether we need to initialize
1383 /// any vtable pointers before calling this destructor.
1385  const CXXDestructorDecl *Dtor) {
1386  const CXXRecordDecl *ClassDecl = Dtor->getParent();
1387  if (!ClassDecl->isDynamicClass())
1388  return true;
1389 
1390  if (!Dtor->hasTrivialBody())
1391  return false;
1392 
1393  // Check the fields.
1394  for (const auto *Field : ClassDecl->fields())
1395  if (!FieldHasTrivialDestructorBody(CGF.getContext(), Field))
1396  return false;
1397 
1398  return true;
1399 }
1400 
1401 /// EmitDestructorBody - Emits the body of the current destructor.
1403  const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
1404  CXXDtorType DtorType = CurGD.getDtorType();
1405 
1406  // For an abstract class, non-base destructors are never used (and can't
1407  // be emitted in general, because vbase dtors may not have been validated
1408  // by Sema), but the Itanium ABI doesn't make them optional and Clang may
1409  // in fact emit references to them from other compilations, so emit them
1410  // as functions containing a trap instruction.
1411  if (DtorType != Dtor_Base && Dtor->getParent()->isAbstract()) {
1412  llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap);
1413  TrapCall->setDoesNotReturn();
1414  TrapCall->setDoesNotThrow();
1415  Builder.CreateUnreachable();
1416  Builder.ClearInsertionPoint();
1417  return;
1418  }
1419 
1420  Stmt *Body = Dtor->getBody();
1421  if (Body)
1422  incrementProfileCounter(Body);
1423 
1424  // The call to operator delete in a deleting destructor happens
1425  // outside of the function-try-block, which means it's always
1426  // possible to delegate the destructor body to the complete
1427  // destructor. Do so.
1428  if (DtorType == Dtor_Deleting) {
1429  RunCleanupsScope DtorEpilogue(*this);
1430  EnterDtorCleanups(Dtor, Dtor_Deleting);
1431  if (HaveInsertPoint())
1432  EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
1433  /*Delegating=*/false, LoadCXXThisAddress());
1434  return;
1435  }
1436 
1437  // If the body is a function-try-block, enter the try before
1438  // anything else.
1439  bool isTryBody = (Body && isa<CXXTryStmt>(Body));
1440  if (isTryBody)
1441  EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1442  EmitAsanPrologueOrEpilogue(false);
1443 
1444  // Enter the epilogue cleanups.
1445  RunCleanupsScope DtorEpilogue(*this);
1446 
1447  // If this is the complete variant, just invoke the base variant;
1448  // the epilogue will destruct the virtual bases. But we can't do
1449  // this optimization if the body is a function-try-block, because
1450  // we'd introduce *two* handler blocks. In the Microsoft ABI, we
1451  // always delegate because we might not have a definition in this TU.
1452  switch (DtorType) {
1453  case Dtor_Comdat: llvm_unreachable("not expecting a COMDAT");
1454  case Dtor_Deleting: llvm_unreachable("already handled deleting case");
1455 
1456  case Dtor_Complete:
1457  assert((Body || getTarget().getCXXABI().isMicrosoft()) &&
1458  "can't emit a dtor without a body for non-Microsoft ABIs");
1459 
1460  // Enter the cleanup scopes for virtual bases.
1461  EnterDtorCleanups(Dtor, Dtor_Complete);
1462 
1463  if (!isTryBody) {
1464  EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
1465  /*Delegating=*/false, LoadCXXThisAddress());
1466  break;
1467  }
1468 
1469  // Fallthrough: act like we're in the base variant.
1470  LLVM_FALLTHROUGH;
1471 
1472  case Dtor_Base:
1473  assert(Body);
1474 
1475  // Enter the cleanup scopes for fields and non-virtual bases.
1476  EnterDtorCleanups(Dtor, Dtor_Base);
1477 
1478  // Initialize the vtable pointers before entering the body.
1479  if (!CanSkipVTablePointerInitialization(*this, Dtor)) {
1480  // Insert the llvm.launder.invariant.group intrinsic before initializing
1481  // the vptrs to cancel any previous assumptions we might have made.
1482  if (CGM.getCodeGenOpts().StrictVTablePointers &&
1483  CGM.getCodeGenOpts().OptimizationLevel > 0)
1484  CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis());
1485  InitializeVTablePointers(Dtor->getParent());
1486  }
1487 
1488  if (isTryBody)
1489  EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
1490  else if (Body)
1491  EmitStmt(Body);
1492  else {
1493  assert(Dtor->isImplicit() && "bodyless dtor not implicit");
1494  // nothing to do besides what's in the epilogue
1495  }
1496  // -fapple-kext must inline any call to this dtor into
1497  // the caller's body.
1498  if (getLangOpts().AppleKext)
1499  CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
1500 
1501  break;
1502  }
1503 
1504  // Jump out through the epilogue cleanups.
1505  DtorEpilogue.ForceCleanup();
1506 
1507  // Exit the try if applicable.
1508  if (isTryBody)
1509  ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1510 }
1511 
1513  const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl());
1514  const Stmt *RootS = AssignOp->getBody();
1515  assert(isa<CompoundStmt>(RootS) &&
1516  "Body of an implicit assignment operator should be compound stmt.");
1517  const CompoundStmt *RootCS = cast<CompoundStmt>(RootS);
1518 
1519  LexicalScope Scope(*this, RootCS->getSourceRange());
1520 
1521  incrementProfileCounter(RootCS);
1522  AssignmentMemcpyizer AM(*this, AssignOp, Args);
1523  for (auto *I : RootCS->body())
1524  AM.emitAssignment(I);
1525  AM.finish();
1526 }
1527 
1528 namespace {
1529  llvm::Value *LoadThisForDtorDelete(CodeGenFunction &CGF,
1530  const CXXDestructorDecl *DD) {
1531  if (Expr *ThisArg = DD->getOperatorDeleteThisArg())
1532  return CGF.EmitScalarExpr(ThisArg);
1533  return CGF.LoadCXXThis();
1534  }
1535 
1536  /// Call the operator delete associated with the current destructor.
1537  struct CallDtorDelete final : EHScopeStack::Cleanup {
1538  CallDtorDelete() {}
1539 
1540  void Emit(CodeGenFunction &CGF, Flags flags) override {
1541  const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1542  const CXXRecordDecl *ClassDecl = Dtor->getParent();
1543  CGF.EmitDeleteCall(Dtor->getOperatorDelete(),
1544  LoadThisForDtorDelete(CGF, Dtor),
1545  CGF.getContext().getTagDeclType(ClassDecl));
1546  }
1547  };
1548 
1549  void EmitConditionalDtorDeleteCall(CodeGenFunction &CGF,
1550  llvm::Value *ShouldDeleteCondition,
1551  bool ReturnAfterDelete) {
1552  llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete");
1553  llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue");
1554  llvm::Value *ShouldCallDelete
1555  = CGF.Builder.CreateIsNull(ShouldDeleteCondition);
1556  CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB);
1557 
1558  CGF.EmitBlock(callDeleteBB);
1559  const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1560  const CXXRecordDecl *ClassDecl = Dtor->getParent();
1561  CGF.EmitDeleteCall(Dtor->getOperatorDelete(),
1562  LoadThisForDtorDelete(CGF, Dtor),
1563  CGF.getContext().getTagDeclType(ClassDecl));
1564  assert(Dtor->getOperatorDelete()->isDestroyingOperatorDelete() ==
1565  ReturnAfterDelete &&
1566  "unexpected value for ReturnAfterDelete");
1567  if (ReturnAfterDelete)
1569  else
1570  CGF.Builder.CreateBr(continueBB);
1571 
1572  CGF.EmitBlock(continueBB);
1573  }
1574 
1575  struct CallDtorDeleteConditional final : EHScopeStack::Cleanup {
1576  llvm::Value *ShouldDeleteCondition;
1577 
1578  public:
1579  CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition)
1580  : ShouldDeleteCondition(ShouldDeleteCondition) {
1581  assert(ShouldDeleteCondition != nullptr);
1582  }
1583 
1584  void Emit(CodeGenFunction &CGF, Flags flags) override {
1585  EmitConditionalDtorDeleteCall(CGF, ShouldDeleteCondition,
1586  /*ReturnAfterDelete*/false);
1587  }
1588  };
1589 
1590  class DestroyField final : public EHScopeStack::Cleanup {
1591  const FieldDecl *field;
1592  CodeGenFunction::Destroyer *destroyer;
1593  bool useEHCleanupForArray;
1594 
1595  public:
1596  DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
1597  bool useEHCleanupForArray)
1598  : field(field), destroyer(destroyer),
1599  useEHCleanupForArray(useEHCleanupForArray) {}
1600 
1601  void Emit(CodeGenFunction &CGF, Flags flags) override {
1602  // Find the address of the field.
1603  Address thisValue = CGF.LoadCXXThisAddress();
1604  QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent());
1605  LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy);
1606  LValue LV = CGF.EmitLValueForField(ThisLV, field);
1607  assert(LV.isSimple());
1608 
1609  CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer,
1610  flags.isForNormalCleanup() && useEHCleanupForArray);
1611  }
1612  };
1613 
1614  static void EmitSanitizerDtorCallback(CodeGenFunction &CGF, llvm::Value *Ptr,
1615  CharUnits::QuantityType PoisonSize) {
1616  CodeGenFunction::SanitizerScope SanScope(&CGF);
1617  // Pass in void pointer and size of region as arguments to runtime
1618  // function
1619  llvm::Value *Args[] = {CGF.Builder.CreateBitCast(Ptr, CGF.VoidPtrTy),
1620  llvm::ConstantInt::get(CGF.SizeTy, PoisonSize)};
1621 
1622  llvm::Type *ArgTypes[] = {CGF.VoidPtrTy, CGF.SizeTy};
1623 
1624  llvm::FunctionType *FnType =
1625  llvm::FunctionType::get(CGF.VoidTy, ArgTypes, false);
1626  llvm::Value *Fn =
1627  CGF.CGM.CreateRuntimeFunction(FnType, "__sanitizer_dtor_callback");
1628  CGF.EmitNounwindRuntimeCall(Fn, Args);
1629  }
1630 
1631  class SanitizeDtorMembers final : public EHScopeStack::Cleanup {
1632  const CXXDestructorDecl *Dtor;
1633 
1634  public:
1635  SanitizeDtorMembers(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {}
1636 
1637  // Generate function call for handling object poisoning.
1638  // Disables tail call elimination, to prevent the current stack frame
1639  // from disappearing from the stack trace.
1640  void Emit(CodeGenFunction &CGF, Flags flags) override {
1641  const ASTRecordLayout &Layout =
1642  CGF.getContext().getASTRecordLayout(Dtor->getParent());
1643 
1644  // Nothing to poison.
1645  if (Layout.getFieldCount() == 0)
1646  return;
1647 
1648  // Prevent the current stack frame from disappearing from the stack trace.
1649  CGF.CurFn->addFnAttr("disable-tail-calls", "true");
1650 
1651  // Construct pointer to region to begin poisoning, and calculate poison
1652  // size, so that only members declared in this class are poisoned.
1653  ASTContext &Context = CGF.getContext();
1654  unsigned fieldIndex = 0;
1655  int startIndex = -1;
1656  // RecordDecl::field_iterator Field;
1657  for (const FieldDecl *Field : Dtor->getParent()->fields()) {
1658  // Poison field if it is trivial
1659  if (FieldHasTrivialDestructorBody(Context, Field)) {
1660  // Start sanitizing at this field
1661  if (startIndex < 0)
1662  startIndex = fieldIndex;
1663 
1664  // Currently on the last field, and it must be poisoned with the
1665  // current block.
1666  if (fieldIndex == Layout.getFieldCount() - 1) {
1667  PoisonMembers(CGF, startIndex, Layout.getFieldCount());
1668  }
1669  } else if (startIndex >= 0) {
1670  // No longer within a block of memory to poison, so poison the block
1671  PoisonMembers(CGF, startIndex, fieldIndex);
1672  // Re-set the start index
1673  startIndex = -1;
1674  }
1675  fieldIndex += 1;
1676  }
1677  }
1678 
1679  private:
1680  /// \param layoutStartOffset index of the ASTRecordLayout field to
1681  /// start poisoning (inclusive)
1682  /// \param layoutEndOffset index of the ASTRecordLayout field to
1683  /// end poisoning (exclusive)
1684  void PoisonMembers(CodeGenFunction &CGF, unsigned layoutStartOffset,
1685  unsigned layoutEndOffset) {
1686  ASTContext &Context = CGF.getContext();
1687  const ASTRecordLayout &Layout =
1688  Context.getASTRecordLayout(Dtor->getParent());
1689 
1690  llvm::ConstantInt *OffsetSizePtr = llvm::ConstantInt::get(
1691  CGF.SizeTy,
1692  Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutStartOffset))
1693  .getQuantity());
1694 
1695  llvm::Value *OffsetPtr = CGF.Builder.CreateGEP(
1696  CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.Int8PtrTy),
1697  OffsetSizePtr);
1698 
1699  CharUnits::QuantityType PoisonSize;
1700  if (layoutEndOffset >= Layout.getFieldCount()) {
1701  PoisonSize = Layout.getNonVirtualSize().getQuantity() -
1702  Context.toCharUnitsFromBits(
1703  Layout.getFieldOffset(layoutStartOffset))
1704  .getQuantity();
1705  } else {
1706  PoisonSize = Context.toCharUnitsFromBits(
1707  Layout.getFieldOffset(layoutEndOffset) -
1708  Layout.getFieldOffset(layoutStartOffset))
1709  .getQuantity();
1710  }
1711 
1712  if (PoisonSize == 0)
1713  return;
1714 
1715  EmitSanitizerDtorCallback(CGF, OffsetPtr, PoisonSize);
1716  }
1717  };
1718 
1719  class SanitizeDtorVTable final : public EHScopeStack::Cleanup {
1720  const CXXDestructorDecl *Dtor;
1721 
1722  public:
1723  SanitizeDtorVTable(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {}
1724 
1725  // Generate function call for handling vtable pointer poisoning.
1726  void Emit(CodeGenFunction &CGF, Flags flags) override {
1727  assert(Dtor->getParent()->isDynamicClass());
1728  (void)Dtor;
1729  ASTContext &Context = CGF.getContext();
1730  // Poison vtable and vtable ptr if they exist for this class.
1731  llvm::Value *VTablePtr = CGF.LoadCXXThis();
1732 
1733  CharUnits::QuantityType PoisonSize =
1734  Context.toCharUnitsFromBits(CGF.PointerWidthInBits).getQuantity();
1735  // Pass in void pointer and size of region as arguments to runtime
1736  // function
1737  EmitSanitizerDtorCallback(CGF, VTablePtr, PoisonSize);
1738  }
1739  };
1740 } // end anonymous namespace
1741 
1742 /// Emit all code that comes at the end of class's
1743 /// destructor. This is to call destructors on members and base classes
1744 /// in reverse order of their construction.
1745 ///
1746 /// For a deleting destructor, this also handles the case where a destroying
1747 /// operator delete completely overrides the definition.
1749  CXXDtorType DtorType) {
1750  assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) &&
1751  "Should not emit dtor epilogue for non-exported trivial dtor!");
1752 
1753  // The deleting-destructor phase just needs to call the appropriate
1754  // operator delete that Sema picked up.
1755  if (DtorType == Dtor_Deleting) {
1756  assert(DD->getOperatorDelete() &&
1757  "operator delete missing - EnterDtorCleanups");
1758  if (CXXStructorImplicitParamValue) {
1759  // If there is an implicit param to the deleting dtor, it's a boolean
1760  // telling whether this is a deleting destructor.
1762  EmitConditionalDtorDeleteCall(*this, CXXStructorImplicitParamValue,
1763  /*ReturnAfterDelete*/true);
1764  else
1765  EHStack.pushCleanup<CallDtorDeleteConditional>(
1766  NormalAndEHCleanup, CXXStructorImplicitParamValue);
1767  } else {
1769  const CXXRecordDecl *ClassDecl = DD->getParent();
1770  EmitDeleteCall(DD->getOperatorDelete(),
1771  LoadThisForDtorDelete(*this, DD),
1772  getContext().getTagDeclType(ClassDecl));
1773  EmitBranchThroughCleanup(ReturnBlock);
1774  } else {
1775  EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1776  }
1777  }
1778  return;
1779  }
1780 
1781  const CXXRecordDecl *ClassDecl = DD->getParent();
1782 
1783  // Unions have no bases and do not call field destructors.
1784  if (ClassDecl->isUnion())
1785  return;
1786 
1787  // The complete-destructor phase just destructs all the virtual bases.
1788  if (DtorType == Dtor_Complete) {
1789  // Poison the vtable pointer such that access after the base
1790  // and member destructors are invoked is invalid.
1791  if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1792  SanOpts.has(SanitizerKind::Memory) && ClassDecl->getNumVBases() &&
1793  ClassDecl->isPolymorphic())
1794  EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD);
1795 
1796  // We push them in the forward order so that they'll be popped in
1797  // the reverse order.
1798  for (const auto &Base : ClassDecl->vbases()) {
1799  CXXRecordDecl *BaseClassDecl
1800  = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1801 
1802  // Ignore trivial destructors.
1803  if (BaseClassDecl->hasTrivialDestructor())
1804  continue;
1805 
1806  EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1807  BaseClassDecl,
1808  /*BaseIsVirtual*/ true);
1809  }
1810 
1811  return;
1812  }
1813 
1814  assert(DtorType == Dtor_Base);
1815  // Poison the vtable pointer if it has no virtual bases, but inherits
1816  // virtual functions.
1817  if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1818  SanOpts.has(SanitizerKind::Memory) && !ClassDecl->getNumVBases() &&
1819  ClassDecl->isPolymorphic())
1820  EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD);
1821 
1822  // Destroy non-virtual bases.
1823  for (const auto &Base : ClassDecl->bases()) {
1824  // Ignore virtual bases.
1825  if (Base.isVirtual())
1826  continue;
1827 
1828  CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
1829 
1830  // Ignore trivial destructors.
1831  if (BaseClassDecl->hasTrivialDestructor())
1832  continue;
1833 
1834  EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1835  BaseClassDecl,
1836  /*BaseIsVirtual*/ false);
1837  }
1838 
1839  // Poison fields such that access after their destructors are
1840  // invoked, and before the base class destructor runs, is invalid.
1841  if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
1842  SanOpts.has(SanitizerKind::Memory))
1843  EHStack.pushCleanup<SanitizeDtorMembers>(NormalAndEHCleanup, DD);
1844 
1845  // Destroy direct fields.
1846  for (const auto *Field : ClassDecl->fields()) {
1847  QualType type = Field->getType();
1848  QualType::DestructionKind dtorKind = type.isDestructedType();
1849  if (!dtorKind) continue;
1850 
1851  // Anonymous union members do not have their destructors called.
1852  const RecordType *RT = type->getAsUnionType();
1853  if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue;
1854 
1855  CleanupKind cleanupKind = getCleanupKind(dtorKind);
1856  EHStack.pushCleanup<DestroyField>(cleanupKind, Field,
1857  getDestroyer(dtorKind),
1858  cleanupKind & EHCleanup);
1859  }
1860 }
1861 
1862 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1863 /// constructor for each of several members of an array.
1864 ///
1865 /// \param ctor the constructor to call for each element
1866 /// \param arrayType the type of the array to initialize
1867 /// \param arrayBegin an arrayType*
1868 /// \param zeroInitialize true if each element should be
1869 /// zero-initialized before it is constructed
1871  const CXXConstructorDecl *ctor, const ArrayType *arrayType,
1872  Address arrayBegin, const CXXConstructExpr *E, bool zeroInitialize) {
1873  QualType elementType;
1874  llvm::Value *numElements =
1875  emitArrayLength(arrayType, elementType, arrayBegin);
1876 
1877  EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, zeroInitialize);
1878 }
1879 
1880 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1881 /// constructor for each of several members of an array.
1882 ///
1883 /// \param ctor the constructor to call for each element
1884 /// \param numElements the number of elements in the array;
1885 /// may be zero
1886 /// \param arrayBase a T*, where T is the type constructed by ctor
1887 /// \param zeroInitialize true if each element should be
1888 /// zero-initialized before it is constructed
1890  llvm::Value *numElements,
1891  Address arrayBase,
1892  const CXXConstructExpr *E,
1893  bool zeroInitialize) {
1894  // It's legal for numElements to be zero. This can happen both
1895  // dynamically, because x can be zero in 'new A[x]', and statically,
1896  // because of GCC extensions that permit zero-length arrays. There
1897  // are probably legitimate places where we could assume that this
1898  // doesn't happen, but it's not clear that it's worth it.
1899  llvm::BranchInst *zeroCheckBranch = nullptr;
1900 
1901  // Optimize for a constant count.
1902  llvm::ConstantInt *constantCount
1903  = dyn_cast<llvm::ConstantInt>(numElements);
1904  if (constantCount) {
1905  // Just skip out if the constant count is zero.
1906  if (constantCount->isZero()) return;
1907 
1908  // Otherwise, emit the check.
1909  } else {
1910  llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
1911  llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
1912  zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
1913  EmitBlock(loopBB);
1914  }
1915 
1916  // Find the end of the array.
1917  llvm::Value *arrayBegin = arrayBase.getPointer();
1918  llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements,
1919  "arrayctor.end");
1920 
1921  // Enter the loop, setting up a phi for the current location to initialize.
1922  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1923  llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
1924  EmitBlock(loopBB);
1925  llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
1926  "arrayctor.cur");
1927  cur->addIncoming(arrayBegin, entryBB);
1928 
1929  // Inside the loop body, emit the constructor call on the array element.
1930 
1931  // The alignment of the base, adjusted by the size of a single element,
1932  // provides a conservative estimate of the alignment of every element.
1933  // (This assumes we never start tracking offsetted alignments.)
1934  //
1935  // Note that these are complete objects and so we don't need to
1936  // use the non-virtual size or alignment.
1938  CharUnits eltAlignment =
1939  arrayBase.getAlignment()
1940  .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
1941  Address curAddr = Address(cur, eltAlignment);
1942 
1943  // Zero initialize the storage, if requested.
1944  if (zeroInitialize)
1945  EmitNullInitialization(curAddr, type);
1946 
1947  // C++ [class.temporary]p4:
1948  // There are two contexts in which temporaries are destroyed at a different
1949  // point than the end of the full-expression. The first context is when a
1950  // default constructor is called to initialize an element of an array.
1951  // If the constructor has one or more default arguments, the destruction of
1952  // every temporary created in a default argument expression is sequenced
1953  // before the construction of the next array element, if any.
1954 
1955  {
1956  RunCleanupsScope Scope(*this);
1957 
1958  // Evaluate the constructor and its arguments in a regular
1959  // partial-destroy cleanup.
1960  if (getLangOpts().Exceptions &&
1961  !ctor->getParent()->hasTrivialDestructor()) {
1962  Destroyer *destroyer = destroyCXXObject;
1963  pushRegularPartialArrayCleanup(arrayBegin, cur, type, eltAlignment,
1964  *destroyer);
1965  }
1966 
1967  EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false,
1968  /*Delegating=*/false, curAddr, E,
1970  }
1971 
1972  // Go to the next element.
1973  llvm::Value *next =
1974  Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1),
1975  "arrayctor.next");
1976  cur->addIncoming(next, Builder.GetInsertBlock());
1977 
1978  // Check whether that's the end of the loop.
1979  llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
1980  llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
1981  Builder.CreateCondBr(done, contBB, loopBB);
1982 
1983  // Patch the earlier check to skip over the loop.
1984  if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
1985 
1986  EmitBlock(contBB);
1987 }
1988 
1990  Address addr,
1991  QualType type) {
1992  const RecordType *rtype = type->castAs<RecordType>();
1993  const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
1994  const CXXDestructorDecl *dtor = record->getDestructor();
1995  assert(!dtor->isTrivial());
1996  CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
1997  /*Delegating=*/false, addr);
1998 }
1999 
2001  CXXCtorType Type,
2002  bool ForVirtualBase,
2003  bool Delegating, Address This,
2004  const CXXConstructExpr *E,
2005  AggValueSlot::Overlap_t Overlap) {
2006  CallArgList Args;
2007 
2008  // Push the this ptr.
2009  Args.add(RValue::get(This.getPointer()), D->getThisType(getContext()));
2010 
2011  // If this is a trivial constructor, emit a memcpy now before we lose
2012  // the alignment information on the argument.
2013  // FIXME: It would be better to preserve alignment information into CallArg.
2015  assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
2016 
2017  const Expr *Arg = E->getArg(0);
2018  LValue Src = EmitLValue(Arg);
2019  QualType DestTy = getContext().getTypeDeclType(D->getParent());
2020  LValue Dest = MakeAddrLValue(This, DestTy);
2021  EmitAggregateCopyCtor(Dest, Src, Overlap);
2022  return;
2023  }
2024 
2025  // Add the rest of the user-supplied arguments.
2026  const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
2028  ? EvaluationOrder::ForceLeftToRight
2030  EmitCallArgs(Args, FPT, E->arguments(), E->getConstructor(),
2031  /*ParamsToSkip*/ 0, Order);
2032 
2033  EmitCXXConstructorCall(D, Type, ForVirtualBase, Delegating, This, Args,
2034  Overlap, E->getExprLoc());
2035 }
2036 
2038  const CXXConstructorDecl *Ctor,
2039  CXXCtorType Type, CallArgList &Args) {
2040  // We can't forward a variadic call.
2041  if (Ctor->isVariadic())
2042  return false;
2043 
2045  // If the parameters are callee-cleanup, it's not safe to forward.
2046  for (auto *P : Ctor->parameters())
2047  if (P->getType().isDestructedType())
2048  return false;
2049 
2050  // Likewise if they're inalloca.
2051  const CGFunctionInfo &Info =
2052  CGF.CGM.getTypes().arrangeCXXConstructorCall(Args, Ctor, Type, 0, 0);
2053  if (Info.usesInAlloca())
2054  return false;
2055  }
2056 
2057  // Anything else should be OK.
2058  return true;
2059 }
2060 
2062  CXXCtorType Type,
2063  bool ForVirtualBase,
2064  bool Delegating,
2065  Address This,
2066  CallArgList &Args,
2067  AggValueSlot::Overlap_t Overlap,
2068  SourceLocation Loc) {
2069  const CXXRecordDecl *ClassDecl = D->getParent();
2070 
2071  // C++11 [class.mfct.non-static]p2:
2072  // If a non-static member function of a class X is called for an object that
2073  // is not of type X, or of a type derived from X, the behavior is undefined.
2074  EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, Loc,
2075  This.getPointer(), getContext().getRecordType(ClassDecl));
2076 
2077  if (D->isTrivial() && D->isDefaultConstructor()) {
2078  assert(Args.size() == 1 && "trivial default ctor with args");
2079  return;
2080  }
2081 
2082  // If this is a trivial constructor, just emit what's needed. If this is a
2083  // union copy constructor, we must emit a memcpy, because the AST does not
2084  // model that copy.
2086  assert(Args.size() == 2 && "unexpected argcount for trivial ctor");
2087 
2088  QualType SrcTy = D->getParamDecl(0)->getType().getNonReferenceType();
2089  Address Src(Args[1].getRValue(*this).getScalarVal(),
2090  getNaturalTypeAlignment(SrcTy));
2091  LValue SrcLVal = MakeAddrLValue(Src, SrcTy);
2092  QualType DestTy = getContext().getTypeDeclType(ClassDecl);
2093  LValue DestLVal = MakeAddrLValue(This, DestTy);
2094  EmitAggregateCopyCtor(DestLVal, SrcLVal, Overlap);
2095  return;
2096  }
2097 
2098  bool PassPrototypeArgs = true;
2099  // Check whether we can actually emit the constructor before trying to do so.
2100  if (auto Inherited = D->getInheritedConstructor()) {
2101  PassPrototypeArgs = getTypes().inheritingCtorHasParams(Inherited, Type);
2102  if (PassPrototypeArgs && !canEmitDelegateCallArgs(*this, D, Type, Args)) {
2103  EmitInlinedInheritingCXXConstructorCall(D, Type, ForVirtualBase,
2104  Delegating, Args);
2105  return;
2106  }
2107  }
2108 
2109  // Insert any ABI-specific implicit constructor arguments.
2110  CGCXXABI::AddedStructorArgs ExtraArgs =
2111  CGM.getCXXABI().addImplicitConstructorArgs(*this, D, Type, ForVirtualBase,
2112  Delegating, Args);
2113 
2114  // Emit the call.
2115  llvm::Constant *CalleePtr =
2116  CGM.getAddrOfCXXStructor(D, getFromCtorType(Type));
2117  const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall(
2118  Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs);
2119  CGCallee Callee = CGCallee::forDirect(CalleePtr, D);
2120  EmitCall(Info, Callee, ReturnValueSlot(), Args);
2121 
2122  // Generate vtable assumptions if we're constructing a complete object
2123  // with a vtable. We don't do this for base subobjects for two reasons:
2124  // first, it's incorrect for classes with virtual bases, and second, we're
2125  // about to overwrite the vptrs anyway.
2126  // We also have to make sure if we can refer to vtable:
2127  // - Otherwise we can refer to vtable if it's safe to speculatively emit.
2128  // FIXME: If vtable is used by ctor/dtor, or if vtable is external and we are
2129  // sure that definition of vtable is not hidden,
2130  // then we are always safe to refer to it.
2131  // FIXME: It looks like InstCombine is very inefficient on dealing with
2132  // assumes. Make assumption loads require -fstrict-vtable-pointers temporarily.
2133  if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2134  ClassDecl->isDynamicClass() && Type != Ctor_Base &&
2135  CGM.getCXXABI().canSpeculativelyEmitVTable(ClassDecl) &&
2136  CGM.getCodeGenOpts().StrictVTablePointers)
2137  EmitVTableAssumptionLoads(ClassDecl, This);
2138 }
2139 
2141  const CXXConstructorDecl *D, bool ForVirtualBase, Address This,
2142  bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E) {
2143  CallArgList Args;
2144  CallArg ThisArg(RValue::get(This.getPointer()), D->getThisType(getContext()));
2145 
2146  // Forward the parameters.
2147  if (InheritedFromVBase &&
2148  CGM.getTarget().getCXXABI().hasConstructorVariants()) {
2149  // Nothing to do; this construction is not responsible for constructing
2150  // the base class containing the inherited constructor.
2151  // FIXME: Can we just pass undef's for the remaining arguments if we don't
2152  // have constructor variants?
2153  Args.push_back(ThisArg);
2154  } else if (!CXXInheritedCtorInitExprArgs.empty()) {
2155  // The inheriting constructor was inlined; just inject its arguments.
2156  assert(CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() &&
2157  "wrong number of parameters for inherited constructor call");
2158  Args = CXXInheritedCtorInitExprArgs;
2159  Args[0] = ThisArg;
2160  } else {
2161  // The inheriting constructor was not inlined. Emit delegating arguments.
2162  Args.push_back(ThisArg);
2163  const auto *OuterCtor = cast<CXXConstructorDecl>(CurCodeDecl);
2164  assert(OuterCtor->getNumParams() == D->getNumParams());
2165  assert(!OuterCtor->isVariadic() && "should have been inlined");
2166 
2167  for (const auto *Param : OuterCtor->parameters()) {
2168  assert(getContext().hasSameUnqualifiedType(
2169  OuterCtor->getParamDecl(Param->getFunctionScopeIndex())->getType(),
2170  Param->getType()));
2171  EmitDelegateCallArg(Args, Param, E->getLocation());
2172 
2173  // Forward __attribute__(pass_object_size).
2174  if (Param->hasAttr<PassObjectSizeAttr>()) {
2175  auto *POSParam = SizeArguments[Param];
2176  assert(POSParam && "missing pass_object_size value for forwarding");
2177  EmitDelegateCallArg(Args, POSParam, E->getLocation());
2178  }
2179  }
2180  }
2181 
2182  EmitCXXConstructorCall(D, Ctor_Base, ForVirtualBase, /*Delegating*/false,
2183  This, Args, AggValueSlot::MayOverlap,
2184  E->getLocation());
2185 }
2186 
2188  const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase,
2189  bool Delegating, CallArgList &Args) {
2190  GlobalDecl GD(Ctor, CtorType);
2192  ApplyInlineDebugLocation DebugScope(*this, GD);
2193 
2194  // Save the arguments to be passed to the inherited constructor.
2195  CXXInheritedCtorInitExprArgs = Args;
2196 
2197  FunctionArgList Params;
2198  QualType RetType = BuildFunctionArgList(CurGD, Params);
2199  FnRetTy = RetType;
2200 
2201  // Insert any ABI-specific implicit constructor arguments.
2202  CGM.getCXXABI().addImplicitConstructorArgs(*this, Ctor, CtorType,
2203  ForVirtualBase, Delegating, Args);
2204 
2205  // Emit a simplified prolog. We only need to emit the implicit params.
2206  assert(Args.size() >= Params.size() && "too few arguments for call");
2207  for (unsigned I = 0, N = Args.size(); I != N; ++I) {
2208  if (I < Params.size() && isa<ImplicitParamDecl>(Params[I])) {
2209  const RValue &RV = Args[I].getRValue(*this);
2210  assert(!RV.isComplex() && "complex indirect params not supported");
2211  ParamValue Val = RV.isScalar()
2212  ? ParamValue::forDirect(RV.getScalarVal())
2213  : ParamValue::forIndirect(RV.getAggregateAddress());
2214  EmitParmDecl(*Params[I], Val, I + 1);
2215  }
2216  }
2217 
2218  // Create a return value slot if the ABI implementation wants one.
2219  // FIXME: This is dumb, we should ask the ABI not to try to set the return
2220  // value instead.
2221  if (!RetType->isVoidType())
2222  ReturnValue = CreateIRTemp(RetType, "retval.inhctor");
2223 
2224  CGM.getCXXABI().EmitInstanceFunctionProlog(*this);
2225  CXXThisValue = CXXABIThisValue;
2226 
2227  // Directly emit the constructor initializers.
2228  EmitCtorPrologue(Ctor, CtorType, Params);
2229 }
2230 
2232  llvm::Value *VTableGlobal =
2233  CGM.getCXXABI().getVTableAddressPoint(Vptr.Base, Vptr.VTableClass);
2234  if (!VTableGlobal)
2235  return;
2236 
2237  // We can just use the base offset in the complete class.
2238  CharUnits NonVirtualOffset = Vptr.Base.getBaseOffset();
2239 
2240  if (!NonVirtualOffset.isZero())
2241  This =
2242  ApplyNonVirtualAndVirtualOffset(*this, This, NonVirtualOffset, nullptr,
2243  Vptr.VTableClass, Vptr.NearestVBase);
2244 
2245  llvm::Value *VPtrValue =
2246  GetVTablePtr(This, VTableGlobal->getType(), Vptr.VTableClass);
2247  llvm::Value *Cmp =
2248  Builder.CreateICmpEQ(VPtrValue, VTableGlobal, "cmp.vtables");
2249  Builder.CreateAssumption(Cmp);
2250 }
2251 
2253  Address This) {
2254  if (CGM.getCXXABI().doStructorsInitializeVPtrs(ClassDecl))
2255  for (const VPtr &Vptr : getVTablePointers(ClassDecl))
2256  EmitVTableAssumptionLoad(Vptr, This);
2257 }
2258 
2259 void
2261  Address This, Address Src,
2262  const CXXConstructExpr *E) {
2263  const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
2264 
2265  CallArgList Args;
2266 
2267  // Push the this ptr.
2268  Args.add(RValue::get(This.getPointer()), D->getThisType(getContext()));
2269 
2270  // Push the src ptr.
2271  QualType QT = *(FPT->param_type_begin());
2272  llvm::Type *t = CGM.getTypes().ConvertType(QT);
2273  Src = Builder.CreateBitCast(Src, t);
2274  Args.add(RValue::get(Src.getPointer()), QT);
2275 
2276  // Skip over first argument (Src).
2277  EmitCallArgs(Args, FPT, drop_begin(E->arguments(), 1), E->getConstructor(),
2278  /*ParamsToSkip*/ 1);
2279 
2280  EmitCXXConstructorCall(D, Ctor_Complete, false, false, This, Args,
2282 }
2283 
2284 void
2286  CXXCtorType CtorType,
2287  const FunctionArgList &Args,
2288  SourceLocation Loc) {
2289  CallArgList DelegateArgs;
2290 
2291  FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
2292  assert(I != E && "no parameters to constructor");
2293 
2294  // this
2295  Address This = LoadCXXThisAddress();
2296  DelegateArgs.add(RValue::get(This.getPointer()), (*I)->getType());
2297  ++I;
2298 
2299  // FIXME: The location of the VTT parameter in the parameter list is
2300  // specific to the Itanium ABI and shouldn't be hardcoded here.
2301  if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) {
2302  assert(I != E && "cannot skip vtt parameter, already done with args");
2303  assert((*I)->getType()->isPointerType() &&
2304  "skipping parameter not of vtt type");
2305  ++I;
2306  }
2307 
2308  // Explicit arguments.
2309  for (; I != E; ++I) {
2310  const VarDecl *param = *I;
2311  // FIXME: per-argument source location
2312  EmitDelegateCallArg(DelegateArgs, param, Loc);
2313  }
2314 
2315  EmitCXXConstructorCall(Ctor, CtorType, /*ForVirtualBase=*/false,
2316  /*Delegating=*/true, This, DelegateArgs,
2318 }
2319 
2320 namespace {
2321  struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup {
2322  const CXXDestructorDecl *Dtor;
2323  Address Addr;
2324  CXXDtorType Type;
2325 
2326  CallDelegatingCtorDtor(const CXXDestructorDecl *D, Address Addr,
2327  CXXDtorType Type)
2328  : Dtor(D), Addr(Addr), Type(Type) {}
2329 
2330  void Emit(CodeGenFunction &CGF, Flags flags) override {
2331  CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
2332  /*Delegating=*/true, Addr);
2333  }
2334  };
2335 } // end anonymous namespace
2336 
2337 void
2339  const FunctionArgList &Args) {
2340  assert(Ctor->isDelegatingConstructor());
2341 
2342  Address ThisPtr = LoadCXXThisAddress();
2343 
2344  AggValueSlot AggSlot =
2345  AggValueSlot::forAddr(ThisPtr, Qualifiers(),
2350 
2351  EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
2352 
2353  const CXXRecordDecl *ClassDecl = Ctor->getParent();
2354  if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
2355  CXXDtorType Type =
2356  CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
2357 
2358  EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
2359  ClassDecl->getDestructor(),
2360  ThisPtr, Type);
2361  }
2362 }
2363 
2365  CXXDtorType Type,
2366  bool ForVirtualBase,
2367  bool Delegating,
2368  Address This) {
2369  CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase,
2370  Delegating, This);
2371 }
2372 
2373 namespace {
2374  struct CallLocalDtor final : EHScopeStack::Cleanup {
2375  const CXXDestructorDecl *Dtor;
2376  Address Addr;
2377 
2378  CallLocalDtor(const CXXDestructorDecl *D, Address Addr)
2379  : Dtor(D), Addr(Addr) {}
2380 
2381  void Emit(CodeGenFunction &CGF, Flags flags) override {
2383  /*ForVirtualBase=*/false,
2384  /*Delegating=*/false, Addr);
2385  }
2386  };
2387 } // end anonymous namespace
2388 
2390  Address Addr) {
2391  EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
2392 }
2393 
2395  CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
2396  if (!ClassDecl) return;
2397  if (ClassDecl->hasTrivialDestructor()) return;
2398 
2399  const CXXDestructorDecl *D = ClassDecl->getDestructor();
2400  assert(D && D->isUsed() && "destructor not marked as used!");
2401  PushDestructorCleanup(D, Addr);
2402 }
2403 
2405  // Compute the address point.
2406  llvm::Value *VTableAddressPoint =
2407  CGM.getCXXABI().getVTableAddressPointInStructor(
2408  *this, Vptr.VTableClass, Vptr.Base, Vptr.NearestVBase);
2409 
2410  if (!VTableAddressPoint)
2411  return;
2412 
2413  // Compute where to store the address point.
2414  llvm::Value *VirtualOffset = nullptr;
2415  CharUnits NonVirtualOffset = CharUnits::Zero();
2416 
2417  if (CGM.getCXXABI().isVirtualOffsetNeededForVTableField(*this, Vptr)) {
2418  // We need to use the virtual base offset offset because the virtual base
2419  // might have a different offset in the most derived class.
2420 
2421  VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(
2422  *this, LoadCXXThisAddress(), Vptr.VTableClass, Vptr.NearestVBase);
2423  NonVirtualOffset = Vptr.OffsetFromNearestVBase;
2424  } else {
2425  // We can just use the base offset in the complete class.
2426  NonVirtualOffset = Vptr.Base.getBaseOffset();
2427  }
2428 
2429  // Apply the offsets.
2430  Address VTableField = LoadCXXThisAddress();
2431 
2432  if (!NonVirtualOffset.isZero() || VirtualOffset)
2433  VTableField = ApplyNonVirtualAndVirtualOffset(
2434  *this, VTableField, NonVirtualOffset, VirtualOffset, Vptr.VTableClass,
2435  Vptr.NearestVBase);
2436 
2437  // Finally, store the address point. Use the same LLVM types as the field to
2438  // support optimization.
2439  llvm::Type *VTablePtrTy =
2440  llvm::FunctionType::get(CGM.Int32Ty, /*isVarArg=*/true)
2441  ->getPointerTo()
2442  ->getPointerTo();
2443  VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo());
2444  VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy);
2445 
2446  llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
2447  TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTablePtrTy);
2448  CGM.DecorateInstructionWithTBAA(Store, TBAAInfo);
2449  if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2450  CGM.getCodeGenOpts().StrictVTablePointers)
2451  CGM.DecorateInstructionWithInvariantGroup(Store, Vptr.VTableClass);
2452 }
2453 
2456  CodeGenFunction::VPtrsVector VPtrsResult;
2457  VisitedVirtualBasesSetTy VBases;
2458  getVTablePointers(BaseSubobject(VTableClass, CharUnits::Zero()),
2459  /*NearestVBase=*/nullptr,
2460  /*OffsetFromNearestVBase=*/CharUnits::Zero(),
2461  /*BaseIsNonVirtualPrimaryBase=*/false, VTableClass, VBases,
2462  VPtrsResult);
2463  return VPtrsResult;
2464 }
2465 
2467  const CXXRecordDecl *NearestVBase,
2468  CharUnits OffsetFromNearestVBase,
2469  bool BaseIsNonVirtualPrimaryBase,
2470  const CXXRecordDecl *VTableClass,
2471  VisitedVirtualBasesSetTy &VBases,
2472  VPtrsVector &Vptrs) {
2473  // If this base is a non-virtual primary base the address point has already
2474  // been set.
2475  if (!BaseIsNonVirtualPrimaryBase) {
2476  // Initialize the vtable pointer for this base.
2477  VPtr Vptr = {Base, NearestVBase, OffsetFromNearestVBase, VTableClass};
2478  Vptrs.push_back(Vptr);
2479  }
2480 
2481  const CXXRecordDecl *RD = Base.getBase();
2482 
2483  // Traverse bases.
2484  for (const auto &I : RD->bases()) {
2485  CXXRecordDecl *BaseDecl
2486  = cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
2487 
2488  // Ignore classes without a vtable.
2489  if (!BaseDecl->isDynamicClass())
2490  continue;
2491 
2492  CharUnits BaseOffset;
2493  CharUnits BaseOffsetFromNearestVBase;
2494  bool BaseDeclIsNonVirtualPrimaryBase;
2495 
2496  if (I.isVirtual()) {
2497  // Check if we've visited this virtual base before.
2498  if (!VBases.insert(BaseDecl).second)
2499  continue;
2500 
2501  const ASTRecordLayout &Layout =
2502  getContext().getASTRecordLayout(VTableClass);
2503 
2504  BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
2505  BaseOffsetFromNearestVBase = CharUnits::Zero();
2506  BaseDeclIsNonVirtualPrimaryBase = false;
2507  } else {
2508  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2509 
2510  BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
2511  BaseOffsetFromNearestVBase =
2512  OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
2513  BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
2514  }
2515 
2516  getVTablePointers(
2517  BaseSubobject(BaseDecl, BaseOffset),
2518  I.isVirtual() ? BaseDecl : NearestVBase, BaseOffsetFromNearestVBase,
2519  BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, Vptrs);
2520  }
2521 }
2522 
2524  // Ignore classes without a vtable.
2525  if (!RD->isDynamicClass())
2526  return;
2527 
2528  // Initialize the vtable pointers for this class and all of its bases.
2529  if (CGM.getCXXABI().doStructorsInitializeVPtrs(RD))
2530  for (const VPtr &Vptr : getVTablePointers(RD))
2531  InitializeVTablePointer(Vptr);
2532 
2533  if (RD->getNumVBases())
2534  CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD);
2535 }
2536 
2538  llvm::Type *VTableTy,
2539  const CXXRecordDecl *RD) {
2540  Address VTablePtrSrc = Builder.CreateElementBitCast(This, VTableTy);
2541  llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
2542  TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTableTy);
2543  CGM.DecorateInstructionWithTBAA(VTable, TBAAInfo);
2544 
2545  if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2546  CGM.getCodeGenOpts().StrictVTablePointers)
2547  CGM.DecorateInstructionWithInvariantGroup(VTable, RD);
2548 
2549  return VTable;
2550 }
2551 
2552 // If a class has a single non-virtual base and does not introduce or override
2553 // virtual member functions or fields, it will have the same layout as its base.
2554 // This function returns the least derived such class.
2555 //
2556 // Casting an instance of a base class to such a derived class is technically
2557 // undefined behavior, but it is a relatively common hack for introducing member
2558 // functions on class instances with specific properties (e.g. llvm::Operator)
2559 // that works under most compilers and should not have security implications, so
2560 // we allow it by default. It can be disabled with -fsanitize=cfi-cast-strict.
2561 static const CXXRecordDecl *
2563  if (!RD->field_empty())
2564  return RD;
2565 
2566  if (RD->getNumVBases() != 0)
2567  return RD;
2568 
2569  if (RD->getNumBases() != 1)
2570  return RD;
2571 
2572  for (const CXXMethodDecl *MD : RD->methods()) {
2573  if (MD->isVirtual()) {
2574  // Virtual member functions are only ok if they are implicit destructors
2575  // because the implicit destructor will have the same semantics as the
2576  // base class's destructor if no fields are added.
2577  if (isa<CXXDestructorDecl>(MD) && MD->isImplicit())
2578  continue;
2579  return RD;
2580  }
2581  }
2582 
2584  RD->bases_begin()->getType()->getAsCXXRecordDecl());
2585 }
2586 
2588  llvm::Value *VTable,
2589  SourceLocation Loc) {
2590  if (SanOpts.has(SanitizerKind::CFIVCall))
2591  EmitVTablePtrCheckForCall(RD, VTable, CodeGenFunction::CFITCK_VCall, Loc);
2592  else if (CGM.getCodeGenOpts().WholeProgramVTables &&
2593  CGM.HasHiddenLTOVisibility(RD)) {
2594  llvm::Metadata *MD =
2595  CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
2596  llvm::Value *TypeId =
2597  llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD);
2598 
2599  llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
2600  llvm::Value *TypeTest =
2601  Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test),
2602  {CastedVTable, TypeId});
2603  Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::assume), TypeTest);
2604  }
2605 }
2606 
2608  llvm::Value *VTable,
2609  CFITypeCheckKind TCK,
2610  SourceLocation Loc) {
2611  if (!SanOpts.has(SanitizerKind::CFICastStrict))
2613 
2614  EmitVTablePtrCheck(RD, VTable, TCK, Loc);
2615 }
2616 
2618  llvm::Value *Derived,
2619  bool MayBeNull,
2620  CFITypeCheckKind TCK,
2621  SourceLocation Loc) {
2622  if (!getLangOpts().CPlusPlus)
2623  return;
2624 
2625  auto *ClassTy = T->getAs<RecordType>();
2626  if (!ClassTy)
2627  return;
2628 
2629  const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassTy->getDecl());
2630 
2631  if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass())
2632  return;
2633 
2634  if (!SanOpts.has(SanitizerKind::CFICastStrict))
2635  ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
2636 
2637  llvm::BasicBlock *ContBlock = nullptr;
2638 
2639  if (MayBeNull) {
2640  llvm::Value *DerivedNotNull =
2641  Builder.CreateIsNotNull(Derived, "cast.nonnull");
2642 
2643  llvm::BasicBlock *CheckBlock = createBasicBlock("cast.check");
2644  ContBlock = createBasicBlock("cast.cont");
2645 
2646  Builder.CreateCondBr(DerivedNotNull, CheckBlock, ContBlock);
2647 
2648  EmitBlock(CheckBlock);
2649  }
2650 
2651  llvm::Value *VTable;
2652  std::tie(VTable, ClassDecl) = CGM.getCXXABI().LoadVTablePtr(
2653  *this, Address(Derived, getPointerAlign()), ClassDecl);
2654 
2655  EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc);
2656 
2657  if (MayBeNull) {
2658  Builder.CreateBr(ContBlock);
2659  EmitBlock(ContBlock);
2660  }
2661 }
2662 
2664  llvm::Value *VTable,
2665  CFITypeCheckKind TCK,
2666  SourceLocation Loc) {
2667  if (!CGM.getCodeGenOpts().SanitizeCfiCrossDso &&
2668  !CGM.HasHiddenLTOVisibility(RD))
2669  return;
2670 
2671  SanitizerMask M;
2672  llvm::SanitizerStatKind SSK;
2673  switch (TCK) {
2674  case CFITCK_VCall:
2675  M = SanitizerKind::CFIVCall;
2676  SSK = llvm::SanStat_CFI_VCall;
2677  break;
2678  case CFITCK_NVCall:
2679  M = SanitizerKind::CFINVCall;
2680  SSK = llvm::SanStat_CFI_NVCall;
2681  break;
2682  case CFITCK_DerivedCast:
2683  M = SanitizerKind::CFIDerivedCast;
2684  SSK = llvm::SanStat_CFI_DerivedCast;
2685  break;
2686  case CFITCK_UnrelatedCast:
2687  M = SanitizerKind::CFIUnrelatedCast;
2688  SSK = llvm::SanStat_CFI_UnrelatedCast;
2689  break;
2690  case CFITCK_ICall:
2691  llvm_unreachable("not expecting CFITCK_ICall");
2692  }
2693 
2694  std::string TypeName = RD->getQualifiedNameAsString();
2695  if (getContext().getSanitizerBlacklist().isBlacklistedType(M, TypeName))
2696  return;
2697 
2698  SanitizerScope SanScope(this);
2699  EmitSanitizerStatReport(SSK);
2700 
2701  llvm::Metadata *MD =
2702  CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
2703  llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD);
2704 
2705  llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
2706  llvm::Value *TypeTest = Builder.CreateCall(
2707  CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, TypeId});
2708 
2709  llvm::Constant *StaticData[] = {
2710  llvm::ConstantInt::get(Int8Ty, TCK),
2711  EmitCheckSourceLocation(Loc),
2712  EmitCheckTypeDescriptor(QualType(RD->getTypeForDecl(), 0)),
2713  };
2714 
2715  auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
2716  if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
2717  EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, CastedVTable, StaticData);
2718  return;
2719  }
2720 
2721  if (CGM.getCodeGenOpts().SanitizeTrap.has(M)) {
2722  EmitTrapCheck(TypeTest);
2723  return;
2724  }
2725 
2726  llvm::Value *AllVtables = llvm::MetadataAsValue::get(
2727  CGM.getLLVMContext(),
2728  llvm::MDString::get(CGM.getLLVMContext(), "all-vtables"));
2729  llvm::Value *ValidVtable = Builder.CreateCall(
2730  CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, AllVtables});
2731  EmitCheck(std::make_pair(TypeTest, M), SanitizerHandler::CFICheckFail,
2732  StaticData, {CastedVTable, ValidVtable});
2733 }
2734 
2736  if (!CGM.getCodeGenOpts().WholeProgramVTables ||
2737  !SanOpts.has(SanitizerKind::CFIVCall) ||
2738  !CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIVCall) ||
2739  !CGM.HasHiddenLTOVisibility(RD))
2740  return false;
2741 
2742  std::string TypeName = RD->getQualifiedNameAsString();
2744  SanitizerKind::CFIVCall, TypeName);
2745 }
2746 
2748  const CXXRecordDecl *RD, llvm::Value *VTable, uint64_t VTableByteOffset) {
2749  SanitizerScope SanScope(this);
2750 
2751  EmitSanitizerStatReport(llvm::SanStat_CFI_VCall);
2752 
2753  llvm::Metadata *MD =
2754  CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0));
2755  llvm::Value *TypeId = llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD);
2756 
2757  llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
2758  llvm::Value *CheckedLoad = Builder.CreateCall(
2759  CGM.getIntrinsic(llvm::Intrinsic::type_checked_load),
2760  {CastedVTable, llvm::ConstantInt::get(Int32Ty, VTableByteOffset),
2761  TypeId});
2762  llvm::Value *CheckResult = Builder.CreateExtractValue(CheckedLoad, 1);
2763 
2764  EmitCheck(std::make_pair(CheckResult, SanitizerKind::CFIVCall),
2765  SanitizerHandler::CFICheckFail, nullptr, nullptr);
2766 
2767  return Builder.CreateBitCast(
2768  Builder.CreateExtractValue(CheckedLoad, 0),
2769  cast<llvm::PointerType>(VTable->getType())->getElementType());
2770 }
2771 
2773  const CXXMethodDecl *callOperator,
2774  CallArgList &callArgs) {
2775  // Get the address of the call operator.
2776  const CGFunctionInfo &calleeFnInfo =
2777  CGM.getTypes().arrangeCXXMethodDeclaration(callOperator);
2778  llvm::Constant *calleePtr =
2779  CGM.GetAddrOfFunction(GlobalDecl(callOperator),
2780  CGM.getTypes().GetFunctionType(calleeFnInfo));
2781 
2782  // Prepare the return slot.
2783  const FunctionProtoType *FPT =
2784  callOperator->getType()->castAs<FunctionProtoType>();
2785  QualType resultType = FPT->getReturnType();
2786  ReturnValueSlot returnSlot;
2787  if (!resultType->isVoidType() &&
2788  calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2789  !hasScalarEvaluationKind(calleeFnInfo.getReturnType()))
2790  returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified());
2791 
2792  // We don't need to separately arrange the call arguments because
2793  // the call can't be variadic anyway --- it's impossible to forward
2794  // variadic arguments.
2795 
2796  // Now emit our call.
2797  auto callee = CGCallee::forDirect(calleePtr, callOperator);
2798  RValue RV = EmitCall(calleeFnInfo, callee, returnSlot, callArgs);
2799 
2800  // If necessary, copy the returned value into the slot.
2801  if (!resultType->isVoidType() && returnSlot.isNull()) {
2802  if (getLangOpts().ObjCAutoRefCount && resultType->isObjCRetainableType()) {
2803  RV = RValue::get(EmitARCRetainAutoreleasedReturnValue(RV.getScalarVal()));
2804  }
2805  EmitReturnOfRValue(RV, resultType);
2806  } else
2807  EmitBranchThroughCleanup(ReturnBlock);
2808 }
2809 
2811  const BlockDecl *BD = BlockInfo->getBlockDecl();
2812  const VarDecl *variable = BD->capture_begin()->getVariable();
2813  const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
2814  const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
2815 
2816  if (CallOp->isVariadic()) {
2817  // FIXME: Making this work correctly is nasty because it requires either
2818  // cloning the body of the call operator or making the call operator
2819  // forward.
2820  CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function");
2821  return;
2822  }
2823 
2824  // Start building arguments for forwarding call
2825  CallArgList CallArgs;
2826 
2827  QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2828  Address ThisPtr = GetAddrOfBlockDecl(variable, false);
2829  CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType);
2830 
2831  // Add the rest of the parameters.
2832  for (auto param : BD->parameters())
2833  EmitDelegateCallArg(CallArgs, param, param->getLocStart());
2834 
2835  assert(!Lambda->isGenericLambda() &&
2836  "generic lambda interconversion to block not implemented");
2837  EmitForwardingCallToLambda(CallOp, CallArgs);
2838 }
2839 
2841  const CXXRecordDecl *Lambda = MD->getParent();
2842 
2843  // Start building arguments for forwarding call
2844  CallArgList CallArgs;
2845 
2846  QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2847  llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType));
2848  CallArgs.add(RValue::get(ThisPtr), ThisType);
2849 
2850  // Add the rest of the parameters.
2851  for (auto Param : MD->parameters())
2852  EmitDelegateCallArg(CallArgs, Param, Param->getLocStart());
2853 
2854  const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
2855  // For a generic lambda, find the corresponding call operator specialization
2856  // to which the call to the static-invoker shall be forwarded.
2857  if (Lambda->isGenericLambda()) {
2858  assert(MD->isFunctionTemplateSpecialization());
2860  FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate();
2861  void *InsertPos = nullptr;
2862  FunctionDecl *CorrespondingCallOpSpecialization =
2863  CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
2864  assert(CorrespondingCallOpSpecialization);
2865  CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
2866  }
2867  EmitForwardingCallToLambda(CallOp, CallArgs);
2868 }
2869 
2871  if (MD->isVariadic()) {
2872  // FIXME: Making this work correctly is nasty because it requires either
2873  // cloning the body of the call operator or making the call operator forward.
2874  CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
2875  return;
2876  }
2877 
2878  EmitLambdaDelegatingInvokeBody(MD);
2879 }
bool isBaseInitializer() const
Determine whether this initializer is initializing a base class.
Definition: DeclCXX.h:2304
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:361
void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type)
EnterDtorCleanups - Enter the cleanups necessary to complete the given phase of destruction for a des...
Definition: CGClass.cpp:1748
Represents a function declaration or definition.
Definition: Decl.h:1714
int64_t QuantityType
Definition: CharUnits.h:40
LValue MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T)
Given a value of type T* that may not be to a complete object, construct an l-value with the natural ...
Expr * getInit() const
Get the initializer.
Definition: DeclCXX.h:2433
void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, CXXCtorType CtorType, const FunctionArgList &Args, SourceLocation Loc)
Definition: CGClass.cpp:2285
Complete object ctor.
Definition: ABI.h:26
A (possibly-)qualified type.
Definition: Type.h:655
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
Address EmitCXXMemberDataPointerAddress(const Expr *E, Address base, llvm::Value *memberPtr, const MemberPointerType *memberPtrType, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr)
Emit the address of a field using a member data pointer.
Definition: CGClass.cpp:129
void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type, FunctionArgList &Args)
EmitCtorPrologue - This routine generates necessary code to initialize base classes and non-static da...
Definition: CGClass.cpp:1252
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition: Type.cpp:2063
base_class_range bases()
Definition: DeclCXX.h:811
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:2561
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:805
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
bool isBlacklistedType(SanitizerMask Mask, StringRef MangledTypeName, StringRef Category=StringRef()) const
FunctionDecl * findSpecialization(ArrayRef< TemplateArgument > Args, void *&InsertPos)
Return the specialization with the provided arguments if it exists, otherwise return the insertion po...
capture_const_iterator capture_begin() const
Definition: Decl.h:3970
llvm::LLVMContext & getLLVMContext()
CharUnits getClassPointerAlignment(const CXXRecordDecl *CD)
Returns the assumed alignment of an opaque pointer to the given class.
Definition: CGClass.cpp:36
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
Stmt - This represents one statement.
Definition: Stmt.h:66
Address GetAddressOfDirectBaseInCompleteClass(Address Value, const CXXRecordDecl *Derived, const CXXRecordDecl *Base, bool BaseIsVirtual)
GetAddressOfBaseOfCompleteClass - Convert the given pointer to a complete class to the given direct b...
Definition: CGClass.cpp:199
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:223
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:826
Address GetAddressOfDerivedClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue)
Definition: CGClass.cpp:374
Checking the &#39;this&#39; pointer for a constructor call.
const Type * getTypeForDecl() const
Definition: Decl.h:2840
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
Definition: DeclCXX.h:245
Defines the C++ template declaration subclasses.
StringRef P
void EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD)
Definition: CGClass.cpp:2870
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition: Decl.cpp:3725
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:5998
llvm::Value * LoadCXXThis()
LoadCXXThis - Load the value of &#39;this&#39;.
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined...
Definition: Decl.h:2705
static bool isMemcpyEquivalentSpecialMember(const CXXMethodDecl *D)
Definition: CGClass.cpp:573
The base class of the type hierarchy.
Definition: Type.h:1421
Expr * getOperatorDeleteThisArg() const
Definition: DeclCXX.h:2722
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2650
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1239
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
CharUnits getAlignment() const
getAlignment - Get the record alignment in characters.
Definition: RecordLayout.h:171
LValue EmitLValueForFieldInitialization(LValue Base, const FieldDecl *Field)
EmitLValueForFieldInitialization - Like EmitLValueForField, except that if the Field is a reference...
Definition: CGExpr.cpp:3922
SourceLocation getLocEnd() const LLVM_READONLY
Definition: DeclBase.h:413
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2463
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition: Decl.h:3157
const RecordType * getAsUnionType() const
NOTE: getAs*ArrayType are methods on ASTContext.
Definition: Type.cpp:495
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition: DeclCXX.cpp:2293
bool isIndirectMemberInitializer() const
Definition: DeclCXX.h:2316
const CXXBaseSpecifier *const * path_const_iterator
Definition: Expr.h:2854
Address GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
Represents a variable declaration or definition.
Definition: Decl.h:812
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6456
const void * Store
Store - This opaque type encapsulates an immutable mapping from locations to values.
Definition: StoreRef.h:28
llvm::Value * GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, bool Delegating)
GetVTTParameter - Return the VTT parameter that should be passed to a base constructor/destructor wit...
Definition: CGClass.cpp:431
bool field_empty() const
Definition: Decl.h:3772
llvm::Value * getPointer() const
Definition: Address.h:38
void emitImplicitAssignmentOperatorBody(FunctionArgList &Args)
Definition: CGClass.cpp:1512
The collection of all-type qualifiers we support.
Definition: Type.h:154
void add(RValue rvalue, QualType type)
Definition: CGCall.h:285
bool mayInsertExtraPadding(bool EmitRemark=false) const
Whether we are allowed to insert extra padding between fields.
Definition: Decl.cpp:4074
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition: DeclCXX.cpp:2086
CharUnits getBaseOffset() const
getBaseOffset - Returns the base class offset.
Definition: BaseSubobject.h:46
static const CXXRecordDecl * LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD)
Definition: CGClass.cpp:2562
const TargetInfo & getTarget() const
bool usesInAlloca() const
Return true if this function uses inalloca arguments.
AggValueSlot::Overlap_t overlapForBaseInit(const CXXRecordDecl *RD, const CXXRecordDecl *BaseRD, bool IsVirtual)
Determine whether a base class initialization may overlap some other object.
Definition: CGExprAgg.cpp:1795
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1004
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
emitDestroy - Immediately perform the destruction of the given object.
Definition: CGDecl.cpp:1682
Address getAddress() const
Definition: CGValue.h:327
Indirect - Pass the argument indirectly via a hidden pointer with the specified alignment (0 indicate...
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:150
bool hasTrivialBody() const
Returns whether the function has a trivial body that does not require any specific codegen...
Definition: Decl.cpp:2637
llvm::SmallPtrSet< const CXXRecordDecl *, 4 > VisitedVirtualBasesSetTy
void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, Address This, Address Src, const CXXConstructExpr *E)
Definition: CGClass.cpp:2260
field_range fields() const
Definition: Decl.h:3764
bool isVolatileQualified() const
Definition: CGValue.h:258
Represents a member of a struct/union/class.
Definition: Decl.h:2521
bool ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD)
Returns whether we should perform a type checked load when loading a virtual function for virtual cal...
Definition: CGClass.cpp:2735
bool isReferenceType() const
Definition: Type.h:6061
void pushEHDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushEHDestroy - Push the standard destructor for the given type as an EH-only cleanup.
Definition: CGDecl.cpp:1621
void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc)
EmitVTablePtrCheck - Emit a check that VTable is a valid virtual table for RD using llvm...
Definition: CGClass.cpp:2663
InheritedConstructor getInheritedConstructor() const
Get the constructor that this inheriting constructor is based on.
Definition: DeclCXX.h:2659
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:81
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:231
void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD)
Definition: CGClass.cpp:2840
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2222
void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init)
Definition: CGClass.cpp:666
CharUnits getDynamicOffsetAlignment(CharUnits ActualAlign, const CXXRecordDecl *Class, CharUnits ExpectedTargetAlign)
Given a class pointer with an actual known alignment, and the expected alignment of an object at a dy...
Definition: CGClass.cpp:70
void EmitInheritedCXXConstructorCall(const CXXConstructorDecl *D, bool ForVirtualBase, Address This, bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E)
Emit a call to a constructor inherited from a base class, passing the current constructor&#39;s arguments...
Definition: CGClass.cpp:2140
QualType getThisType(ASTContext &C) const
Returns the type of the this pointer.
Definition: DeclCXX.cpp:2138
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:2599
bool isDelegatingConstructor() const
Determine whether this constructor is a delegating constructor.
Definition: DeclCXX.h:2582
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:2662
Base object ctor.
Definition: ABI.h:27
bool isBaseVirtual() const
Returns whether the base is virtual or not.
Definition: DeclCXX.h:2357
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
void EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, llvm::Value *VTable, SourceLocation Loc)
If whole-program virtual table optimization is enabled, emit an assumption that VTable is a member of...
Definition: CGClass.cpp:2587
uint32_t Offset
Definition: CacheTokens.cpp:43
bool isTriviallyCopyableType(const ASTContext &Context) const
Return true if this is a trivially copyable type (C++0x [basic.types]p9)
Definition: Type.cpp:2167
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:3279
CharUnits StorageOffset
The offset of the bitfield storage from the start of the struct.
ArrayRef< NamedDecl * > chain() const
Definition: Decl.h:2789
unsigned char PointerWidthInBits
The width of a pointer into the generic address space.
Deleting dtor.
Definition: ABI.h:35
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:67
bool isComplex() const
Definition: CGValue.h:53
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3065
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind...
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:66
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraPrefixArgs, unsigned ExtraSuffixArgs, bool PassProtoArgs=true)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:390
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:259
const Type * getClass() const
Definition: Type.h:2628
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, QualType DeleteTy, llvm::Value *NumElements=nullptr, CharUnits CookieSize=CharUnits())
Definition: CGExprCXX.cpp:1744
param_type_iterator param_type_begin() const
Definition: Type.h:3737
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
bool isAnyMemberInitializer() const
Definition: DeclCXX.h:2312
base_class_iterator bases_begin()
Definition: DeclCXX.h:818
void ForceCleanup(std::initializer_list< llvm::Value **> ValuesToReload={})
Force the emission of cleanups now, instead of waiting until this object is destroyed.
bool isSimple() const
Definition: CGValue.h:252
FieldDecl * getAnyMember() const
Definition: DeclCXX.h:2377
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1468
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1663
FieldDecl * getMember() const
If this is a member initializer, returns the declaration of the non-static data member being initiali...
Definition: DeclCXX.h:2371
bool isAbstract() const
Determine whether this class has a pure virtual function.
Definition: DeclCXX.h:1334
static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init)
Definition: CGClass.cpp:520
Address getAggregateAddress() const
getAggregateAddr() - Return the Value* of the address of the aggregate.
Definition: CGValue.h:71
init_iterator init_begin()
Retrieve an iterator to the first initializer.
Definition: DeclCXX.h:2523
bool isAnonymousStructOrUnion() const
Whether this is an anonymous struct or union.
Definition: Decl.h:3653
Represents the this expression in C++.
Definition: ExprCXX.h:945
LValue EmitLValueForField(LValue Base, const FieldDecl *Field)
Definition: CGExpr.cpp:3806
llvm::Constant * CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false)
Create a new runtime function with the specified type and name.
bool hasAttr() const
Definition: DeclBase.h:536
CanQualType getReturnType() const
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:609
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1590
Represents a prototype with parameter type info, e.g.
Definition: Type.h:3369
llvm::CallInst * EmitNounwindRuntimeCall(llvm::Value *callee, const Twine &name="")
bool isDynamicClass() const
Definition: DeclCXX.h:789
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1302
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:39
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
AggValueSlot::Overlap_t overlapForFieldInit(const FieldDecl *FD)
Determine whether a field initialization may overlap some other object.
static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF, CXXCtorInitializer *MemberInit, LValue &LHS)
Definition: CGClass.cpp:590
CXXCtorInitializer *const * init_const_iterator
Iterates through the member/base initializer list.
Definition: DeclCXX.h:2512
ASTRecordLayout - This class contains layout information for one RecordDecl, which is a struct/union/...
Definition: RecordLayout.h:39
llvm::Value * GetVTablePtr(Address This, llvm::Type *VTableTy, const CXXRecordDecl *VTableClass)
GetVTablePtr - Return the Value of the vtable pointer member pointed to by This.
Definition: CGClass.cpp:2537
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
Pepresents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:3838
Expr - This represents one expression.
Definition: Expr.h:106
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition: DeclCXX.cpp:2313
SourceLocation End
bool isVariadic() const
Whether this function is variadic.
Definition: Decl.cpp:2620
bool isDefaulted() const
Whether this function is defaulted per C++0x.
Definition: Decl.h:2058
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
const FunctionProtoType * T
void EmitVTableAssumptionLoad(const VPtr &vptr, Address This)
Emit assumption that vptr load == global vtable.
Definition: CGClass.cpp:2231
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6519
void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, const ArrayType *ArrayTy, Address ArrayPtr, const CXXConstructExpr *E, bool ZeroInitialization=false)
EmitCXXAggrConstructorCall - Emit a loop to call a particular constructor for each of several members...
Definition: CGClass.cpp:1870
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:134
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2686
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition: DeclBase.h:552
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition: Decl.cpp:3400
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl...
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:44
arg_range arguments()
Definition: ExprCXX.h:1348
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
Definition: RecordLayout.h:208
unsigned getFieldCount() const
getFieldCount - Get the number of fields in the layout.
Definition: RecordLayout.h:177
void EmitAsanPrologueOrEpilogue(bool Prologue)
Definition: CGClass.cpp:753
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition: Decl.h:2386
llvm::LLVMContext & getLLVMContext()
const CXXRecordDecl * getBase() const
getBase - Returns the base class declaration.
Definition: BaseSubobject.h:43
bool isPolymorphic() const
Whether this class is polymorphic (C++ [class.virtual]), which means that the class contains or inher...
Definition: DeclCXX.h:1327
Base object dtor.
Definition: ABI.h:37
void EmitConstructorBody(FunctionArgList &Args)
EmitConstructorBody - Emits the body of the current constructor.
Definition: CGClass.cpp:816
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:197
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T)
QualType getRecordType(const RecordDecl *Decl) const
A scoped helper to set the current debug location to an inlined location.
Definition: CGDebugInfo.h:696
void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator, CallArgList &CallArgs)
Definition: CGClass.cpp:2772
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1782
const TargetInfo & getTarget() const
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
Definition: ASTContext.h:1369
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition: Decl.h:2050
const LangOptions & getLangOpts() const
ASTContext & getContext() const
The COMDAT used for dtors.
Definition: ABI.h:38
const SanitizerBlacklist & getSanitizerBlacklist() const
Definition: ASTContext.h:691
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:35
std::pair< CharUnits, CharUnits > getTypeInfoDataSizeInChars(QualType T) const
static const RecordType * getRecordType(QualType QT)
Checks that the passed in QualType either is of RecordType or points to RecordType.
The l-value was considered opaque, so the alignment was determined from a type.
RecordDecl * getDecl() const
Definition: Type.h:4082
static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor)
CanSkipVTablePointerInitialization - Check whether we need to initialize any vtable pointers before c...
Definition: CGClass.cpp:1384
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
Definition: RecordLayout.h:181
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition: Sanitizers.h:61
Enumerates target-specific builtins in their own namespaces within namespace clang.
static AggValueSlot forAddr(Address addr, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed)
forAddr - Make a slot for an aggregate value.
Definition: CGValue.h:506
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:142
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition: ExprCXX.h:1400
void PushDestructorCleanup(QualType T, Address Addr)
PushDestructorCleanup - Push a cleanup to call the complete-object destructor of an object of the giv...
Definition: CGClass.cpp:2394
Encodes a location in the source.
body_range body()
Definition: Stmt.h:640
QualType getReturnType() const
Definition: Type.h:3302
Expr * getSubExpr() const
Definition: Expr.h:1809
void EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This)
Emit assumption load for all bases.
Definition: CGClass.cpp:2252
bool inheritingCtorHasParams(const InheritedConstructor &Inherited, CXXCtorType Type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Definition: CGCall.cpp:290
Represents a call to a member function that may be written either with member call syntax (e...
Definition: ExprCXX.h:164
const Decl * getDecl() const
Definition: GlobalDecl.h:64
An aggregate value slot.
Definition: CGValue.h:437
init_iterator init_end()
Retrieve an iterator past the last initializer.
Definition: DeclCXX.h:2532
A scoped helper to set the current debug location to the specified location or preferred location of ...
Definition: CGDebugInfo.h:641
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2031
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:2354
void EmitStmt(const Stmt *S, ArrayRef< const Attr *> Attrs=None)
EmitStmt - Emit the code for the statement.
Definition: CGStmt.cpp:48
virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, FunctionArgList &Args) const =0
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2245
SanitizerSet SanOpts
Sanitizers enabled for this function.
bool isGenericLambda() const
Determine whether this class describes a generic lambda function object (i.e.
Definition: DeclCXX.cpp:1325
An aligned address.
Definition: Address.h:25
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:2902
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
Definition: Type.h:1166
All available information about a concrete callee.
Definition: CGCall.h:67
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required...
Definition: DeclBase.cpp:397
Complete object dtor.
Definition: ABI.h:36
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed)
Definition: CGValue.h:530
void EmitInlinedInheritingCXXConstructorCall(const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase, bool Delegating, CallArgList &Args)
Emit a call to an inheriting constructor (that is, one that invokes a constructor inherited from a ba...
Definition: CGClass.cpp:2187
static void EmitBaseInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *BaseInit, CXXCtorType CtorType)
Definition: CGClass.cpp:526
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:216
std::pair< CharUnits, CharUnits > getTypeInfoInChars(const Type *T) const
SourceLocation getLocation() const LLVM_READONLY
Definition: ExprCXX.h:1451
void InitializeVTablePointer(const VPtr &vptr)
Initialize the vtable pointer of the given subobject.
Definition: CGClass.cpp:2404
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:356
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:59
CGFunctionInfo - Class to encapsulate the information about a function definition.
CharUnits alignmentAtOffset(CharUnits offset) const
Given that this is a non-zero alignment value, what is the alignment at the given offset...
Definition: CharUnits.h:190
Dataflow Directional Tag Classes.
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This)
Definition: CGClass.cpp:2364
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition: DeclCXX.cpp:1330
void EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived, bool MayBeNull, CFITypeCheckKind TCK, SourceLocation Loc)
Derived is the presumed address of an object of type T after a cast.
Definition: CGClass.cpp:2617
static bool canEmitDelegateCallArgs(CodeGenFunction &CGF, const CXXConstructorDecl *Ctor, CXXCtorType Type, CallArgList &Args)
Definition: CGClass.cpp:2037
A scope within which we are constructing the fields of an object which might use a CXXDefaultInitExpr...
Represents a field injected from an anonymous union/struct into the parent scope. ...
Definition: Decl.h:2767
SourceLocation getSourceLocation() const
Determine the source location of the initializer.
Definition: DeclCXX.cpp:2225
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:70
void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, const FunctionArgList &Args)
Definition: CGClass.cpp:2338
bool areArgsDestroyedLeftToRightInCallee() const
Are arguments to a call destroyed left to right in the callee? This is a fundamental language change...
Definition: TargetCXXABI.h:209
const CXXRecordDecl * getParent() const
Returns the parent of this method declaration, which is the class in which this method is defined...
Definition: DeclCXX.h:2151
bool isListInitialization() const
Whether this constructor call was written as list-initialization.
Definition: ExprCXX.h:1317
void InitializeVTablePointers(const CXXRecordDecl *ClassDecl)
Definition: CGClass.cpp:2523
const Type * getBaseClass() const
If this is a base class initializer, returns the type of the base class.
Definition: DeclCXX.cpp:2218
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition: Type.h:2594
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition: DeclCXX.cpp:2065
bool isBitField() const
Definition: CGValue.h:254
bool hasObjCLifetime() const
Definition: Type.h:342
bool isDestroyingOperatorDelete() const
Determine whether this is a destroying operator delete.
Definition: Decl.cpp:2815
ConstEvaluatedExprVisitor - This class visits &#39;const Expr *&#39;s.
CharUnits getVBaseAlignment(CharUnits DerivedAlign, const CXXRecordDecl *Derived, const CXXRecordDecl *VBase)
Returns the assumed alignment of a virtual base of a class.
Definition: CGClass.cpp:55
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type. ...
Definition: CGExprAgg.cpp:1772
JumpDest ReturnBlock
ReturnBlock - Unified return block.
const FunctionDecl * getOperatorDelete() const
Definition: DeclCXX.h:2718
uint64_t SanitizerMask
Definition: Sanitizers.h:26
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4072
StructorType getFromCtorType(CXXCtorType T)
Definition: CodeGenTypes.h:77
CodeGenTypes & getTypes() const
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2238
Address getBitFieldAddress() const
Definition: CGValue.h:355
IndirectFieldDecl * getIndirectMember() const
Definition: DeclCXX.h:2385
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:52
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:2044
llvm::Constant * GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd)
Returns the offset from a derived class to a class.
Definition: CGClass.cpp:177
Opcode getOpcode() const
Definition: Expr.h:1806
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition: ExprCXX.h:1365
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:445
Represents a base class of a C++ class.
Definition: DeclCXX.h:192
VPtrsVector getVTablePointers(const CXXRecordDecl *VTableClass)
Definition: CGClass.cpp:2455
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2207
A template argument list.
Definition: DeclTemplate.h:210
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:2465
static void EmitMemberInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *MemberInit, const CXXConstructorDecl *Constructor, FunctionArgList &Args)
Definition: CGClass.cpp:604
Represents a C++ struct/union/class.
Definition: DeclCXX.h:300
bool isVoidType() const
Definition: Type.h:6276
unsigned getBuiltinID() const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:2902
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:5852
static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor)
Checks whether the given constructor is a valid subject for the complete-to-base constructor delegati...
Definition: CGClass.cpp:705
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
Definition: RecordLayout.h:193
void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase, bool Delegating, Address This, const CXXConstructExpr *E, AggValueSlot::Overlap_t Overlap)
Definition: CGClass.cpp:2000
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition: Stmt.cpp:266
void EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc)
EmitVTablePtrCheckForCall - Virtual method MD is being called via VTable.
Definition: CGClass.cpp:2607
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2283
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, CastExpr::path_const_iterator End)
Definition: CGClass.cpp:149
static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit)
Definition: CGClass.cpp:1243
bool hasVolatile() const
Definition: Type.h:278
CGCXXABI & getCXXABI() const
std::string getQualifiedNameAsString() const
Definition: Decl.cpp:1500
static Address ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, Address addr, CharUnits nonVirtualOffset, llvm::Value *virtualOffset, const CXXRecordDecl *derivedClass, const CXXRecordDecl *nearestVBase)
Definition: CGClass.cpp:227
Struct with all information about dynamic [sub]class needed to set vptr.
void EmitAggregateCopy(LValue Dest, LValue Src, QualType EltTy, AggValueSlot::Overlap_t MayOverlap, bool isVolatile=false)
EmitAggregateCopy - Emit an aggregate copy.
Definition: CGExprAgg.cpp:1818
static RValue get(llvm::Value *V)
Definition: CGValue.h:86
bool isUnion() const
Definition: Decl.h:3225
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:3927
void EmitDestructorBody(FunctionArgList &Args)
EmitDestructorBody - Emits the body of the current destructor.
Definition: CGClass.cpp:1402
__DEVICE__ int min(int __a, int __b)
void EmitBranchThroughCleanup(JumpDest Dest)
EmitBranchThroughCleanup - Emit a branch from the current insert block through the normal cleanup han...
Definition: CGCleanup.cpp:1039
QualType getType() const
Definition: Decl.h:647
unsigned getNumArgs() const
Definition: ExprCXX.h:1362
LValue - This represents an lvalue references.
Definition: CGValue.h:167
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:147
static bool HasTrivialDestructorBody(ASTContext &Context, const CXXRecordDecl *BaseClassDecl, const CXXRecordDecl *MostDerivedClassDecl)
Definition: CGClass.cpp:1321
Notes how many arguments were added to the beginning (Prefix) and ending (Suffix) of an arg list...
Definition: CGCXXABI.h:300
const LangOptions & getLangOpts() const
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:2963
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
Address GetAddressOfBaseClass(Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue, SourceLocation Loc)
GetAddressOfBaseClass - This function will add the necessary delta to the load of &#39;this&#39; and returns ...
Definition: CGClass.cpp:267
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:260
const LangOptions & getLangOpts() const
Definition: ASTContext.h:689
base_class_range vbases()
Definition: DeclCXX.h:828
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:2710
Declaration of a template function.
Definition: DeclTemplate.h:968
static bool FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field)
Definition: CGClass.cpp:1364
llvm::Value * EmitVTableTypeCheckedLoad(const CXXRecordDecl *RD, llvm::Value *VTable, uint64_t VTableByteOffset)
Emit a type checked load from the given vtable.
Definition: CGClass.cpp:2747
bool isScalar() const
Definition: CGValue.h:52
QualType getPointeeType() const
Definition: Type.h:2614
Structure with information about how a bitfield should be accessed.
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
method_range methods() const
Definition: DeclCXX.h:853
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:291