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