clang  3.7.0
CGClass.cpp
Go to the documentation of this file.
1 //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code dealing with C++ code generation of classes
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGBlocks.h"
15 #include "CGCXXABI.h"
16 #include "CGDebugInfo.h"
17 #include "CGRecordLayout.h"
18 #include "CodeGenFunction.h"
20 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/RecordLayout.h"
23 #include "clang/AST/StmtCXX.h"
27 #include "llvm/IR/Intrinsics.h"
28 
29 using namespace clang;
30 using namespace CodeGen;
31 
33  const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start,
36 
37  const ASTContext &Context = getContext();
38  const CXXRecordDecl *RD = DerivedClass;
39 
40  for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
41  const CXXBaseSpecifier *Base = *I;
42  assert(!Base->isVirtual() && "Should not see virtual bases here!");
43 
44  // Get the layout.
45  const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD);
46 
47  const CXXRecordDecl *BaseDecl =
48  cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
49 
50  // Add the offset.
51  Offset += Layout.getBaseClassOffset(BaseDecl);
52 
53  RD = BaseDecl;
54  }
55 
56  return Offset;
57 }
58 
59 llvm::Constant *
63  assert(PathBegin != PathEnd && "Base path should not be empty!");
64 
66  computeNonVirtualBaseClassOffset(ClassDecl, PathBegin, PathEnd);
67  if (Offset.isZero())
68  return nullptr;
69 
70  llvm::Type *PtrDiffTy =
71  Types.ConvertType(getContext().getPointerDiffType());
72 
73  return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
74 }
75 
76 /// Gets the address of a direct base class within a complete object.
77 /// This should only be used for (1) non-virtual bases or (2) virtual bases
78 /// when the type is known to be complete (e.g. in complete destructors).
79 ///
80 /// The object pointed to by 'This' is assumed to be non-null.
83  const CXXRecordDecl *Derived,
84  const CXXRecordDecl *Base,
85  bool BaseIsVirtual) {
86  // 'this' must be a pointer (in some address space) to Derived.
87  assert(This->getType()->isPointerTy() &&
88  cast<llvm::PointerType>(This->getType())->getElementType()
89  == ConvertType(Derived));
90 
91  // Compute the offset of the virtual base.
93  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived);
94  if (BaseIsVirtual)
95  Offset = Layout.getVBaseClassOffset(Base);
96  else
97  Offset = Layout.getBaseClassOffset(Base);
98 
99  // Shift and cast down to the base type.
100  // TODO: for complete types, this should be possible with a GEP.
101  llvm::Value *V = This;
102  if (Offset.isPositive()) {
103  V = Builder.CreateBitCast(V, Int8PtrTy);
104  V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity());
105  }
106  V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo());
107 
108  return V;
109 }
110 
111 static llvm::Value *
113  CharUnits nonVirtualOffset,
114  llvm::Value *virtualOffset) {
115  // Assert that we have something to do.
116  assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
117 
118  // Compute the offset from the static and dynamic components.
119  llvm::Value *baseOffset;
120  if (!nonVirtualOffset.isZero()) {
121  baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy,
122  nonVirtualOffset.getQuantity());
123  if (virtualOffset) {
124  baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset);
125  }
126  } else {
127  baseOffset = virtualOffset;
128  }
129 
130  // Apply the base offset.
131  ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy);
132  ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr");
133  return ptr;
134 }
135 
137  llvm::Value *Value, const CXXRecordDecl *Derived,
139  CastExpr::path_const_iterator PathEnd, bool NullCheckValue,
140  SourceLocation Loc) {
141  assert(PathBegin != PathEnd && "Base path should not be empty!");
142 
143  CastExpr::path_const_iterator Start = PathBegin;
144  const CXXRecordDecl *VBase = nullptr;
145 
146  // Sema has done some convenient canonicalization here: if the
147  // access path involved any virtual steps, the conversion path will
148  // *start* with a step down to the correct virtual base subobject,
149  // and hence will not require any further steps.
150  if ((*Start)->isVirtual()) {
151  VBase =
152  cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl());
153  ++Start;
154  }
155 
156  // Compute the static offset of the ultimate destination within its
157  // allocating subobject (the virtual base, if there is one, or else
158  // the "complete" object that we see).
160  VBase ? VBase : Derived, Start, PathEnd);
161 
162  // If there's a virtual step, we can sometimes "devirtualize" it.
163  // For now, that's limited to when the derived type is final.
164  // TODO: "devirtualize" this for accesses to known-complete objects.
165  if (VBase && Derived->hasAttr<FinalAttr>()) {
166  const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived);
167  CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase);
168  NonVirtualOffset += vBaseOffset;
169  VBase = nullptr; // we no longer have a virtual step
170  }
171 
172  // Get the base pointer type.
173  llvm::Type *BasePtrTy =
174  ConvertType((PathEnd[-1])->getType())->getPointerTo();
175 
176  QualType DerivedTy = getContext().getRecordType(Derived);
177  CharUnits DerivedAlign = getContext().getTypeAlignInChars(DerivedTy);
178 
179  // If the static offset is zero and we don't have a virtual step,
180  // just do a bitcast; null checks are unnecessary.
181  if (NonVirtualOffset.isZero() && !VBase) {
182  if (sanitizePerformTypeCheck()) {
183  EmitTypeCheck(TCK_Upcast, Loc, Value, DerivedTy, DerivedAlign,
184  !NullCheckValue);
185  }
186  return Builder.CreateBitCast(Value, BasePtrTy);
187  }
188 
189  llvm::BasicBlock *origBB = nullptr;
190  llvm::BasicBlock *endBB = nullptr;
191 
192  // Skip over the offset (and the vtable load) if we're supposed to
193  // null-check the pointer.
194  if (NullCheckValue) {
195  origBB = Builder.GetInsertBlock();
196  llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull");
197  endBB = createBasicBlock("cast.end");
198 
199  llvm::Value *isNull = Builder.CreateIsNull(Value);
200  Builder.CreateCondBr(isNull, endBB, notNullBB);
201  EmitBlock(notNullBB);
202  }
203 
204  if (sanitizePerformTypeCheck()) {
205  EmitTypeCheck(VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc, Value,
206  DerivedTy, DerivedAlign, true);
207  }
208 
209  // Compute the virtual offset.
210  llvm::Value *VirtualOffset = nullptr;
211  if (VBase) {
212  VirtualOffset =
213  CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase);
214  }
215 
216  // Apply both offsets.
217  Value = ApplyNonVirtualAndVirtualOffset(*this, Value,
218  NonVirtualOffset,
219  VirtualOffset);
220 
221  // Cast to the destination type.
222  Value = Builder.CreateBitCast(Value, BasePtrTy);
223 
224  // Build a phi if we needed a null check.
225  if (NullCheckValue) {
226  llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
227  Builder.CreateBr(endBB);
228  EmitBlock(endBB);
229 
230  llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result");
231  PHI->addIncoming(Value, notNullBB);
232  PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB);
233  Value = PHI;
234  }
235 
236  return Value;
237 }
238 
239 llvm::Value *
241  const CXXRecordDecl *Derived,
244  bool NullCheckValue) {
245  assert(PathBegin != PathEnd && "Base path should not be empty!");
246 
247  QualType DerivedTy =
248  getContext().getCanonicalType(getContext().getTagDeclType(Derived));
249  llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo();
250 
251  llvm::Value *NonVirtualOffset =
252  CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd);
253 
254  if (!NonVirtualOffset) {
255  // No offset, we can just cast back.
256  return Builder.CreateBitCast(Value, DerivedPtrTy);
257  }
258 
259  llvm::BasicBlock *CastNull = nullptr;
260  llvm::BasicBlock *CastNotNull = nullptr;
261  llvm::BasicBlock *CastEnd = nullptr;
262 
263  if (NullCheckValue) {
264  CastNull = createBasicBlock("cast.null");
265  CastNotNull = createBasicBlock("cast.notnull");
266  CastEnd = createBasicBlock("cast.end");
267 
268  llvm::Value *IsNull = Builder.CreateIsNull(Value);
269  Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
270  EmitBlock(CastNotNull);
271  }
272 
273  // Apply the offset.
274  Value = Builder.CreateBitCast(Value, Int8PtrTy);
275  Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset),
276  "sub.ptr");
277 
278  // Just cast.
279  Value = Builder.CreateBitCast(Value, DerivedPtrTy);
280 
281  if (NullCheckValue) {
282  Builder.CreateBr(CastEnd);
283  EmitBlock(CastNull);
284  Builder.CreateBr(CastEnd);
285  EmitBlock(CastEnd);
286 
287  llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2);
288  PHI->addIncoming(Value, CastNotNull);
289  PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()),
290  CastNull);
291  Value = PHI;
292  }
293 
294  return Value;
295 }
296 
298  bool ForVirtualBase,
299  bool Delegating) {
300  if (!CGM.getCXXABI().NeedsVTTParameter(GD)) {
301  // This constructor/destructor does not need a VTT parameter.
302  return nullptr;
303  }
304 
305  const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent();
306  const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent();
307 
308  llvm::Value *VTT;
309 
310  uint64_t SubVTTIndex;
311 
312  if (Delegating) {
313  // If this is a delegating constructor call, just load the VTT.
314  return LoadCXXVTT();
315  } else if (RD == Base) {
316  // If the record matches the base, this is the complete ctor/dtor
317  // variant calling the base variant in a class with virtual bases.
318  assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) &&
319  "doing no-op VTT offset in base dtor/ctor?");
320  assert(!ForVirtualBase && "Can't have same class as virtual base!");
321  SubVTTIndex = 0;
322  } else {
323  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
324  CharUnits BaseOffset = ForVirtualBase ?
325  Layout.getVBaseClassOffset(Base) :
326  Layout.getBaseClassOffset(Base);
327 
328  SubVTTIndex =
329  CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset));
330  assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
331  }
332 
334  // A VTT parameter was passed to the constructor, use it.
335  VTT = LoadCXXVTT();
336  VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex);
337  } else {
338  // We're the complete constructor, so get the VTT by name.
339  VTT = CGM.getVTables().GetAddrOfVTT(RD);
340  VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex);
341  }
342 
343  return VTT;
344 }
345 
346 namespace {
347  /// Call the destructor for a direct base class.
348  struct CallBaseDtor : EHScopeStack::Cleanup {
349  const CXXRecordDecl *BaseClass;
350  bool BaseIsVirtual;
351  CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
352  : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
353 
354  void Emit(CodeGenFunction &CGF, Flags flags) override {
355  const CXXRecordDecl *DerivedClass =
356  cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent();
357 
358  const CXXDestructorDecl *D = BaseClass->getDestructor();
359  llvm::Value *Addr =
361  DerivedClass, BaseClass,
362  BaseIsVirtual);
363  CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual,
364  /*Delegating=*/false, Addr);
365  }
366  };
367 
368  /// A visitor which checks whether an initializer uses 'this' in a
369  /// way which requires the vtable to be properly set.
370  struct DynamicThisUseChecker : ConstEvaluatedExprVisitor<DynamicThisUseChecker> {
372 
373  bool UsesThis;
374 
375  DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {}
376 
377  // Black-list all explicit and implicit references to 'this'.
378  //
379  // Do we need to worry about external references to 'this' derived
380  // from arbitrary code? If so, then anything which runs arbitrary
381  // external code might potentially access the vtable.
382  void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; }
383  };
384 }
385 
386 static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
387  DynamicThisUseChecker Checker(C);
388  Checker.Visit(Init);
389  return Checker.UsesThis;
390 }
391 
393  const CXXRecordDecl *ClassDecl,
394  CXXCtorInitializer *BaseInit,
395  CXXCtorType CtorType) {
396  assert(BaseInit->isBaseInitializer() &&
397  "Must have base initializer!");
398 
399  llvm::Value *ThisPtr = CGF.LoadCXXThis();
400 
401  const Type *BaseType = BaseInit->getBaseClass();
402  CXXRecordDecl *BaseClassDecl =
403  cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl());
404 
405  bool isBaseVirtual = BaseInit->isBaseVirtual();
406 
407  // The base constructor doesn't construct virtual bases.
408  if (CtorType == Ctor_Base && isBaseVirtual)
409  return;
410 
411  // If the initializer for the base (other than the constructor
412  // itself) accesses 'this' in any way, we need to initialize the
413  // vtables.
414  if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit()))
415  CGF.InitializeVTablePointers(ClassDecl);
416 
417  // We can pretend to be a complete class because it only matters for
418  // virtual bases, and we only do virtual bases for complete ctors.
419  llvm::Value *V =
420  CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl,
421  BaseClassDecl,
422  isBaseVirtual);
423  CharUnits Alignment = CGF.getContext().getTypeAlignInChars(BaseType);
424  AggValueSlot AggSlot =
425  AggValueSlot::forAddr(V, Alignment, Qualifiers(),
429 
430  CGF.EmitAggExpr(BaseInit->getInit(), AggSlot);
431 
432  if (CGF.CGM.getLangOpts().Exceptions &&
433  !BaseClassDecl->hasTrivialDestructor())
434  CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl,
435  isBaseVirtual);
436 }
437 
439  LValue LHS,
440  Expr *Init,
441  llvm::Value *ArrayIndexVar,
442  QualType T,
443  ArrayRef<VarDecl *> ArrayIndexes,
444  unsigned Index) {
445  if (Index == ArrayIndexes.size()) {
446  LValue LV = LHS;
447 
448  if (ArrayIndexVar) {
449  // If we have an array index variable, load it and use it as an offset.
450  // Then, increment the value.
451  llvm::Value *Dest = LHS.getAddress();
452  llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar);
453  Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress");
454  llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1);
455  Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc");
456  CGF.Builder.CreateStore(Next, ArrayIndexVar);
457 
458  // Update the LValue.
459  LV.setAddress(Dest);
460  CharUnits Align = CGF.getContext().getTypeAlignInChars(T);
461  LV.setAlignment(std::min(Align, LV.getAlignment()));
462  }
463 
464  switch (CGF.getEvaluationKind(T)) {
465  case TEK_Scalar:
466  CGF.EmitScalarInit(Init, /*decl*/ nullptr, LV, false);
467  break;
468  case TEK_Complex:
469  CGF.EmitComplexExprIntoLValue(Init, LV, /*isInit*/ true);
470  break;
471  case TEK_Aggregate: {
472  AggValueSlot Slot =
477 
478  CGF.EmitAggExpr(Init, Slot);
479  break;
480  }
481  }
482 
483  return;
484  }
485 
486  const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T);
487  assert(Array && "Array initialization without the array type?");
488  llvm::Value *IndexVar
489  = CGF.GetAddrOfLocalVar(ArrayIndexes[Index]);
490  assert(IndexVar && "Array index variable not loaded");
491 
492  // Initialize this index variable to zero.
493  llvm::Value* Zero
494  = llvm::Constant::getNullValue(
495  CGF.ConvertType(CGF.getContext().getSizeType()));
496  CGF.Builder.CreateStore(Zero, IndexVar);
497 
498  // Start the loop with a block that tests the condition.
499  llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond");
500  llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end");
501 
502  CGF.EmitBlock(CondBlock);
503 
504  llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body");
505  // Generate: if (loop-index < number-of-elements) fall to the loop body,
506  // otherwise, go to the block after the for-loop.
507  uint64_t NumElements = Array->getSize().getZExtValue();
508  llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar);
509  llvm::Value *NumElementsPtr =
510  llvm::ConstantInt::get(Counter->getType(), NumElements);
511  llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr,
512  "isless");
513 
514  // If the condition is true, execute the body.
515  CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor);
516 
517  CGF.EmitBlock(ForBody);
518  llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc");
519 
520  // Inside the loop body recurse to emit the inner loop or, eventually, the
521  // constructor call.
522  EmitAggMemberInitializer(CGF, LHS, Init, ArrayIndexVar,
523  Array->getElementType(), ArrayIndexes, Index + 1);
524 
525  CGF.EmitBlock(ContinueBlock);
526 
527  // Emit the increment of the loop counter.
528  llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1);
529  Counter = CGF.Builder.CreateLoad(IndexVar);
530  NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc");
531  CGF.Builder.CreateStore(NextVal, IndexVar);
532 
533  // Finally, branch back up to the condition for the next iteration.
534  CGF.EmitBranch(CondBlock);
535 
536  // Emit the fall-through block.
537  CGF.EmitBlock(AfterFor, true);
538 }
539 
541  auto *CD = dyn_cast<CXXConstructorDecl>(D);
542  if (!(CD && CD->isCopyOrMoveConstructor()) &&
544  return false;
545 
546  // We can emit a memcpy for a trivial copy or move constructor/assignment.
547  if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding())
548  return true;
549 
550  // We *must* emit a memcpy for a defaulted union copy or move op.
551  if (D->getParent()->isUnion() && D->isDefaulted())
552  return true;
553 
554  return false;
555 }
556 
558  CXXCtorInitializer *MemberInit,
559  LValue &LHS) {
560  FieldDecl *Field = MemberInit->getAnyMember();
561  if (MemberInit->isIndirectMemberInitializer()) {
562  // If we are initializing an anonymous union field, drill down to the field.
563  IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
564  for (const auto *I : IndirectField->chain())
565  LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I));
566  } else {
567  LHS = CGF.EmitLValueForFieldInitialization(LHS, Field);
568  }
569 }
570 
572  const CXXRecordDecl *ClassDecl,
573  CXXCtorInitializer *MemberInit,
574  const CXXConstructorDecl *Constructor,
575  FunctionArgList &Args) {
576  ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation());
577  assert(MemberInit->isAnyMemberInitializer() &&
578  "Must have member initializer!");
579  assert(MemberInit->getInit() && "Must have initializer!");
580 
581  // non-static data member initializers.
582  FieldDecl *Field = MemberInit->getAnyMember();
583  QualType FieldType = Field->getType();
584 
585  llvm::Value *ThisPtr = CGF.LoadCXXThis();
586  QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
587  LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
588 
589  EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS);
590 
591  // Special case: if we are in a copy or move constructor, and we are copying
592  // an array of PODs or classes with trivial copy constructors, ignore the
593  // AST and perform the copy we know is equivalent.
594  // FIXME: This is hacky at best... if we had a bit more explicit information
595  // in the AST, we could generalize it more easily.
596  const ConstantArrayType *Array
597  = CGF.getContext().getAsConstantArrayType(FieldType);
598  if (Array && Constructor->isDefaulted() &&
599  Constructor->isCopyOrMoveConstructor()) {
600  QualType BaseElementTy = CGF.getContext().getBaseElementType(Array);
601  CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
602  if (BaseElementTy.isPODType(CGF.getContext()) ||
604  unsigned SrcArgIndex =
605  CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args);
606  llvm::Value *SrcPtr
607  = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex]));
608  LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
609  LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field);
610 
611  // Copy the aggregate.
612  CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType,
613  LHS.isVolatileQualified());
614  // Ensure that we destroy the objects if an exception is thrown later in
615  // the constructor.
616  QualType::DestructionKind dtorKind = FieldType.isDestructedType();
617  if (CGF.needsEHCleanup(dtorKind))
618  CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
619  return;
620  }
621  }
622 
623  ArrayRef<VarDecl *> ArrayIndexes;
624  if (MemberInit->getNumArrayIndices())
625  ArrayIndexes = MemberInit->getArrayIndexes();
626  CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit(), ArrayIndexes);
627 }
628 
630  FieldDecl *Field, LValue LHS, Expr *Init,
631  ArrayRef<VarDecl *> ArrayIndexes) {
632  QualType FieldType = Field->getType();
633  switch (getEvaluationKind(FieldType)) {
634  case TEK_Scalar:
635  if (LHS.isSimple()) {
636  EmitExprAsInit(Init, Field, LHS, false);
637  } else {
638  RValue RHS = RValue::get(EmitScalarExpr(Init));
639  EmitStoreThroughLValue(RHS, LHS);
640  }
641  break;
642  case TEK_Complex:
643  EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true);
644  break;
645  case TEK_Aggregate: {
646  llvm::Value *ArrayIndexVar = nullptr;
647  if (ArrayIndexes.size()) {
648  llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
649 
650  // The LHS is a pointer to the first object we'll be constructing, as
651  // a flat array.
652  QualType BaseElementTy = getContext().getBaseElementType(FieldType);
653  llvm::Type *BasePtr = ConvertType(BaseElementTy);
654  BasePtr = llvm::PointerType::getUnqual(BasePtr);
655  llvm::Value *BaseAddrPtr = Builder.CreateBitCast(LHS.getAddress(),
656  BasePtr);
657  LHS = MakeAddrLValue(BaseAddrPtr, BaseElementTy);
658 
659  // Create an array index that will be used to walk over all of the
660  // objects we're constructing.
661  ArrayIndexVar = CreateTempAlloca(SizeTy, "object.index");
662  llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy);
663  Builder.CreateStore(Zero, ArrayIndexVar);
664 
665 
666  // Emit the block variables for the array indices, if any.
667  for (unsigned I = 0, N = ArrayIndexes.size(); I != N; ++I)
668  EmitAutoVarDecl(*ArrayIndexes[I]);
669  }
670 
671  EmitAggMemberInitializer(*this, LHS, Init, ArrayIndexVar, FieldType,
672  ArrayIndexes, 0);
673  }
674  }
675 
676  // Ensure that we destroy this object if an exception is thrown
677  // later in the constructor.
678  QualType::DestructionKind dtorKind = FieldType.isDestructedType();
679  if (needsEHCleanup(dtorKind))
680  pushEHDestroy(dtorKind, LHS.getAddress(), FieldType);
681 }
682 
683 /// Checks whether the given constructor is a valid subject for the
684 /// complete-to-base constructor delegation optimization, i.e.
685 /// emitting the complete constructor as a simple call to the base
686 /// constructor.
688 
689  // Currently we disable the optimization for classes with virtual
690  // bases because (1) the addresses of parameter variables need to be
691  // consistent across all initializers but (2) the delegate function
692  // call necessarily creates a second copy of the parameter variable.
693  //
694  // The limiting example (purely theoretical AFAIK):
695  // struct A { A(int &c) { c++; } };
696  // struct B : virtual A {
697  // B(int count) : A(count) { printf("%d\n", count); }
698  // };
699  // ...although even this example could in principle be emitted as a
700  // delegation since the address of the parameter doesn't escape.
701  if (Ctor->getParent()->getNumVBases()) {
702  // TODO: white-list trivial vbase initializers. This case wouldn't
703  // be subject to the restrictions below.
704 
705  // TODO: white-list cases where:
706  // - there are no non-reference parameters to the constructor
707  // - the initializers don't access any non-reference parameters
708  // - the initializers don't take the address of non-reference
709  // parameters
710  // - etc.
711  // If we ever add any of the above cases, remember that:
712  // - function-try-blocks will always blacklist this optimization
713  // - we need to perform the constructor prologue and cleanup in
714  // EmitConstructorBody.
715 
716  return false;
717  }
718 
719  // We also disable the optimization for variadic functions because
720  // it's impossible to "re-pass" varargs.
721  if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic())
722  return false;
723 
724  // FIXME: Decide if we can do a delegation of a delegating constructor.
725  if (Ctor->isDelegatingConstructor())
726  return false;
727 
728  return true;
729 }
730 
731 // Emit code in ctor (Prologue==true) or dtor (Prologue==false)
732 // to poison the extra field paddings inserted under
733 // -fsanitize-address-field-padding=1|2.
736  const CXXRecordDecl *ClassDecl =
737  Prologue ? cast<CXXConstructorDecl>(CurGD.getDecl())->getParent()
738  : cast<CXXDestructorDecl>(CurGD.getDecl())->getParent();
739  if (!ClassDecl->mayInsertExtraPadding()) return;
740 
741  struct SizeAndOffset {
742  uint64_t Size;
743  uint64_t Offset;
744  };
745 
746  unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits();
747  const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl);
748 
749  // Populate sizes and offsets of fields.
751  for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i)
752  SSV[i].Offset =
753  Context.toCharUnitsFromBits(Info.getFieldOffset(i)).getQuantity();
754 
755  size_t NumFields = 0;
756  for (const auto *Field : ClassDecl->fields()) {
757  const FieldDecl *D = Field;
758  std::pair<CharUnits, CharUnits> FieldInfo =
759  Context.getTypeInfoInChars(D->getType());
760  CharUnits FieldSize = FieldInfo.first;
761  assert(NumFields < SSV.size());
762  SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity();
763  NumFields++;
764  }
765  assert(NumFields == SSV.size());
766  if (SSV.size() <= 1) return;
767 
768  // We will insert calls to __asan_* run-time functions.
769  // LLVM AddressSanitizer pass may decide to inline them later.
770  llvm::Type *Args[2] = {IntPtrTy, IntPtrTy};
771  llvm::FunctionType *FTy =
772  llvm::FunctionType::get(CGM.VoidTy, Args, false);
773  llvm::Constant *F = CGM.CreateRuntimeFunction(
774  FTy, Prologue ? "__asan_poison_intra_object_redzone"
775  : "__asan_unpoison_intra_object_redzone");
776 
777  llvm::Value *ThisPtr = LoadCXXThis();
778  ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy);
779  uint64_t TypeSize = Info.getNonVirtualSize().getQuantity();
780  // For each field check if it has sufficient padding,
781  // if so (un)poison it with a call.
782  for (size_t i = 0; i < SSV.size(); i++) {
783  uint64_t AsanAlignment = 8;
784  uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset;
785  uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size;
786  uint64_t EndOffset = SSV[i].Offset + SSV[i].Size;
787  if (PoisonSize < AsanAlignment || !SSV[i].Size ||
788  (NextField % AsanAlignment) != 0)
789  continue;
790  Builder.CreateCall(
791  F, {Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)),
792  Builder.getIntN(PtrSize, PoisonSize)});
793  }
794 }
795 
796 /// EmitConstructorBody - Emits the body of the current constructor.
799  const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl());
800  CXXCtorType CtorType = CurGD.getCtorType();
801 
803  CtorType == Ctor_Complete) &&
804  "can only generate complete ctor for this ABI");
805 
806  // Before we go any further, try the complete->base constructor
807  // delegation optimization.
808  if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
810  EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getLocEnd());
811  return;
812  }
813 
814  const FunctionDecl *Definition = 0;
815  Stmt *Body = Ctor->getBody(Definition);
816  assert(Definition == Ctor && "emitting wrong constructor body");
817 
818  // Enter the function-try-block before the constructor prologue if
819  // applicable.
820  bool IsTryBody = (Body && isa<CXXTryStmt>(Body));
821  if (IsTryBody)
822  EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
823 
825 
826  RunCleanupsScope RunCleanups(*this);
827 
828  // TODO: in restricted cases, we can emit the vbase initializers of
829  // a complete ctor and then delegate to the base ctor.
830 
831  // Emit the constructor prologue, i.e. the base and member
832  // initializers.
833  EmitCtorPrologue(Ctor, CtorType, Args);
834 
835  // Emit the body of the statement.
836  if (IsTryBody)
837  EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
838  else if (Body)
839  EmitStmt(Body);
840 
841  // Emit any cleanup blocks associated with the member or base
842  // initializers, which includes (along the exceptional path) the
843  // destructors for those members and bases that were fully
844  // constructed.
845  RunCleanups.ForceCleanup();
846 
847  if (IsTryBody)
848  ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
849 }
850 
851 namespace {
852  /// RAII object to indicate that codegen is copying the value representation
853  /// instead of the object representation. Useful when copying a struct or
854  /// class which has uninitialized members and we're only performing
855  /// lvalue-to-rvalue conversion on the object but not its members.
856  class CopyingValueRepresentation {
857  public:
858  explicit CopyingValueRepresentation(CodeGenFunction &CGF)
859  : CGF(CGF), OldSanOpts(CGF.SanOpts) {
860  CGF.SanOpts.set(SanitizerKind::Bool, false);
861  CGF.SanOpts.set(SanitizerKind::Enum, false);
862  }
863  ~CopyingValueRepresentation() {
864  CGF.SanOpts = OldSanOpts;
865  }
866  private:
867  CodeGenFunction &CGF;
868  SanitizerSet OldSanOpts;
869  };
870 }
871 
872 namespace {
873  class FieldMemcpyizer {
874  public:
875  FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl,
876  const VarDecl *SrcRec)
877  : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec),
878  RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)),
879  FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0),
880  LastFieldOffset(0), LastAddedFieldIndex(0) {}
881 
882  bool isMemcpyableField(FieldDecl *F) const {
883  // Never memcpy fields when we are adding poisoned paddings.
884  if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding)
885  return false;
886  Qualifiers Qual = F->getType().getQualifiers();
887  if (Qual.hasVolatile() || Qual.hasObjCLifetime())
888  return false;
889  return true;
890  }
891 
892  void addMemcpyableField(FieldDecl *F) {
893  if (!FirstField)
894  addInitialField(F);
895  else
896  addNextField(F);
897  }
898 
899  CharUnits getMemcpySize(uint64_t FirstByteOffset) const {
900  unsigned LastFieldSize =
901  LastField->isBitField() ?
902  LastField->getBitWidthValue(CGF.getContext()) :
903  CGF.getContext().getTypeSize(LastField->getType());
904  uint64_t MemcpySizeBits =
905  LastFieldOffset + LastFieldSize - FirstByteOffset +
906  CGF.getContext().getCharWidth() - 1;
907  CharUnits MemcpySize =
908  CGF.getContext().toCharUnitsFromBits(MemcpySizeBits);
909  return MemcpySize;
910  }
911 
912  void emitMemcpy() {
913  // Give the subclass a chance to bail out if it feels the memcpy isn't
914  // worth it (e.g. Hasn't aggregated enough data).
915  if (!FirstField) {
916  return;
917  }
918 
919  uint64_t FirstByteOffset;
920  if (FirstField->isBitField()) {
921  const CGRecordLayout &RL =
922  CGF.getTypes().getCGRecordLayout(FirstField->getParent());
923  const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField);
924  // FirstFieldOffset is not appropriate for bitfields,
925  // we need to use the storage offset instead.
926  FirstByteOffset = CGF.getContext().toBits(BFInfo.StorageOffset);
927  } else {
928  FirstByteOffset = FirstFieldOffset;
929  }
930 
931  CharUnits MemcpySize = getMemcpySize(FirstByteOffset);
932  QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
933  llvm::Value *ThisPtr = CGF.LoadCXXThis();
934  LValue DestLV = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
935  LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField);
936  llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec));
937  LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy);
938  LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField);
939 
940  CharUnits Offset = CGF.getContext().toCharUnitsFromBits(FirstByteOffset);
941  CharUnits Alignment = DestLV.getAlignment().alignmentAtOffset(Offset);
942 
943  emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddr() : Dest.getAddress(),
944  Src.isBitField() ? Src.getBitFieldAddr() : Src.getAddress(),
945  MemcpySize, Alignment);
946  reset();
947  }
948 
949  void reset() {
950  FirstField = nullptr;
951  }
952 
953  protected:
954  CodeGenFunction &CGF;
955  const CXXRecordDecl *ClassDecl;
956 
957  private:
958 
959  void emitMemcpyIR(llvm::Value *DestPtr, llvm::Value *SrcPtr,
960  CharUnits Size, CharUnits Alignment) {
961  llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
962  llvm::Type *DBP =
963  llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace());
964  DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP);
965 
966  llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
967  llvm::Type *SBP =
968  llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace());
969  SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP);
970 
971  CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity(),
972  Alignment.getQuantity());
973  }
974 
975  void addInitialField(FieldDecl *F) {
976  FirstField = F;
977  LastField = F;
978  FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex());
979  LastFieldOffset = FirstFieldOffset;
980  LastAddedFieldIndex = F->getFieldIndex();
981  return;
982  }
983 
984  void addNextField(FieldDecl *F) {
985  // For the most part, the following invariant will hold:
986  // F->getFieldIndex() == LastAddedFieldIndex + 1
987  // The one exception is that Sema won't add a copy-initializer for an
988  // unnamed bitfield, which will show up here as a gap in the sequence.
989  assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 &&
990  "Cannot aggregate fields out of order.");
991  LastAddedFieldIndex = F->getFieldIndex();
992 
993  // The 'first' and 'last' fields are chosen by offset, rather than field
994  // index. This allows the code to support bitfields, as well as regular
995  // fields.
996  uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex());
997  if (FOffset < FirstFieldOffset) {
998  FirstField = F;
999  FirstFieldOffset = FOffset;
1000  } else if (FOffset > LastFieldOffset) {
1001  LastField = F;
1002  LastFieldOffset = FOffset;
1003  }
1004  }
1005 
1006  const VarDecl *SrcRec;
1007  const ASTRecordLayout &RecLayout;
1008  FieldDecl *FirstField;
1009  FieldDecl *LastField;
1010  uint64_t FirstFieldOffset, LastFieldOffset;
1011  unsigned LastAddedFieldIndex;
1012  };
1013 
1014  class ConstructorMemcpyizer : public FieldMemcpyizer {
1015  private:
1016 
1017  /// Get source argument for copy constructor. Returns null if not a copy
1018  /// constructor.
1019  static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF,
1020  const CXXConstructorDecl *CD,
1021  FunctionArgList &Args) {
1022  if (CD->isCopyOrMoveConstructor() && CD->isDefaulted())
1023  return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)];
1024  return nullptr;
1025  }
1026 
1027  // Returns true if a CXXCtorInitializer represents a member initialization
1028  // that can be rolled into a memcpy.
1029  bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
1030  if (!MemcpyableCtor)
1031  return false;
1032  FieldDecl *Field = MemberInit->getMember();
1033  assert(Field && "No field for member init.");
1034  QualType FieldType = Field->getType();
1035  CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit());
1036 
1037  // Bail out on non-memcpyable, not-trivially-copyable members.
1038  if (!(CE && isMemcpyEquivalentSpecialMember(CE->getConstructor())) &&
1039  !(FieldType.isTriviallyCopyableType(CGF.getContext()) ||
1040  FieldType->isReferenceType()))
1041  return false;
1042 
1043  // Bail out on volatile fields.
1044  if (!isMemcpyableField(Field))
1045  return false;
1046 
1047  // Otherwise we're good.
1048  return true;
1049  }
1050 
1051  public:
1052  ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD,
1053  FunctionArgList &Args)
1054  : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)),
1055  ConstructorDecl(CD),
1056  MemcpyableCtor(CD->isDefaulted() &&
1057  CD->isCopyOrMoveConstructor() &&
1058  CGF.getLangOpts().getGC() == LangOptions::NonGC),
1059  Args(Args) { }
1060 
1061  void addMemberInitializer(CXXCtorInitializer *MemberInit) {
1062  if (isMemberInitMemcpyable(MemberInit)) {
1063  AggregatedInits.push_back(MemberInit);
1064  addMemcpyableField(MemberInit->getMember());
1065  } else {
1066  emitAggregatedInits();
1067  EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit,
1068  ConstructorDecl, Args);
1069  }
1070  }
1071 
1072  void emitAggregatedInits() {
1073  if (AggregatedInits.size() <= 1) {
1074  // This memcpy is too small to be worthwhile. Fall back on default
1075  // codegen.
1076  if (!AggregatedInits.empty()) {
1077  CopyingValueRepresentation CVR(CGF);
1078  EmitMemberInitializer(CGF, ConstructorDecl->getParent(),
1079  AggregatedInits[0], ConstructorDecl, Args);
1080  AggregatedInits.clear();
1081  }
1082  reset();
1083  return;
1084  }
1085 
1086  pushEHDestructors();
1087  emitMemcpy();
1088  AggregatedInits.clear();
1089  }
1090 
1091  void pushEHDestructors() {
1092  llvm::Value *ThisPtr = CGF.LoadCXXThis();
1093  QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl);
1094  LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy);
1095 
1096  for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
1097  CXXCtorInitializer *MemberInit = AggregatedInits[i];
1098  QualType FieldType = MemberInit->getAnyMember()->getType();
1099  QualType::DestructionKind dtorKind = FieldType.isDestructedType();
1100  if (!CGF.needsEHCleanup(dtorKind))
1101  continue;
1102  LValue FieldLHS = LHS;
1103  EmitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS);
1104  CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(), FieldType);
1105  }
1106  }
1107 
1108  void finish() {
1109  emitAggregatedInits();
1110  }
1111 
1112  private:
1113  const CXXConstructorDecl *ConstructorDecl;
1114  bool MemcpyableCtor;
1115  FunctionArgList &Args;
1116  SmallVector<CXXCtorInitializer*, 16> AggregatedInits;
1117  };
1118 
1119  class AssignmentMemcpyizer : public FieldMemcpyizer {
1120  private:
1121 
1122  // Returns the memcpyable field copied by the given statement, if one
1123  // exists. Otherwise returns null.
1124  FieldDecl *getMemcpyableField(Stmt *S) {
1125  if (!AssignmentsMemcpyable)
1126  return nullptr;
1127  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) {
1128  // Recognise trivial assignments.
1129  if (BO->getOpcode() != BO_Assign)
1130  return nullptr;
1131  MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS());
1132  if (!ME)
1133  return nullptr;
1134  FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1135  if (!Field || !isMemcpyableField(Field))
1136  return nullptr;
1137  Stmt *RHS = BO->getRHS();
1138  if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS))
1139  RHS = EC->getSubExpr();
1140  if (!RHS)
1141  return nullptr;
1142  MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS);
1143  if (dyn_cast<FieldDecl>(ME2->getMemberDecl()) != Field)
1144  return nullptr;
1145  return Field;
1146  } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) {
1147  CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl());
1148  if (!(MD && isMemcpyEquivalentSpecialMember(MD)))
1149  return nullptr;
1150  MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument());
1151  if (!IOA)
1152  return nullptr;
1153  FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl());
1154  if (!Field || !isMemcpyableField(Field))
1155  return nullptr;
1156  MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0));
1157  if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl()))
1158  return nullptr;
1159  return Field;
1160  } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
1161  FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl());
1162  if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
1163  return nullptr;
1164  Expr *DstPtr = CE->getArg(0);
1165  if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr))
1166  DstPtr = DC->getSubExpr();
1167  UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr);
1168  if (!DUO || DUO->getOpcode() != UO_AddrOf)
1169  return nullptr;
1170  MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr());
1171  if (!ME)
1172  return nullptr;
1173  FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
1174  if (!Field || !isMemcpyableField(Field))
1175  return nullptr;
1176  Expr *SrcPtr = CE->getArg(1);
1177  if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr))
1178  SrcPtr = SC->getSubExpr();
1179  UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr);
1180  if (!SUO || SUO->getOpcode() != UO_AddrOf)
1181  return nullptr;
1182  MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr());
1183  if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl()))
1184  return nullptr;
1185  return Field;
1186  }
1187 
1188  return nullptr;
1189  }
1190 
1191  bool AssignmentsMemcpyable;
1192  SmallVector<Stmt*, 16> AggregatedStmts;
1193 
1194  public:
1195 
1196  AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD,
1197  FunctionArgList &Args)
1198  : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
1199  AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
1200  assert(Args.size() == 2);
1201  }
1202 
1203  void emitAssignment(Stmt *S) {
1204  FieldDecl *F = getMemcpyableField(S);
1205  if (F) {
1206  addMemcpyableField(F);
1207  AggregatedStmts.push_back(S);
1208  } else {
1209  emitAggregatedStmts();
1210  CGF.EmitStmt(S);
1211  }
1212  }
1213 
1214  void emitAggregatedStmts() {
1215  if (AggregatedStmts.size() <= 1) {
1216  if (!AggregatedStmts.empty()) {
1217  CopyingValueRepresentation CVR(CGF);
1218  CGF.EmitStmt(AggregatedStmts[0]);
1219  }
1220  reset();
1221  }
1222 
1223  emitMemcpy();
1224  AggregatedStmts.clear();
1225  }
1226 
1227  void finish() {
1228  emitAggregatedStmts();
1229  }
1230  };
1231 
1232 }
1233 
1234 /// EmitCtorPrologue - This routine generates necessary code to initialize
1235 /// base classes and non-static data members belonging to this constructor.
1237  CXXCtorType CtorType,
1238  FunctionArgList &Args) {
1239  if (CD->isDelegatingConstructor())
1240  return EmitDelegatingCXXConstructorCall(CD, Args);
1241 
1242  const CXXRecordDecl *ClassDecl = CD->getParent();
1243 
1245  E = CD->init_end();
1246 
1247  llvm::BasicBlock *BaseCtorContinueBB = nullptr;
1248  if (ClassDecl->getNumVBases() &&
1250  // The ABIs that don't have constructor variants need to put a branch
1251  // before the virtual base initialization code.
1252  BaseCtorContinueBB =
1253  CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl);
1254  assert(BaseCtorContinueBB);
1255  }
1256 
1257  // Virtual base initializers first.
1258  for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) {
1259  EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1260  }
1261 
1262  if (BaseCtorContinueBB) {
1263  // Complete object handler should continue to the remaining initializers.
1264  Builder.CreateBr(BaseCtorContinueBB);
1265  EmitBlock(BaseCtorContinueBB);
1266  }
1267 
1268  // Then, non-virtual base initializers.
1269  for (; B != E && (*B)->isBaseInitializer(); B++) {
1270  assert(!(*B)->isBaseVirtual());
1271  EmitBaseInitializer(*this, ClassDecl, *B, CtorType);
1272  }
1273 
1274  InitializeVTablePointers(ClassDecl);
1275 
1276  // And finally, initialize class members.
1277  FieldConstructionScope FCS(*this, CXXThisValue);
1278  ConstructorMemcpyizer CM(*this, CD, Args);
1279  for (; B != E; B++) {
1280  CXXCtorInitializer *Member = (*B);
1281  assert(!Member->isBaseInitializer());
1282  assert(Member->isAnyMemberInitializer() &&
1283  "Delegating initializer on non-delegating constructor");
1284  CM.addMemberInitializer(Member);
1285  }
1286  CM.finish();
1287 }
1288 
1289 static bool
1291 
1292 static bool
1294  const CXXRecordDecl *BaseClassDecl,
1295  const CXXRecordDecl *MostDerivedClassDecl)
1296 {
1297  // If the destructor is trivial we don't have to check anything else.
1298  if (BaseClassDecl->hasTrivialDestructor())
1299  return true;
1300 
1301  if (!BaseClassDecl->getDestructor()->hasTrivialBody())
1302  return false;
1303 
1304  // Check fields.
1305  for (const auto *Field : BaseClassDecl->fields())
1306  if (!FieldHasTrivialDestructorBody(Context, Field))
1307  return false;
1308 
1309  // Check non-virtual bases.
1310  for (const auto &I : BaseClassDecl->bases()) {
1311  if (I.isVirtual())
1312  continue;
1313 
1314  const CXXRecordDecl *NonVirtualBase =
1315  cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1316  if (!HasTrivialDestructorBody(Context, NonVirtualBase,
1317  MostDerivedClassDecl))
1318  return false;
1319  }
1320 
1321  if (BaseClassDecl == MostDerivedClassDecl) {
1322  // Check virtual bases.
1323  for (const auto &I : BaseClassDecl->vbases()) {
1324  const CXXRecordDecl *VirtualBase =
1325  cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1326  if (!HasTrivialDestructorBody(Context, VirtualBase,
1327  MostDerivedClassDecl))
1328  return false;
1329  }
1330  }
1331 
1332  return true;
1333 }
1334 
1335 static bool
1337  const FieldDecl *Field)
1338 {
1339  QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
1340 
1341  const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
1342  if (!RT)
1343  return true;
1344 
1345  CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl());
1346 
1347  // The destructor for an implicit anonymous union member is never invoked.
1348  if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
1349  return false;
1350 
1351  return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
1352 }
1353 
1354 /// CanSkipVTablePointerInitialization - Check whether we need to initialize
1355 /// any vtable pointers before calling this destructor.
1357  const CXXDestructorDecl *Dtor) {
1358  if (!Dtor->hasTrivialBody())
1359  return false;
1360 
1361  // Check the fields.
1362  const CXXRecordDecl *ClassDecl = Dtor->getParent();
1363  for (const auto *Field : ClassDecl->fields())
1364  if (!FieldHasTrivialDestructorBody(Context, Field))
1365  return false;
1366 
1367  return true;
1368 }
1369 
1370 // Generates function call for handling object poisoning, passing in
1371 // references to 'this' and its size as arguments.
1373  const CXXDestructorDecl *Dtor) {
1374  const ASTRecordLayout &Layout =
1375  CGF.getContext().getASTRecordLayout(Dtor->getParent());
1376 
1377  llvm::Value *Args[] = {
1378  CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.VoidPtrTy),
1379  llvm::ConstantInt::get(CGF.SizeTy, Layout.getSize().getQuantity())};
1380  llvm::Type *ArgTypes[] = {CGF.VoidPtrTy, CGF.SizeTy};
1381 
1382  llvm::FunctionType *FnType =
1383  llvm::FunctionType::get(CGF.VoidTy, ArgTypes, false);
1384  llvm::Value *Fn =
1385  CGF.CGM.CreateRuntimeFunction(FnType, "__sanitizer_dtor_callback");
1386  CGF.EmitNounwindRuntimeCall(Fn, Args);
1387 }
1388 
1389 /// EmitDestructorBody - Emits the body of the current destructor.
1391  const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl());
1392  CXXDtorType DtorType = CurGD.getDtorType();
1393 
1394  Stmt *Body = Dtor->getBody();
1395  if (Body)
1397 
1398  // The call to operator delete in a deleting destructor happens
1399  // outside of the function-try-block, which means it's always
1400  // possible to delegate the destructor body to the complete
1401  // destructor. Do so.
1402  if (DtorType == Dtor_Deleting) {
1404  EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false,
1405  /*Delegating=*/false, LoadCXXThis());
1406  PopCleanupBlock();
1407  return;
1408  }
1409 
1410  // If the body is a function-try-block, enter the try before
1411  // anything else.
1412  bool isTryBody = (Body && isa<CXXTryStmt>(Body));
1413  if (isTryBody)
1414  EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1416 
1417  // Enter the epilogue cleanups.
1418  RunCleanupsScope DtorEpilogue(*this);
1419 
1420  // If this is the complete variant, just invoke the base variant;
1421  // the epilogue will destruct the virtual bases. But we can't do
1422  // this optimization if the body is a function-try-block, because
1423  // we'd introduce *two* handler blocks. In the Microsoft ABI, we
1424  // always delegate because we might not have a definition in this TU.
1425  switch (DtorType) {
1426  case Dtor_Comdat:
1427  llvm_unreachable("not expecting a COMDAT");
1428 
1429  case Dtor_Deleting: llvm_unreachable("already handled deleting case");
1430 
1431  case Dtor_Complete:
1432  assert((Body || getTarget().getCXXABI().isMicrosoft()) &&
1433  "can't emit a dtor without a body for non-Microsoft ABIs");
1434 
1435  // Enter the cleanup scopes for virtual bases.
1437 
1438  if (!isTryBody) {
1439  EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false,
1440  /*Delegating=*/false, LoadCXXThis());
1441  break;
1442  }
1443  // Fallthrough: act like we're in the base variant.
1444 
1445  case Dtor_Base:
1446  assert(Body);
1447 
1448  // Enter the cleanup scopes for fields and non-virtual bases.
1450 
1451  // Initialize the vtable pointers before entering the body.
1454 
1455  if (isTryBody)
1456  EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock());
1457  else if (Body)
1458  EmitStmt(Body);
1459  else {
1460  assert(Dtor->isImplicit() && "bodyless dtor not implicit");
1461  // nothing to do besides what's in the epilogue
1462  }
1463  // -fapple-kext must inline any call to this dtor into
1464  // the caller's body.
1465  if (getLangOpts().AppleKext)
1466  CurFn->addFnAttr(llvm::Attribute::AlwaysInline);
1467  break;
1468  }
1469 
1470  // Jump out through the epilogue cleanups.
1471  DtorEpilogue.ForceCleanup();
1472 
1473  // Exit the try if applicable.
1474  if (isTryBody)
1475  ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true);
1476 
1477  // Insert memory-poisoning instrumentation.
1478  if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor)
1479  EmitDtorSanitizerCallback(*this, Dtor);
1480 }
1481 
1483  const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl());
1484  const Stmt *RootS = AssignOp->getBody();
1485  assert(isa<CompoundStmt>(RootS) &&
1486  "Body of an implicit assignment operator should be compound stmt.");
1487  const CompoundStmt *RootCS = cast<CompoundStmt>(RootS);
1488 
1489  LexicalScope Scope(*this, RootCS->getSourceRange());
1490 
1491  AssignmentMemcpyizer AM(*this, AssignOp, Args);
1492  for (auto *I : RootCS->body())
1493  AM.emitAssignment(I);
1494  AM.finish();
1495 }
1496 
1497 namespace {
1498  /// Call the operator delete associated with the current destructor.
1499  struct CallDtorDelete : EHScopeStack::Cleanup {
1500  CallDtorDelete() {}
1501 
1502  void Emit(CodeGenFunction &CGF, Flags flags) override {
1503  const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1504  const CXXRecordDecl *ClassDecl = Dtor->getParent();
1505  CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1506  CGF.getContext().getTagDeclType(ClassDecl));
1507  }
1508  };
1509 
1510  struct CallDtorDeleteConditional : EHScopeStack::Cleanup {
1511  llvm::Value *ShouldDeleteCondition;
1512  public:
1513  CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition)
1514  : ShouldDeleteCondition(ShouldDeleteCondition) {
1515  assert(ShouldDeleteCondition != nullptr);
1516  }
1517 
1518  void Emit(CodeGenFunction &CGF, Flags flags) override {
1519  llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete");
1520  llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue");
1521  llvm::Value *ShouldCallDelete
1522  = CGF.Builder.CreateIsNull(ShouldDeleteCondition);
1523  CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB);
1524 
1525  CGF.EmitBlock(callDeleteBB);
1526  const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl);
1527  const CXXRecordDecl *ClassDecl = Dtor->getParent();
1528  CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(),
1529  CGF.getContext().getTagDeclType(ClassDecl));
1530  CGF.Builder.CreateBr(continueBB);
1531 
1532  CGF.EmitBlock(continueBB);
1533  }
1534  };
1535 
1536  class DestroyField : public EHScopeStack::Cleanup {
1537  const FieldDecl *field;
1538  CodeGenFunction::Destroyer *destroyer;
1539  bool useEHCleanupForArray;
1540 
1541  public:
1542  DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
1543  bool useEHCleanupForArray)
1544  : field(field), destroyer(destroyer),
1545  useEHCleanupForArray(useEHCleanupForArray) {}
1546 
1547  void Emit(CodeGenFunction &CGF, Flags flags) override {
1548  // Find the address of the field.
1549  llvm::Value *thisValue = CGF.LoadCXXThis();
1550  QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent());
1551  LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy);
1552  LValue LV = CGF.EmitLValueForField(ThisLV, field);
1553  assert(LV.isSimple());
1554 
1555  CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer,
1556  flags.isForNormalCleanup() && useEHCleanupForArray);
1557  }
1558  };
1559 }
1560 
1561 /// \brief Emit all code that comes at the end of class's
1562 /// destructor. This is to call destructors on members and base classes
1563 /// in reverse order of their construction.
1565  CXXDtorType DtorType) {
1566  assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) &&
1567  "Should not emit dtor epilogue for non-exported trivial dtor!");
1568 
1569  // The deleting-destructor phase just needs to call the appropriate
1570  // operator delete that Sema picked up.
1571  if (DtorType == Dtor_Deleting) {
1572  assert(DD->getOperatorDelete() &&
1573  "operator delete missing - EnterDtorCleanups");
1574  if (CXXStructorImplicitParamValue) {
1575  // If there is an implicit param to the deleting dtor, it's a boolean
1576  // telling whether we should call delete at the end of the dtor.
1577  EHStack.pushCleanup<CallDtorDeleteConditional>(
1578  NormalAndEHCleanup, CXXStructorImplicitParamValue);
1579  } else {
1580  EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup);
1581  }
1582  return;
1583  }
1584 
1585  const CXXRecordDecl *ClassDecl = DD->getParent();
1586 
1587  // Unions have no bases and do not call field destructors.
1588  if (ClassDecl->isUnion())
1589  return;
1590 
1591  // The complete-destructor phase just destructs all the virtual bases.
1592  if (DtorType == Dtor_Complete) {
1593 
1594  // We push them in the forward order so that they'll be popped in
1595  // the reverse order.
1596  for (const auto &Base : ClassDecl->vbases()) {
1597  CXXRecordDecl *BaseClassDecl
1598  = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl());
1599 
1600  // Ignore trivial destructors.
1601  if (BaseClassDecl->hasTrivialDestructor())
1602  continue;
1603 
1604  EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1605  BaseClassDecl,
1606  /*BaseIsVirtual*/ true);
1607  }
1608 
1609  return;
1610  }
1611 
1612  assert(DtorType == Dtor_Base);
1613 
1614  // Destroy non-virtual bases.
1615  for (const auto &Base : ClassDecl->bases()) {
1616  // Ignore virtual bases.
1617  if (Base.isVirtual())
1618  continue;
1619 
1620  CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
1621 
1622  // Ignore trivial destructors.
1623  if (BaseClassDecl->hasTrivialDestructor())
1624  continue;
1625 
1626  EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup,
1627  BaseClassDecl,
1628  /*BaseIsVirtual*/ false);
1629  }
1630 
1631  // Destroy direct fields.
1632  for (const auto *Field : ClassDecl->fields()) {
1633  QualType type = Field->getType();
1634  QualType::DestructionKind dtorKind = type.isDestructedType();
1635  if (!dtorKind) continue;
1636 
1637  // Anonymous union members do not have their destructors called.
1638  const RecordType *RT = type->getAsUnionType();
1639  if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue;
1640 
1641  CleanupKind cleanupKind = getCleanupKind(dtorKind);
1642  EHStack.pushCleanup<DestroyField>(cleanupKind, Field,
1643  getDestroyer(dtorKind),
1644  cleanupKind & EHCleanup);
1645  }
1646 }
1647 
1648 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1649 /// constructor for each of several members of an array.
1650 ///
1651 /// \param ctor the constructor to call for each element
1652 /// \param arrayType the type of the array to initialize
1653 /// \param arrayBegin an arrayType*
1654 /// \param zeroInitialize true if each element should be
1655 /// zero-initialized before it is constructed
1657  const CXXConstructorDecl *ctor, const ConstantArrayType *arrayType,
1658  llvm::Value *arrayBegin, const CXXConstructExpr *E, bool zeroInitialize) {
1659  QualType elementType;
1660  llvm::Value *numElements =
1661  emitArrayLength(arrayType, elementType, arrayBegin);
1662 
1663  EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, zeroInitialize);
1664 }
1665 
1666 /// EmitCXXAggrConstructorCall - Emit a loop to call a particular
1667 /// constructor for each of several members of an array.
1668 ///
1669 /// \param ctor the constructor to call for each element
1670 /// \param numElements the number of elements in the array;
1671 /// may be zero
1672 /// \param arrayBegin a T*, where T is the type constructed by ctor
1673 /// \param zeroInitialize true if each element should be
1674 /// zero-initialized before it is constructed
1676  llvm::Value *numElements,
1677  llvm::Value *arrayBegin,
1678  const CXXConstructExpr *E,
1679  bool zeroInitialize) {
1680 
1681  // It's legal for numElements to be zero. This can happen both
1682  // dynamically, because x can be zero in 'new A[x]', and statically,
1683  // because of GCC extensions that permit zero-length arrays. There
1684  // are probably legitimate places where we could assume that this
1685  // doesn't happen, but it's not clear that it's worth it.
1686  llvm::BranchInst *zeroCheckBranch = nullptr;
1687 
1688  // Optimize for a constant count.
1689  llvm::ConstantInt *constantCount
1690  = dyn_cast<llvm::ConstantInt>(numElements);
1691  if (constantCount) {
1692  // Just skip out if the constant count is zero.
1693  if (constantCount->isZero()) return;
1694 
1695  // Otherwise, emit the check.
1696  } else {
1697  llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop");
1698  llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty");
1699  zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB);
1700  EmitBlock(loopBB);
1701  }
1702 
1703  // Find the end of the array.
1704  llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements,
1705  "arrayctor.end");
1706 
1707  // Enter the loop, setting up a phi for the current location to initialize.
1708  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1709  llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop");
1710  EmitBlock(loopBB);
1711  llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2,
1712  "arrayctor.cur");
1713  cur->addIncoming(arrayBegin, entryBB);
1714 
1715  // Inside the loop body, emit the constructor call on the array element.
1716 
1718 
1719  // Zero initialize the storage, if requested.
1720  if (zeroInitialize)
1721  EmitNullInitialization(cur, type);
1722 
1723  // C++ [class.temporary]p4:
1724  // There are two contexts in which temporaries are destroyed at a different
1725  // point than the end of the full-expression. The first context is when a
1726  // default constructor is called to initialize an element of an array.
1727  // If the constructor has one or more default arguments, the destruction of
1728  // every temporary created in a default argument expression is sequenced
1729  // before the construction of the next array element, if any.
1730 
1731  {
1732  RunCleanupsScope Scope(*this);
1733 
1734  // Evaluate the constructor and its arguments in a regular
1735  // partial-destroy cleanup.
1736  if (getLangOpts().Exceptions &&
1737  !ctor->getParent()->hasTrivialDestructor()) {
1738  Destroyer *destroyer = destroyCXXObject;
1739  pushRegularPartialArrayCleanup(arrayBegin, cur, type, *destroyer);
1740  }
1741 
1742  EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false,
1743  /*Delegating=*/false, cur, E);
1744  }
1745 
1746  // Go to the next element.
1747  llvm::Value *next =
1748  Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1),
1749  "arrayctor.next");
1750  cur->addIncoming(next, Builder.GetInsertBlock());
1751 
1752  // Check whether that's the end of the loop.
1753  llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done");
1754  llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont");
1755  Builder.CreateCondBr(done, contBB, loopBB);
1756 
1757  // Patch the earlier check to skip over the loop.
1758  if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB);
1759 
1760  EmitBlock(contBB);
1761 }
1762 
1764  llvm::Value *addr,
1765  QualType type) {
1766  const RecordType *rtype = type->castAs<RecordType>();
1767  const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl());
1768  const CXXDestructorDecl *dtor = record->getDestructor();
1769  assert(!dtor->isTrivial());
1770  CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false,
1771  /*Delegating=*/false, addr);
1772 }
1773 
1775  CXXCtorType Type,
1776  bool ForVirtualBase,
1777  bool Delegating, llvm::Value *This,
1778  const CXXConstructExpr *E) {
1779  // C++11 [class.mfct.non-static]p2:
1780  // If a non-static member function of a class X is called for an object that
1781  // is not of type X, or of a type derived from X, the behavior is undefined.
1782  // FIXME: Provide a source location here.
1785 
1786  if (D->isTrivial() && D->isDefaultConstructor()) {
1787  assert(E->getNumArgs() == 0 && "trivial default ctor with args");
1788  return;
1789  }
1790 
1791  // If this is a trivial constructor, just emit what's needed. If this is a
1792  // union copy constructor, we must emit a memcpy, because the AST does not
1793  // model that copy.
1795  assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
1796 
1797  const Expr *Arg = E->getArg(0);
1798  QualType SrcTy = Arg->getType();
1799  llvm::Value *Src = EmitLValue(Arg).getAddress();
1800  QualType DestTy = getContext().getTypeDeclType(D->getParent());
1801  EmitAggregateCopyCtor(This, Src, DestTy, SrcTy);
1802  return;
1803  }
1804 
1805  CallArgList Args;
1806 
1807  // Push the this ptr.
1808  Args.add(RValue::get(This), D->getThisType(getContext()));
1809 
1810  // Add the rest of the user-supplied arguments.
1811  const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
1812  EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end(), E->getConstructor());
1813 
1814  // Insert any ABI-specific implicit constructor arguments.
1815  unsigned ExtraArgs = CGM.getCXXABI().addImplicitConstructorArgs(
1816  *this, D, Type, ForVirtualBase, Delegating, Args);
1817 
1818  // Emit the call.
1820  const CGFunctionInfo &Info =
1821  CGM.getTypes().arrangeCXXConstructorCall(Args, D, Type, ExtraArgs);
1822  EmitCall(Info, Callee, ReturnValueSlot(), Args, D);
1823 }
1824 
1825 void
1827  llvm::Value *This, llvm::Value *Src,
1828  const CXXConstructExpr *E) {
1830  assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
1831  assert(D->isCopyOrMoveConstructor() &&
1832  "trivial 1-arg ctor not a copy/move ctor");
1833  EmitAggregateCopyCtor(This, Src,
1834  getContext().getTypeDeclType(D->getParent()),
1835  E->arg_begin()->getType());
1836  return;
1837  }
1839  assert(D->isInstance() &&
1840  "Trying to emit a member call expr on a static method!");
1841 
1842  const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
1843 
1844  CallArgList Args;
1845 
1846  // Push the this ptr.
1847  Args.add(RValue::get(This), D->getThisType(getContext()));
1848 
1849  // Push the src ptr.
1850  QualType QT = *(FPT->param_type_begin());
1851  llvm::Type *t = CGM.getTypes().ConvertType(QT);
1852  Src = Builder.CreateBitCast(Src, t);
1853  Args.add(RValue::get(Src), QT);
1854 
1855  // Skip over first argument (Src).
1856  EmitCallArgs(Args, FPT, E->arg_begin() + 1, E->arg_end(), E->getConstructor(),
1857  /*ParamsToSkip*/ 1);
1858 
1860  Callee, ReturnValueSlot(), Args, D);
1861 }
1862 
1863 void
1865  CXXCtorType CtorType,
1866  const FunctionArgList &Args,
1867  SourceLocation Loc) {
1868  CallArgList DelegateArgs;
1869 
1870  FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
1871  assert(I != E && "no parameters to constructor");
1872 
1873  // this
1874  DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType());
1875  ++I;
1876 
1877  // vtt
1878  if (llvm::Value *VTT = GetVTTParameter(GlobalDecl(Ctor, CtorType),
1879  /*ForVirtualBase=*/false,
1880  /*Delegating=*/true)) {
1882  DelegateArgs.add(RValue::get(VTT), VoidPP);
1883 
1885  assert(I != E && "cannot skip vtt parameter, already done with args");
1886  assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type");
1887  ++I;
1888  }
1889  }
1890 
1891  // Explicit arguments.
1892  for (; I != E; ++I) {
1893  const VarDecl *param = *I;
1894  // FIXME: per-argument source location
1895  EmitDelegateCallArg(DelegateArgs, param, Loc);
1896  }
1897 
1898  llvm::Value *Callee =
1899  CGM.getAddrOfCXXStructor(Ctor, getFromCtorType(CtorType));
1902  Callee, ReturnValueSlot(), DelegateArgs, Ctor);
1903 }
1904 
1905 namespace {
1906  struct CallDelegatingCtorDtor : EHScopeStack::Cleanup {
1907  const CXXDestructorDecl *Dtor;
1908  llvm::Value *Addr;
1909  CXXDtorType Type;
1910 
1911  CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr,
1912  CXXDtorType Type)
1913  : Dtor(D), Addr(Addr), Type(Type) {}
1914 
1915  void Emit(CodeGenFunction &CGF, Flags flags) override {
1916  CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false,
1917  /*Delegating=*/true, Addr);
1918  }
1919  };
1920 }
1921 
1922 void
1924  const FunctionArgList &Args) {
1925  assert(Ctor->isDelegatingConstructor());
1926 
1927  llvm::Value *ThisPtr = LoadCXXThis();
1928 
1929  QualType Ty = getContext().getTagDeclType(Ctor->getParent());
1930  CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
1931  AggValueSlot AggSlot =
1932  AggValueSlot::forAddr(ThisPtr, Alignment, Qualifiers(),
1936 
1937  EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot);
1938 
1939  const CXXRecordDecl *ClassDecl = Ctor->getParent();
1940  if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
1941  CXXDtorType Type =
1943 
1944  EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup,
1945  ClassDecl->getDestructor(),
1946  ThisPtr, Type);
1947  }
1948 }
1949 
1951  CXXDtorType Type,
1952  bool ForVirtualBase,
1953  bool Delegating,
1954  llvm::Value *This) {
1955  CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase,
1956  Delegating, This);
1957 }
1958 
1959 namespace {
1960  struct CallLocalDtor : EHScopeStack::Cleanup {
1961  const CXXDestructorDecl *Dtor;
1962  llvm::Value *Addr;
1963 
1964  CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr)
1965  : Dtor(D), Addr(Addr) {}
1966 
1967  void Emit(CodeGenFunction &CGF, Flags flags) override {
1969  /*ForVirtualBase=*/false,
1970  /*Delegating=*/false, Addr);
1971  }
1972  };
1973 }
1974 
1976  llvm::Value *Addr) {
1977  EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr);
1978 }
1979 
1981  CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
1982  if (!ClassDecl) return;
1983  if (ClassDecl->hasTrivialDestructor()) return;
1984 
1985  const CXXDestructorDecl *D = ClassDecl->getDestructor();
1986  assert(D && D->isUsed() && "destructor not marked as used!");
1987  PushDestructorCleanup(D, Addr);
1988 }
1989 
1990 void
1992  const CXXRecordDecl *NearestVBase,
1993  CharUnits OffsetFromNearestVBase,
1994  const CXXRecordDecl *VTableClass) {
1995  const CXXRecordDecl *RD = Base.getBase();
1996 
1997  // Don't initialize the vtable pointer if the class is marked with the
1998  // 'novtable' attribute.
1999  if ((RD == VTableClass || RD == NearestVBase) &&
2000  VTableClass->hasAttr<MSNoVTableAttr>())
2001  return;
2002 
2003  // Compute the address point.
2004  bool NeedsVirtualOffset;
2005  llvm::Value *VTableAddressPoint =
2007  *this, VTableClass, Base, NearestVBase, NeedsVirtualOffset);
2008  if (!VTableAddressPoint)
2009  return;
2010 
2011  // Compute where to store the address point.
2012  llvm::Value *VirtualOffset = nullptr;
2013  CharUnits NonVirtualOffset = CharUnits::Zero();
2014 
2015  if (NeedsVirtualOffset) {
2016  // We need to use the virtual base offset offset because the virtual base
2017  // might have a different offset in the most derived class.
2018  VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(*this,
2019  LoadCXXThis(),
2020  VTableClass,
2021  NearestVBase);
2022  NonVirtualOffset = OffsetFromNearestVBase;
2023  } else {
2024  // We can just use the base offset in the complete class.
2025  NonVirtualOffset = Base.getBaseOffset();
2026  }
2027 
2028  // Apply the offsets.
2029  llvm::Value *VTableField = LoadCXXThis();
2030 
2031  if (!NonVirtualOffset.isZero() || VirtualOffset)
2032  VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField,
2033  NonVirtualOffset,
2034  VirtualOffset);
2035 
2036  // Finally, store the address point. Use the same LLVM types as the field to
2037  // support optimization.
2038  llvm::Type *VTablePtrTy =
2039  llvm::FunctionType::get(CGM.Int32Ty, /*isVarArg=*/true)
2040  ->getPointerTo()
2041  ->getPointerTo();
2042  VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo());
2043  VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy);
2044  llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField);
2046 }
2047 
2048 void
2050  const CXXRecordDecl *NearestVBase,
2051  CharUnits OffsetFromNearestVBase,
2052  bool BaseIsNonVirtualPrimaryBase,
2053  const CXXRecordDecl *VTableClass,
2054  VisitedVirtualBasesSetTy& VBases) {
2055  // If this base is a non-virtual primary base the address point has already
2056  // been set.
2057  if (!BaseIsNonVirtualPrimaryBase) {
2058  // Initialize the vtable pointer for this base.
2059  InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase,
2060  VTableClass);
2061  }
2062 
2063  const CXXRecordDecl *RD = Base.getBase();
2064 
2065  // Traverse bases.
2066  for (const auto &I : RD->bases()) {
2067  CXXRecordDecl *BaseDecl
2068  = cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl());
2069 
2070  // Ignore classes without a vtable.
2071  if (!BaseDecl->isDynamicClass())
2072  continue;
2073 
2074  CharUnits BaseOffset;
2075  CharUnits BaseOffsetFromNearestVBase;
2076  bool BaseDeclIsNonVirtualPrimaryBase;
2077 
2078  if (I.isVirtual()) {
2079  // Check if we've visited this virtual base before.
2080  if (!VBases.insert(BaseDecl).second)
2081  continue;
2082 
2083  const ASTRecordLayout &Layout =
2084  getContext().getASTRecordLayout(VTableClass);
2085 
2086  BaseOffset = Layout.getVBaseClassOffset(BaseDecl);
2087  BaseOffsetFromNearestVBase = CharUnits::Zero();
2088  BaseDeclIsNonVirtualPrimaryBase = false;
2089  } else {
2090  const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD);
2091 
2092  BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl);
2093  BaseOffsetFromNearestVBase =
2094  OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl);
2095  BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
2096  }
2097 
2098  InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset),
2099  I.isVirtual() ? BaseDecl : NearestVBase,
2100  BaseOffsetFromNearestVBase,
2101  BaseDeclIsNonVirtualPrimaryBase,
2102  VTableClass, VBases);
2103  }
2104 }
2105 
2107  // Ignore classes without a vtable.
2108  if (!RD->isDynamicClass())
2109  return;
2110 
2111  // Initialize the vtable pointers for this class and all of its bases.
2112  VisitedVirtualBasesSetTy VBases;
2114  /*NearestVBase=*/nullptr,
2115  /*OffsetFromNearestVBase=*/CharUnits::Zero(),
2116  /*BaseIsNonVirtualPrimaryBase=*/false, RD, VBases);
2117 
2118  if (RD->getNumVBases())
2120 }
2121 
2123  llvm::Type *Ty) {
2124  llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo());
2125  llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable");
2127  return VTable;
2128 }
2129 
2130 // If a class has a single non-virtual base and does not introduce or override
2131 // virtual member functions or fields, it will have the same layout as its base.
2132 // This function returns the least derived such class.
2133 //
2134 // Casting an instance of a base class to such a derived class is technically
2135 // undefined behavior, but it is a relatively common hack for introducing member
2136 // functions on class instances with specific properties (e.g. llvm::Operator)
2137 // that works under most compilers and should not have security implications, so
2138 // we allow it by default. It can be disabled with -fsanitize=cfi-cast-strict.
2139 static const CXXRecordDecl *
2141  if (!RD->field_empty())
2142  return RD;
2143 
2144  if (RD->getNumVBases() != 0)
2145  return RD;
2146 
2147  if (RD->getNumBases() != 1)
2148  return RD;
2149 
2150  for (const CXXMethodDecl *MD : RD->methods()) {
2151  if (MD->isVirtual()) {
2152  // Virtual member functions are only ok if they are implicit destructors
2153  // because the implicit destructor will have the same semantics as the
2154  // base class's destructor if no fields are added.
2155  if (isa<CXXDestructorDecl>(MD) && MD->isImplicit())
2156  continue;
2157  return RD;
2158  }
2159  }
2160 
2162  RD->bases_begin()->getType()->getAsCXXRecordDecl());
2163 }
2164 
2166  llvm::Value *VTable,
2167  CFITypeCheckKind TCK,
2168  SourceLocation Loc) {
2169  const CXXRecordDecl *ClassDecl = MD->getParent();
2170  if (!SanOpts.has(SanitizerKind::CFICastStrict))
2171  ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
2172 
2173  EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc);
2174 }
2175 
2177  llvm::Value *Derived,
2178  bool MayBeNull,
2179  CFITypeCheckKind TCK,
2180  SourceLocation Loc) {
2181  if (!getLangOpts().CPlusPlus)
2182  return;
2183 
2184  auto *ClassTy = T->getAs<RecordType>();
2185  if (!ClassTy)
2186  return;
2187 
2188  const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassTy->getDecl());
2189 
2190  if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass())
2191  return;
2192 
2193  SmallString<64> MangledName;
2194  llvm::raw_svector_ostream Out(MangledName);
2196  Out);
2197 
2198  // Blacklist based on the mangled type.
2200  return;
2201 
2202  if (!SanOpts.has(SanitizerKind::CFICastStrict))
2203  ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl);
2204 
2205  llvm::BasicBlock *ContBlock = 0;
2206 
2207  if (MayBeNull) {
2208  llvm::Value *DerivedNotNull =
2209  Builder.CreateIsNotNull(Derived, "cast.nonnull");
2210 
2211  llvm::BasicBlock *CheckBlock = createBasicBlock("cast.check");
2212  ContBlock = createBasicBlock("cast.cont");
2213 
2214  Builder.CreateCondBr(DerivedNotNull, CheckBlock, ContBlock);
2215 
2216  EmitBlock(CheckBlock);
2217  }
2218 
2219  llvm::Value *VTable = GetVTablePtr(Derived, Int8PtrTy);
2220  EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc);
2221 
2222  if (MayBeNull) {
2223  Builder.CreateBr(ContBlock);
2224  EmitBlock(ContBlock);
2225  }
2226 }
2227 
2229  llvm::Value *VTable,
2230  CFITypeCheckKind TCK,
2231  SourceLocation Loc) {
2232  if (CGM.IsCFIBlacklistedRecord(RD))
2233  return;
2234 
2235  SanitizerScope SanScope(this);
2236 
2237  std::string OutName;
2238  llvm::raw_string_ostream Out(OutName);
2240 
2241  llvm::Value *BitSetName = llvm::MetadataAsValue::get(
2242  getLLVMContext(), llvm::MDString::get(getLLVMContext(), Out.str()));
2243 
2244  llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy);
2245  llvm::Value *BitSetTest =
2246  Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::bitset_test),
2247  {CastedVTable, BitSetName});
2248 
2249  SanitizerMask M;
2250  switch (TCK) {
2251  case CFITCK_VCall:
2252  M = SanitizerKind::CFIVCall;
2253  break;
2254  case CFITCK_NVCall:
2255  M = SanitizerKind::CFINVCall;
2256  break;
2257  case CFITCK_DerivedCast:
2258  M = SanitizerKind::CFIDerivedCast;
2259  break;
2260  case CFITCK_UnrelatedCast:
2261  M = SanitizerKind::CFIUnrelatedCast;
2262  break;
2263  }
2264 
2265  llvm::Constant *StaticData[] = {
2268  llvm::ConstantInt::get(Int8Ty, TCK),
2269  };
2270  EmitCheck(std::make_pair(BitSetTest, M), "cfi_bad_type", StaticData,
2271  CastedVTable);
2272 }
2273 
2274 // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do
2275 // quite what we want.
2276 static const Expr *skipNoOpCastsAndParens(const Expr *E) {
2277  while (true) {
2278  if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) {
2279  E = PE->getSubExpr();
2280  continue;
2281  }
2282 
2283  if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
2284  if (CE->getCastKind() == CK_NoOp) {
2285  E = CE->getSubExpr();
2286  continue;
2287  }
2288  }
2289  if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
2290  if (UO->getOpcode() == UO_Extension) {
2291  E = UO->getSubExpr();
2292  continue;
2293  }
2294  }
2295  return E;
2296  }
2297 }
2298 
2299 bool
2301  const CXXMethodDecl *MD) {
2302  // When building with -fapple-kext, all calls must go through the vtable since
2303  // the kernel linker can do runtime patching of vtables.
2304  if (getLangOpts().AppleKext)
2305  return false;
2306 
2307  // If the most derived class is marked final, we know that no subclass can
2308  // override this member function and so we can devirtualize it. For example:
2309  //
2310  // struct A { virtual void f(); }
2311  // struct B final : A { };
2312  //
2313  // void f(B *b) {
2314  // b->f();
2315  // }
2316  //
2317  const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
2318  if (MostDerivedClassDecl->hasAttr<FinalAttr>())
2319  return true;
2320 
2321  // If the member function is marked 'final', we know that it can't be
2322  // overridden and can therefore devirtualize it.
2323  if (MD->hasAttr<FinalAttr>())
2324  return true;
2325 
2326  // Similarly, if the class itself is marked 'final' it can't be overridden
2327  // and we can therefore devirtualize the member function call.
2328  if (MD->getParent()->hasAttr<FinalAttr>())
2329  return true;
2330 
2331  Base = skipNoOpCastsAndParens(Base);
2332  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) {
2333  if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
2334  // This is a record decl. We know the type and can devirtualize it.
2335  return VD->getType()->isRecordType();
2336  }
2337 
2338  return false;
2339  }
2340 
2341  // We can devirtualize calls on an object accessed by a class member access
2342  // expression, since by C++11 [basic.life]p6 we know that it can't refer to
2343  // a derived class object constructed in the same location.
2344  if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base))
2345  if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl()))
2346  return VD->getType()->isRecordType();
2347 
2348  // We can always devirtualize calls on temporary object expressions.
2349  if (isa<CXXConstructExpr>(Base))
2350  return true;
2351 
2352  // And calls on bound temporaries.
2353  if (isa<CXXBindTemporaryExpr>(Base))
2354  return true;
2355 
2356  // Check if this is a call expr that returns a record type.
2357  if (const CallExpr *CE = dyn_cast<CallExpr>(Base))
2358  return CE->getCallReturnType(getContext())->isRecordType();
2359 
2360  // We can't devirtualize the call.
2361  return false;
2362 }
2363 
2365  const CXXMethodDecl *callOperator,
2366  CallArgList &callArgs) {
2367  // Get the address of the call operator.
2368  const CGFunctionInfo &calleeFnInfo =
2369  CGM.getTypes().arrangeCXXMethodDeclaration(callOperator);
2370  llvm::Value *callee =
2371  CGM.GetAddrOfFunction(GlobalDecl(callOperator),
2372  CGM.getTypes().GetFunctionType(calleeFnInfo));
2373 
2374  // Prepare the return slot.
2375  const FunctionProtoType *FPT =
2376  callOperator->getType()->castAs<FunctionProtoType>();
2377  QualType resultType = FPT->getReturnType();
2378  ReturnValueSlot returnSlot;
2379  if (!resultType->isVoidType() &&
2380  calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect &&
2381  !hasScalarEvaluationKind(calleeFnInfo.getReturnType()))
2382  returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified());
2383 
2384  // We don't need to separately arrange the call arguments because
2385  // the call can't be variadic anyway --- it's impossible to forward
2386  // variadic arguments.
2387 
2388  // Now emit our call.
2389  RValue RV = EmitCall(calleeFnInfo, callee, returnSlot,
2390  callArgs, callOperator);
2391 
2392  // If necessary, copy the returned value into the slot.
2393  if (!resultType->isVoidType() && returnSlot.isNull())
2394  EmitReturnOfRValue(RV, resultType);
2395  else
2397 }
2398 
2400  const BlockDecl *BD = BlockInfo->getBlockDecl();
2401  const VarDecl *variable = BD->capture_begin()->getVariable();
2402  const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
2403 
2404  // Start building arguments for forwarding call
2405  CallArgList CallArgs;
2406 
2407  QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2408  llvm::Value *ThisPtr = GetAddrOfBlockDecl(variable, false);
2409  CallArgs.add(RValue::get(ThisPtr), ThisType);
2410 
2411  // Add the rest of the parameters.
2412  for (auto param : BD->params())
2413  EmitDelegateCallArg(CallArgs, param, param->getLocStart());
2414 
2415  assert(!Lambda->isGenericLambda() &&
2416  "generic lambda interconversion to block not implemented");
2418 }
2419 
2421  if (cast<CXXMethodDecl>(CurCodeDecl)->isVariadic()) {
2422  // FIXME: Making this work correctly is nasty because it requires either
2423  // cloning the body of the call operator or making the call operator forward.
2424  CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function");
2425  return;
2426  }
2427 
2428  EmitFunctionBody(Args, cast<FunctionDecl>(CurGD.getDecl())->getBody());
2429 }
2430 
2432  const CXXRecordDecl *Lambda = MD->getParent();
2433 
2434  // Start building arguments for forwarding call
2435  CallArgList CallArgs;
2436 
2437  QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda));
2438  llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType));
2439  CallArgs.add(RValue::get(ThisPtr), ThisType);
2440 
2441  // Add the rest of the parameters.
2442  for (auto Param : MD->params())
2443  EmitDelegateCallArg(CallArgs, Param, Param->getLocStart());
2444 
2445  const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
2446  // For a generic lambda, find the corresponding call operator specialization
2447  // to which the call to the static-invoker shall be forwarded.
2448  if (Lambda->isGenericLambda()) {
2449  assert(MD->isFunctionTemplateSpecialization());
2451  FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate();
2452  void *InsertPos = nullptr;
2453  FunctionDecl *CorrespondingCallOpSpecialization =
2454  CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos);
2455  assert(CorrespondingCallOpSpecialization);
2456  CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization);
2457  }
2458  EmitForwardingCallToLambda(CallOp, CallArgs);
2459 }
2460 
2462  if (MD->isVariadic()) {
2463  // FIXME: Making this work correctly is nasty because it requires either
2464  // cloning the body of the call operator or making the call operator forward.
2465  CGM.ErrorUnsupported(MD, "lambda conversion to variadic function");
2466  return;
2467  }
2468 
2470 }
void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init, ArrayRef< VarDecl * > ArrayIndexes)
Definition: CGClass.cpp:629
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:2411
void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type)
Emit all code that comes at the end of class's destructor. This is to call destructors on members and...
Definition: CGClass.cpp:1564
unsigned getNumArrayIndices() const
Determine the number of implicit array indices used while described an array member initialization...
Definition: DeclCXX.h:2109
llvm::Value * GetAddrOfBlockDecl(const VarDecl *var, bool ByRef)
Definition: CGBlocks.cpp:1001
void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, CXXCtorType CtorType, const FunctionArgList &Args, SourceLocation Loc)
Definition: CGClass.cpp:1864
Complete object ctor.
Definition: ABI.h:26
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:1356
bool isVirtual() const
Determines whether the base class is a virtual base class (or not).
Definition: DeclCXX.h:206
void EmitVTablePtrCheckForCall(const CXXMethodDecl *MD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc)
Definition: CGClass.cpp:2165
void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type, FunctionArgList &Args)
Definition: CGClass.cpp:1236
base_class_range bases()
Definition: DeclCXX.h:713
unsigned getFieldCount() const
getFieldCount - Get the number of fields in the layout.
Definition: RecordLayout.h:177
CanQualType getReturnType() const
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:2330
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1263
DestructionKind isDestructedType() const
Definition: Type.h:999
QualType getType() const
Retrieves the type of the base class.
Definition: DeclCXX.h:252
CXXCtorType getCtorType() const
Definition: GlobalDecl.h:62
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler...
Definition: CGExpr.cpp:2135
void InitializeVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase, CharUnits OffsetFromNearestVBase, bool BaseIsNonVirtualPrimaryBase, const CXXRecordDecl *VTableClass, VisitedVirtualBasesSetTy &VBases)
Definition: CGClass.cpp:2049
FunctionDecl * findSpecialization(ArrayRef< TemplateArgument > Args, void *&InsertPos)
Return the specialization with the provided arguments if it exists, otherwise return the insertion po...
method_range methods() const
Definition: DeclCXX.h:755
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, IsZeroed_t isZeroed=IsNotZeroed)
Definition: CGValue.h:441
void setAlignment(CharUnits A)
Definition: CGValue.h:262
const TargetInfo & getTarget() const
CanQualType getSizeType() const
Return the unique type for "size_t" (C99 7.17), defined in <stddef.h>.
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
Definition: CGExpr.cpp:57
Checking the 'this' pointer for a constructor call.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:163
static void EmitAggMemberInitializer(CodeGenFunction &CGF, LValue LHS, Expr *Init, llvm::Value *ArrayIndexVar, QualType T, ArrayRef< VarDecl * > ArrayIndexes, unsigned Index)
Definition: CGClass.cpp:438
chain_range chain() const
Definition: Decl.h:2510
arg_iterator arg_begin()
Definition: ExprCXX.h:1189
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
Defines the C++ template declaration subclasses.
void EmitAutoVarDecl(const VarDecl &D)
Definition: CGDecl.cpp:847
const llvm::DataLayout & getDataLayout() const
const void * Store
Definition: StoreRef.h:26
static bool isMemcpyEquivalentSpecialMember(const CXXMethodDecl *D)
Definition: CGClass.cpp:540
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
Definition: CGExpr.cpp:1469
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
QualType getRecordType(const RecordDecl *Decl) const
bool sanitizePerformTypeCheck() const
Whether any type-checking sanitizers are enabled. If false, calls to EmitTypeCheck can be skipped...
Definition: CGExpr.cpp:482
CharUnits getVBaseClassOffset(const CXXRecordDecl *VBase) const
getVBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:232
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1075
param_range params()
Definition: Decl.h:3523
const LangOptions & getLangOpts() const
LValue EmitLValueForFieldInitialization(LValue Base, const FieldDecl *Field)
Definition: CGExpr.cpp:2794
SourceLocation getLocEnd() const LLVM_READONLY
Definition: DeclBase.h:368
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, llvm::Value *This)
Definition: CGClass.cpp:1950
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2147
llvm::Value * getAddress() const
Definition: CGValue.h:265
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition: DeclCXX.cpp:1526
const llvm::APInt & getSize() const
Definition: Type.h:2472
virtual llvm::BasicBlock * EmitCtorCompleteObjectHandler(CodeGenFunction &CGF, const CXXRecordDecl *RD)
Definition: CGCXXABI.cpp:292
const CXXBaseSpecifier *const * path_const_iterator
Definition: Expr.h:2726
Expr * getInit() const
Get the initializer.
Definition: DeclCXX.h:2134
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:1701
llvm::Value * GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, bool Delegating)
Definition: CGClass.cpp:297
QualType getThisType(ASTContext &C) const
Returns the type of the this pointer.
Definition: DeclCXX.cpp:1592
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
void DecorateInstruction(llvm::Instruction *Inst, llvm::MDNode *TBAAInfo, bool ConvertTypeToTag=true)
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition: DeclCXX.cpp:1797
const CGFunctionInfo & arrangeCXXStructorDeclaration(const CXXMethodDecl *MD, StructorType Type)
Definition: CGCall.cpp:193
capture_iterator capture_begin()
Definition: Decl.h:3570
void setAddress(llvm::Value *address)
Definition: CGValue.h:266
void EmitAggregateCopyCtor(llvm::Value *DestPtr, llvm::Value *SrcPtr, QualType DestTy, QualType SrcTy)
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition: DeclCXX.cpp:1547
void emitImplicitAssignmentOperatorBody(FunctionArgList &Args)
Definition: CGClass.cpp:1482
bool isBaseInitializer() const
Determine whether this initializer is initializing a base class.
Definition: DeclCXX.h:1980
static const CXXRecordDecl * LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD)
Definition: CGClass.cpp:2140
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition: Decl.h:2120
void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo, CallExpr::const_arg_iterator ArgBeg, CallExpr::const_arg_iterator ArgEnd, const FunctionDecl *CalleeDecl=nullptr, unsigned ParamsToSkip=0)
EmitCallArgs - Emit call arguments for a function.
bool isVolatileQualified() const
Definition: CGValue.h:199
bool hasAttr() const
Definition: DeclBase.h:487
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition: Decl.cpp:3073
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:89
void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition: CGDecl.cpp:1211
bool isDelegatingConstructor() const
Determine whether this constructor is a delegating constructor.
Definition: DeclCXX.h:2255
virtual void mangleCXXVTableBitSet(const CXXRecordDecl *RD, raw_ostream &)=0
llvm::SmallPtrSet< const CXXRecordDecl *, 4 > VisitedVirtualBasesSetTy
bool isReferenceType() const
Definition: Type.h:5241
bool isCompleteDefinition() const
Definition: Decl.h:2838
static bool CanSkipVTablePointerInitialization(ASTContext &Context, const CXXDestructorDecl *Dtor)
Definition: CGClass.cpp:1356
virtual llvm::Value * getVTableAddressPointInStructor(CodeGenFunction &CGF, const CXXRecordDecl *RD, BaseSubobject Base, const CXXRecordDecl *NearestVBase, bool &NeedsVirtualOffset)=0
uint64_t getSubVTTIndex(const CXXRecordDecl *RD, BaseSubobject Base)
Definition: CGVTT.cpp:129
void EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc)
Definition: CGClass.cpp:2228
ArrayRef< VarDecl * > getArrayIndexes()
Definition: DeclCXX.h:2127
const RecordType * getAsUnionType() const
NOTE: getAs*ArrayType are methods on ASTContext.
Definition: Type.cpp:449
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
CleanupKind getCleanupKind(QualType::DestructionKind kind)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
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:1114
const Decl * getDecl() const
Definition: GlobalDecl.h:60
void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD)
Definition: CGClass.cpp:2431
LValue MakeAddrLValue(llvm::Value *V, QualType T, CharUnits Alignment=CharUnits())
static bool hasScalarEvaluationKind(QualType T)
void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty)
RValue EmitCall(const CGFunctionInfo &FnInfo, llvm::Value *Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, const Decl *TargetDecl=nullptr, llvm::Instruction **callOrInvoke=nullptr)
Definition: CGCall.cpp:3106
CharUnits getAlignment() const
Definition: CGValue.h:261
llvm::Value * GetVTablePtr(llvm::Value *This, llvm::Type *Ty)
Definition: CGClass.cpp:2122
Base object ctor.
Definition: ABI.h:27
const LangOptions & getLangOpts() const
Definition: ASTContext.h:533
bool isImplicit() const
Definition: DeclBase.h:503
IndirectFieldDecl * getIndirectMember() const
Definition: DeclCXX.h:2060
uint32_t Offset
Definition: CacheTokens.cpp:43
void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, llvm::Value *arrayEnd, QualType elementType, Destroyer *destroyer)
Definition: CGDecl.cpp:1613
QualType getReturnType() const
Definition: Type.h:2952
const CXXRecordDecl * getParent() const
Definition: DeclCXX.h:1817
bool isDefaulted() const
Definition: Decl.h:1805
CharUnits StorageOffset
The offset of the bitfield storage from the start of the struct.
field_range fields() const
Definition: Decl.h:3349
Deleting dtor.
Definition: ABI.h:35
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2918
bool needsEHCleanup(QualType::DestructionKind kind)
RecordDecl * getDecl() const
Definition: Type.h:3527
llvm::Value * GetAddressOfBaseClass(llvm::Value *Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue, SourceLocation Loc)
Definition: CGClass.cpp:136
const SanitizerBlacklist & getSanitizerBlacklist() const
Definition: ASTContext.h:535
bool isVariadic() const
Whether this function is variadic.
Definition: Decl.cpp:2362
void incrementProfileCounter(const Stmt *S)
Increment the profiler's counter for the given statement.
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
const Type * getBaseClass() const
Definition: DeclCXX.cpp:1709
static void EmitDtorSanitizerCallback(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor)
Definition: CGClass.cpp:1372
void EmitStmt(const Stmt *S)
Definition: CGStmt.cpp:45
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
uint64_t getFieldOffset(unsigned FieldNo) const
Definition: RecordLayout.h:181
void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, QualType DeleteTy)
Definition: CGExprCXX.cpp:1393
base_class_iterator bases_begin()
Definition: DeclCXX.h:720
const CGFunctionInfo & arrangeCXXMethodDeclaration(const CXXMethodDecl *MD)
Definition: CGCall.cpp:177
GlobalDecl CurGD
CurGD - The GlobalDecl for the current function being compiled.
static const Expr * skipNoOpCastsAndParens(const Expr *E)
Definition: CGClass.cpp:2276
static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init)
Definition: CGClass.cpp:386
std::pair< CharUnits, CharUnits > getTypeInfoInChars(const Type *T) const
init_iterator init_begin()
Retrieve an iterator to the first initializer.
Definition: DeclCXX.h:2206
virtual void mangleCXXRTTI(QualType T, raw_ostream &)=0
QualType getType() const
Definition: Decl.h:538
Represents the this expression in C++.
Definition: ExprCXX.h:770
static llvm::Value * ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ptr, CharUnits nonVirtualOffset, llvm::Value *virtualOffset)
Definition: CGClass.cpp:112
LValue EmitLValueForField(LValue Base, const FieldDecl *Field)
Definition: CGExpr.cpp:2680
llvm::GlobalValue * getAddrOfCXXStructor(const CXXMethodDecl *MD, StructorType Type, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false)
Return the address of the constructor/destructor of the given type.
Definition: CGCXX.cpp:229
bool isUnion() const
Definition: Decl.h:2906
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerMask >> Checked, StringRef CheckName, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs)
Create a basic block that will call a handler function in a sanitizer runtime with the provided argum...
Definition: CGExpr.cpp:2295
llvm::CallInst * EmitNounwindRuntimeCall(llvm::Value *callee, const Twine &name="")
void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition: CGDecl.cpp:1432
const CodeGen::CGBlockInfo * BlockInfo
void PushDestructorCleanup(QualType T, llvm::Value *Addr)
Definition: CGClass.cpp:1980
const TargetInfo & getTarget() const
bool hasConstructorVariants() const
Does this ABI have different entrypoints for complete-object and base-subobject constructors?
Definition: TargetCXXABI.h:169
bool isGenericLambda() const
Determine whether this class describes a generic lambda function object (i.e. function call operator ...
Definition: DeclCXX.cpp:985
ASTContext * Context
arg_iterator arg_end()
Definition: ExprCXX.h:1190
void EmitDelegateCallArg(CallArgList &args, const VarDecl *param, SourceLocation loc)
Definition: CGCall.cpp:2499
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF, CXXCtorInitializer *MemberInit, LValue &LHS)
Definition: CGClass.cpp:557
const CXXRecordDecl * getBase() const
getBase - Returns the base class declaration.
Definition: BaseSubobject.h:41
bool hasVolatile() const
Definition: Type.h:233
static TypeEvaluationKind getEvaluationKind(QualType T)
CXXDtorType
C++ destructor types.
Definition: ABI.h:34
llvm::Value * getBitFieldAddr() const
Definition: CGValue.h:283
const Type * getTypeForDecl() const
Definition: Decl.h:2557
CXXDtorType getDtorType() const
Definition: GlobalDecl.h:67
bool isInstance() const
Definition: DeclCXX.h:1744
bool CanDevirtualizeMemberFunctionCall(const Expr *Base, const CXXMethodDecl *MD)
Definition: CGClass.cpp:2300
CGCXXABI & getCXXABI() const
VarDecl * getVariable() const
The variable being captured.
Definition: Decl.h:3448
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
llvm::Value * GetAddrOfLocalVar(const VarDecl *VD)
GetAddrOfLocalVar - Return the address of a local variable.
bool isVirtual() const
Definition: DeclCXX.h:1761
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2358
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, llvm::Value *&addr)
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
Definition: RecordLayout.h:224
ASTContext & getContext() const
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:81
void EmitAsanPrologueOrEpilogue(bool Prologue)
Definition: CGClass.cpp:734
llvm::LLVMContext & getLLVMContext()
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
Base object dtor.
Definition: ABI.h:37
Expr * getSubExpr() const
Definition: Expr.h:1699
bool isIndirectMemberInitializer() const
Definition: DeclCXX.h:1992
void EmitConstructorBody(FunctionArgList &Args)
EmitConstructorBody - Emits the body of the current constructor.
Definition: CGClass.cpp:797
LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T)
void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator, CallArgList &CallArgs)
Definition: CGClass.cpp:2364
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition: DeclCXX.cpp:990
void EmitLambdaToBlockPointerBody(FunctionArgList &Args)
Definition: CGClass.cpp:2420
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
unsigned getNumBases() const
Retrieves the number of base classes of this class.
Definition: DeclCXX.h:707
The COMDAT used for dtors.
Definition: ABI.h:38
bool hasObjCLifetime() const
Definition: Type.h:286
static const RecordType * getRecordType(QualType QT)
Checks that the passed in QualType either is of RecordType or points to RecordType. Returns the relevant RecordType, null if it does not exit.
Stmt * getBody(const FunctionDecl *&Definition) const
Definition: Decl.cpp:2405
void set(SanitizerMask K, bool Value)
Enable or disable a certain (single) sanitizer.
Definition: Sanitizers.h:61
Enumerates target-specific builtins in their own namespaces within namespace clang.
virtual unsigned addImplicitConstructorArgs(CodeGenFunction &CGF, const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase, bool Delegating, CallArgList &Args)=0
#define false
Definition: stdbool.h:33
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
llvm::Constant * CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeSet ExtraAttrs=llvm::AttributeSet())
Create a new runtime function with the specified type and name.
void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase, bool Delegating, llvm::Value *This, const CXXConstructExpr *E)
Definition: CGClass.cpp:1774
bool isSimple() const
Definition: CGValue.h:193
bool mayInsertExtraPadding(bool EmitRemark=false) const
Whether we are allowed to insert extra padding between fields. These padding are added to help Addres...
Definition: Decl.cpp:3686
SmallVectorImpl< AnnotatedLine * >::const_iterator Next
virtual bool NeedsVTTParameter(GlobalDecl GD)
Return whether the given global decl needs a VTT parameter.
Definition: CGCXXABI.cpp:301
ASTContext & getContext() const
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
body_range body()
Definition: Stmt.h:585
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.h:2110
bool isBlacklistedType(StringRef MangledTypeName, StringRef Category=StringRef()) const
CharUnits getSize() const
getSize - Get the record size in characters.
Definition: RecordLayout.h:174
void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr, QualType EltTy, bool isVolatile=false, CharUnits Alignment=CharUnits::Zero(), bool isAssignment=false)
Definition: CGExprAgg.cpp:1425
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
Definition: RecordLayout.h:209
FieldDecl * getAnyMember() const
Definition: DeclCXX.h:2052
An aggregate value slot.
Definition: CGValue.h:363
init_iterator init_end()
Retrieve an iterator past the last initializer.
Definition: DeclCXX.h:2214
llvm::GlobalVariable * GetAddrOfVTT(const CXXRecordDecl *RD)
GetAddrOfVTT - Get the address of the VTT for the given record decl.
Definition: CGVTT.cpp:104
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1717
virtual size_t getSrcArgforCopyCtor(const CXXConstructorDecl *, FunctionArgList &Args) const =0
SourceLocation getSourceLocation() const
Determine the source location of the initializer.
Definition: DeclCXX.cpp:1716
const CXXRecordDecl * getBestDynamicClassType() const
For an expression of class type or pointer to class type, return the most derived class decl the expr...
Definition: Expr.cpp:39
void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty)
bool isBaseVirtual() const
Returns whether the base is virtual or not.
Definition: DeclCXX.h:2033
SanitizerSet SanOpts
Sanitizers enabled for this function.
const ConstantArrayType * getAsConstantArrayType(QualType T) const
Definition: ASTContext.h:2003
virtual llvm::Value * GetVirtualBaseClassOffset(CodeGenFunction &CGF, llvm::Value *This, const CXXRecordDecl *ClassDecl, const CXXRecordDecl *BaseClassDecl)=0
void pushEHDestroy(QualType::DestructionKind dtorKind, llvm::Value *addr, QualType type)
Definition: CGDecl.cpp:1371
const CodeGenOptions & getCodeGenOpts() const
const LangOptions & getLangOpts() const
const T * castAs() const
Definition: Type.h:5586
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:85
Complete object dtor.
Definition: ABI.h:36
unsigned getBuiltinID() const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:2601
bool isBitField() const
Definition: CGValue.h:195
const CGFunctionInfo & arrangeCXXMethodCall(const CallArgList &args, const FunctionProtoType *type, RequiredArgs required)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:446
static void EmitBaseInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *BaseInit, CXXCtorType CtorType)
Definition: CGClass.cpp:392
bool isDynamicClass() const
Definition: DeclCXX.h:693
Opcode getOpcode() const
Definition: Expr.h:1696
CXXCtorType
C++ constructor types.
Definition: ABI.h:25
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
param_range params()
Definition: Decl.h:1951
QualType getType() const
Definition: Expr.h:125
bool isAnonymousStructOrUnion() const
Definition: Decl.h:3294
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
static const Type * getElementType(const Expr *BaseExpr)
uint64_t SanitizerMask
Definition: Sanitizers.h:24
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. If T is a polymorphic class type...
Definition: CGClass.cpp:2176
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=0, bool ForVTable=false, bool DontDefer=false)
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition: CharUnits.h:116
llvm::Value * GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value, const CXXRecordDecl *Derived, const CXXRecordDecl *Base, bool BaseIsVirtual)
Definition: CGClass.cpp:82
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1302
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1639
void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, const FunctionArgList &Args)
Definition: CGClass.cpp:1923
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor)
Definition: CGClass.cpp:687
bool isUsed(bool CheckUsedAttr=true) const
Whether this declaration was used, meaning that a definition is required.
Definition: DeclBase.cpp:305
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:1855
FieldDecl * getMember() const
If this is a member initializer, returns the declaration of the non-static data member being initiali...
Definition: DeclCXX.h:2047
ConstEvaluatedExprVisitor - This class visits 'const Expr *'s.
unsigned getNumArgs() const
Definition: ExprCXX.h:1198
bool field_empty() const
Definition: Decl.h:3358
void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V, QualType Type, CharUnits Alignment=CharUnits::Zero(), bool SkipNullCheck=false)
Emit a check that V is the address of storage of the appropriate size and alignment for an object of ...
Definition: CGExpr.cpp:489
void EmitAggExpr(const Expr *E, AggValueSlot AS)
Definition: CGExprAgg.cpp:1403
JumpDest ReturnBlock
ReturnBlock - Unified return block.
virtual void initializeHiddenVirtualInheritanceMembers(CodeGenFunction &CGF, const CXXRecordDecl *RD)
Definition: CGCXXABI.h:264
void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, llvm::Value *This, llvm::Value *Src, const CXXConstructExpr *E)
Definition: CGClass.cpp:1826
bool isTriviallyCopyableType(ASTContext &Context) const
Definition: Type.cpp:2053
StructorType getFromCtorType(CXXCtorType T)
Definition: CodeGenTypes.h:77
const T * getAs() const
Definition: Type.h:5555
Represents a C++ base or member initializer.
Definition: DeclCXX.h:1901
void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD)
Definition: CGClass.cpp:2461
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:52
llvm::Constant * GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd)
Definition: CGClass.cpp:60
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition: CGDecl.cpp:604
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition: ExprCXX.h:1201
CXXConstructorDecl * getConstructor() const
Definition: ExprCXX.h:1137
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1505
bool isTrivial() const
Definition: Decl.h:1800
uint64_t getCharWidth() const
Return the size of the character type, in bits.
Definition: ASTContext.h:1705
virtual void EmitDestructorCall(CodeGenFunction &CGF, const CXXDestructorDecl *DD, CXXDtorType Type, bool ForVirtualBase, bool Delegating, llvm::Value *This)=0
Emit the destructor call.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
Definition: CGStmt.cpp:348
void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
QualType getTagDeclType(const TagDecl *Decl) const
Return the unique reference to the type for the specified TagDecl (struct/union/class/enum) decl...
Represents a base class of a C++ class.
Definition: DeclCXX.h:157
CharUnits getNonVirtualSize() const
Definition: RecordLayout.h:194
unsigned getFieldIndex() const
Definition: Decl.cpp:3350
bool isAnyMemberInitializer() const
Definition: DeclCXX.h:1988
void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body)
bool isDefaultConstructor() const
Definition: DeclCXX.cpp:1777
void InitializeVTablePointer(BaseSubobject Base, const CXXRecordDecl *NearestVBase, CharUnits OffsetFromNearestVBase, const CXXRecordDecl *VTableClass)
Definition: CGClass.cpp:1991
A template argument list.
Definition: DeclTemplate.h:150
bool isPODType(ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition: Type.cpp:1922
void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, const ConstantArrayType *ArrayTy, llvm::Value *ArrayPtr, const CXXConstructExpr *E, bool ZeroInitialization=false)
Definition: CGClass.cpp:1656
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:5096
static void EmitMemberInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *MemberInit, const CXXConstructorDecl *Constructor, FunctionArgList &Args)
Definition: CGClass.cpp:571
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
void EmitBranch(llvm::BasicBlock *Block)
Definition: CGStmt.cpp:368
bool IsCFIBlacklistedRecord(const CXXRecordDecl *RD)
Definition: CGVTables.cpp:848
llvm::Type * ConvertType(QualType T)
LValue EmitLValue(const Expr *E)
Definition: CGExpr.cpp:831
const BlockDecl * getBlockDecl() const
Definition: CGBlocks.h:243
void ForceCleanup()
Force the emission of cleanups now, instead of waiting until this object is destroyed.
CharUnits computeNonVirtualBaseClassOffset(const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, CastExpr::path_const_iterator End)
Definition: CGClass.cpp:32
const FunctionDecl * getOperatorDelete() const
Definition: DeclCXX.h:2383
A reference to a declared variable, function, enum, etc. [C99 6.5.1p2].
Definition: Expr.h:899
static RValue get(llvm::Value *V)
Definition: CGValue.h:71
void EmitDestructorBody(FunctionArgList &Args)
EmitDestructorBody - Emits the body of the current destructor.
Definition: CGClass.cpp:1390
void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock=false)
QualType getElementType() const
Definition: Type.h:2434
CXXCtorInitializer *const * init_const_iterator
Iterates through the member/base initializer list.
Definition: DeclCXX.h:2195
void EmitBranchThroughCleanup(JumpDest Dest)
Definition: CGCleanup.cpp:940
static AggValueSlot forAddr(llvm::Value *addr, CharUnits align, Qualifiers quals, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, IsZeroed_t isZeroed=IsNotZeroed)
Definition: CGValue.h:424
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition: CGExpr.cpp:2213
llvm::Value * GetAddressOfDerivedClass(llvm::Value *Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue)
Definition: CGClass.cpp:240
CodeGenVTables & getVTables()
CharUnits getBaseOffset() const
getBaseOffset - Returns the base class offset.
Definition: BaseSubobject.h:44
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
CodeGenTypes & getTypes() const
CharUnits alignmentAtOffset(CharUnits offset)
Definition: CharUnits.h:175
unsigned getNumVBases() const
Retrieves the number of virtual base classes of this class.
Definition: DeclCXX.h:728
static bool HasTrivialDestructorBody(ASTContext &Context, const CXXRecordDecl *BaseClassDecl, const CXXRecordDecl *MostDerivedClassDecl)
Definition: CGClass.cpp:1293
bool hasTrivialBody() const
Definition: Decl.cpp:2379
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
Definition: CGCleanup.cpp:583
base_class_range vbases()
Definition: DeclCXX.h:730
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
Declaration of a template function.
Definition: DeclTemplate.h:821
static bool FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field)
Definition: CGClass.cpp:1336
Structure with information about how a bitfield should be accessed.
llvm::MDNode * getTBAAInfoForVTablePtr()
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:5043
const CGFunctionInfo & arrangeCXXConstructorCall(const CallArgList &Args, const CXXConstructorDecl *D, CXXCtorType CtorKind, unsigned ExtraArgs)
Arrange a call to a C++ method, passing the given arguments.
Definition: CGCall.cpp:230
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1253