clang  9.0.0svn
CGNonTrivialStruct.cpp
Go to the documentation of this file.
1 //===--- CGNonTrivialStruct.cpp - Emit Special Functions for C Structs ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines functions to generate various special functions for C
10 // structs.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CodeGenModule.h"
18 #include "llvm/Support/ScopedPrinter.h"
19 #include <array>
20 
21 using namespace clang;
22 using namespace CodeGen;
23 
24 // Return the size of a field in number of bits.
25 static uint64_t getFieldSize(const FieldDecl *FD, QualType FT,
26  ASTContext &Ctx) {
27  if (FD && FD->isBitField())
28  return FD->getBitWidthValue(Ctx);
29  return Ctx.getTypeSize(FT);
30 }
31 
32 namespace {
33 enum { DstIdx = 0, SrcIdx = 1 };
34 const char *ValNameStr[2] = {"dst", "src"};
35 
36 template <class Derived> struct StructVisitor {
37  StructVisitor(ASTContext &Ctx) : Ctx(Ctx) {}
38 
39  template <class... Ts>
40  void visitStructFields(QualType QT, CharUnits CurStructOffset, Ts... Args) {
41  const RecordDecl *RD = QT->castAs<RecordType>()->getDecl();
42 
43  // Iterate over the fields of the struct.
44  for (const FieldDecl *FD : RD->fields()) {
45  QualType FT = FD->getType();
46  FT = QT.isVolatileQualified() ? FT.withVolatile() : FT;
47  asDerived().visit(FT, FD, CurStructOffset, Args...);
48  }
49 
50  asDerived().flushTrivialFields(Args...);
51  }
52 
53  template <class... Ts> void visitTrivial(Ts... Args) {}
54 
55  template <class... Ts> void visitCXXDestructor(Ts... Args) {
56  llvm_unreachable("field of a C++ struct type is not expected");
57  }
58 
59  template <class... Ts> void flushTrivialFields(Ts... Args) {}
60 
61  uint64_t getFieldOffsetInBits(const FieldDecl *FD) {
62  return FD ? Ctx.getASTRecordLayout(FD->getParent())
64  : 0;
65  }
66 
68  return Ctx.toCharUnitsFromBits(getFieldOffsetInBits(FD));
69  }
70 
71  Derived &asDerived() { return static_cast<Derived &>(*this); }
72 
73  ASTContext &getContext() { return Ctx; }
74  ASTContext &Ctx;
75 };
76 
77 template <class Derived, bool IsMove>
78 struct CopyStructVisitor : StructVisitor<Derived>,
79  CopiedTypeVisitor<Derived, IsMove> {
80  using StructVisitor<Derived>::asDerived;
82 
83  CopyStructVisitor(ASTContext &Ctx) : StructVisitor<Derived>(Ctx) {}
84 
85  template <class... Ts>
86  void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT,
87  const FieldDecl *FD, CharUnits CurStructOffset, Ts &&... Args) {
88  if (PCK)
89  asDerived().flushTrivialFields(std::forward<Ts>(Args)...);
90  }
91 
92  template <class... Ts>
93  void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT,
94  const FieldDecl *FD, CharUnits CurStructOffset,
95  Ts &&... Args) {
96  if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
97  asDerived().visitArray(PCK, AT, FT.isVolatileQualified(), FD,
98  CurStructOffset, std::forward<Ts>(Args)...);
99  return;
100  }
101 
102  Super::visitWithKind(PCK, FT, FD, CurStructOffset,
103  std::forward<Ts>(Args)...);
104  }
105 
106  template <class... Ts>
107  void visitTrivial(QualType FT, const FieldDecl *FD, CharUnits CurStructOffset,
108  Ts... Args) {
109  assert(!FT.isVolatileQualified() && "volatile field not expected");
110  ASTContext &Ctx = asDerived().getContext();
111  uint64_t FieldSize = getFieldSize(FD, FT, Ctx);
112 
113  // Ignore zero-sized fields.
114  if (FieldSize == 0)
115  return;
116 
117  uint64_t FStartInBits = asDerived().getFieldOffsetInBits(FD);
118  uint64_t FEndInBits = FStartInBits + FieldSize;
119  uint64_t RoundedFEnd = llvm::alignTo(FEndInBits, Ctx.getCharWidth());
120 
121  // Set Start if this is the first field of a sequence of trivial fields.
122  if (Start == End)
123  Start = CurStructOffset + Ctx.toCharUnitsFromBits(FStartInBits);
124  End = CurStructOffset + Ctx.toCharUnitsFromBits(RoundedFEnd);
125  }
126 
128 };
129 
130 // This function creates the mangled name of a special function of a non-trivial
131 // C struct. Since there is no ODR in C, the function is mangled based on the
132 // struct contents and not the name. The mangled name has the following
133 // structure:
134 //
135 // <function-name> ::= <prefix> <alignment-info> "_" <struct-field-info>
136 // <prefix> ::= "__destructor_" | "__default_constructor_" |
137 // "__copy_constructor_" | "__move_constructor_" |
138 // "__copy_assignment_" | "__move_assignment_"
139 // <alignment-info> ::= <dst-alignment> ["_" <src-alignment>]
140 // <struct-field-info> ::= <field-info>+
141 // <field-info> ::= <struct-or-scalar-field-info> | <array-field-info>
142 // <struct-or-scalar-field-info> ::= "_S" <struct-field-info> |
143 // <strong-field-info> | <trivial-field-info>
144 // <array-field-info> ::= "_AB" <array-offset> "s" <element-size> "n"
145 // <num-elements> <innermost-element-info> "_AE"
146 // <innermost-element-info> ::= <struct-or-scalar-field-info>
147 // <strong-field-info> ::= "_s" ["b"] ["v"] <field-offset>
148 // <trivial-field-info> ::= "_t" ["v"] <field-offset> "_" <field-size>
149 
150 template <class Derived> struct GenFuncNameBase {
151  std::string getVolatileOffsetStr(bool IsVolatile, CharUnits Offset) {
152  std::string S;
153  if (IsVolatile)
154  S = "v";
155  S += llvm::to_string(Offset.getQuantity());
156  return S;
157  }
158 
159  void visitARCStrong(QualType FT, const FieldDecl *FD,
160  CharUnits CurStructOffset) {
161  appendStr("_s");
162  if (FT->isBlockPointerType())
163  appendStr("b");
164  CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
165  appendStr(getVolatileOffsetStr(FT.isVolatileQualified(), FieldOffset));
166  }
167 
168  void visitARCWeak(QualType FT, const FieldDecl *FD,
169  CharUnits CurStructOffset) {
170  appendStr("_w");
171  CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
172  appendStr(getVolatileOffsetStr(FT.isVolatileQualified(), FieldOffset));
173  }
174 
175  void visitStruct(QualType QT, const FieldDecl *FD,
176  CharUnits CurStructOffset) {
177  CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
178  appendStr("_S");
179  asDerived().visitStructFields(QT, FieldOffset);
180  }
181 
182  template <class FieldKind>
183  void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
184  const FieldDecl *FD, CharUnits CurStructOffset) {
185  // String for non-volatile trivial fields is emitted when
186  // flushTrivialFields is called.
187  if (!FK)
188  return asDerived().visitTrivial(QualType(AT, 0), FD, CurStructOffset);
189 
190  asDerived().flushTrivialFields();
191  CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
192  ASTContext &Ctx = asDerived().getContext();
193  const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
194  unsigned NumElts = Ctx.getConstantArrayElementCount(CAT);
195  QualType EltTy = Ctx.getBaseElementType(CAT);
196  CharUnits EltSize = Ctx.getTypeSizeInChars(EltTy);
197  appendStr("_AB" + llvm::to_string(FieldOffset.getQuantity()) + "s" +
198  llvm::to_string(EltSize.getQuantity()) + "n" +
199  llvm::to_string(NumElts));
200  EltTy = IsVolatile ? EltTy.withVolatile() : EltTy;
201  asDerived().visitWithKind(FK, EltTy, nullptr, FieldOffset);
202  appendStr("_AE");
203  }
204 
205  void appendStr(StringRef Str) { Name += Str; }
206 
207  std::string getName(QualType QT, bool IsVolatile) {
208  QT = IsVolatile ? QT.withVolatile() : QT;
209  asDerived().visitStructFields(QT, CharUnits::Zero());
210  return Name;
211  }
212 
213  Derived &asDerived() { return static_cast<Derived &>(*this); }
214 
215  std::string Name;
216 };
217 
218 template <class Derived>
219 struct GenUnaryFuncName : StructVisitor<Derived>, GenFuncNameBase<Derived> {
220  GenUnaryFuncName(StringRef Prefix, CharUnits DstAlignment, ASTContext &Ctx)
221  : StructVisitor<Derived>(Ctx) {
222  this->appendStr(Prefix);
223  this->appendStr(llvm::to_string(DstAlignment.getQuantity()));
224  }
225 };
226 
227 // Helper function to create a null constant.
228 static llvm::Constant *getNullForVariable(Address Addr) {
229  llvm::Type *Ty = Addr.getElementType();
230  return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(Ty));
231 }
232 
233 template <bool IsMove>
234 struct GenBinaryFuncName : CopyStructVisitor<GenBinaryFuncName<IsMove>, IsMove>,
235  GenFuncNameBase<GenBinaryFuncName<IsMove>> {
236 
237  GenBinaryFuncName(StringRef Prefix, CharUnits DstAlignment,
238  CharUnits SrcAlignment, ASTContext &Ctx)
239  : CopyStructVisitor<GenBinaryFuncName<IsMove>, IsMove>(Ctx) {
240  this->appendStr(Prefix);
241  this->appendStr(llvm::to_string(DstAlignment.getQuantity()));
242  this->appendStr("_" + llvm::to_string(SrcAlignment.getQuantity()));
243  }
244 
245  void flushTrivialFields() {
246  if (this->Start == this->End)
247  return;
248 
249  this->appendStr("_t" + llvm::to_string(this->Start.getQuantity()) + "w" +
250  llvm::to_string((this->End - this->Start).getQuantity()));
251 
252  this->Start = this->End = CharUnits::Zero();
253  }
254 
255  void visitVolatileTrivial(QualType FT, const FieldDecl *FD,
256  CharUnits CurStructOffset) {
257  // Because volatile fields can be bit-fields and are individually copied,
258  // their offset and width are in bits.
259  uint64_t OffsetInBits =
260  this->Ctx.toBits(CurStructOffset) + this->getFieldOffsetInBits(FD);
261  this->appendStr("_tv" + llvm::to_string(OffsetInBits) + "w" +
262  llvm::to_string(getFieldSize(FD, FT, this->Ctx)));
263  }
264 };
265 
266 struct GenDefaultInitializeFuncName
267  : GenUnaryFuncName<GenDefaultInitializeFuncName>,
268  DefaultInitializedTypeVisitor<GenDefaultInitializeFuncName> {
270  GenDefaultInitializeFuncName(CharUnits DstAlignment, ASTContext &Ctx)
271  : GenUnaryFuncName<GenDefaultInitializeFuncName>("__default_constructor_",
272  DstAlignment, Ctx) {}
273  void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
274  const FieldDecl *FD, CharUnits CurStructOffset) {
275  if (const auto *AT = getContext().getAsArrayType(FT)) {
276  visitArray(PDIK, AT, FT.isVolatileQualified(), FD, CurStructOffset);
277  return;
278  }
279 
280  Super::visitWithKind(PDIK, FT, FD, CurStructOffset);
281  }
282 };
283 
284 struct GenDestructorFuncName : GenUnaryFuncName<GenDestructorFuncName>,
285  DestructedTypeVisitor<GenDestructorFuncName> {
287  GenDestructorFuncName(const char *Prefix, CharUnits DstAlignment,
288  ASTContext &Ctx)
289  : GenUnaryFuncName<GenDestructorFuncName>(Prefix, DstAlignment, Ctx) {}
290  void visitWithKind(QualType::DestructionKind DK, QualType FT,
291  const FieldDecl *FD, CharUnits CurStructOffset) {
292  if (const auto *AT = getContext().getAsArrayType(FT)) {
293  visitArray(DK, AT, FT.isVolatileQualified(), FD, CurStructOffset);
294  return;
295  }
296 
297  Super::visitWithKind(DK, FT, FD, CurStructOffset);
298  }
299 };
300 
301 // Helper function that creates CGFunctionInfo for an N-ary special function.
302 template <size_t N>
303 static const CGFunctionInfo &getFunctionInfo(CodeGenModule &CGM,
304  FunctionArgList &Args) {
305  ASTContext &Ctx = CGM.getContext();
307  QualType ParamTy = Ctx.getPointerType(Ctx.VoidPtrTy);
308 
309  for (unsigned I = 0; I < N; ++I)
310  Params.push_back(ImplicitParamDecl::Create(
311  Ctx, nullptr, SourceLocation(), &Ctx.Idents.get(ValNameStr[I]), ParamTy,
313 
314  for (auto &P : Params)
315  Args.push_back(P);
316 
317  return CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
318 }
319 
320 // Template classes that are used as bases for classes that emit special
321 // functions.
322 template <class Derived> struct GenFuncBase {
323  template <size_t N>
324  void visitStruct(QualType FT, const FieldDecl *FD, CharUnits CurStructOffset,
325  std::array<Address, N> Addrs) {
326  this->asDerived().callSpecialFunction(
327  FT, CurStructOffset + asDerived().getFieldOffset(FD), Addrs);
328  }
329 
330  template <class FieldKind, size_t N>
331  void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
332  const FieldDecl *FD, CharUnits CurStructOffset,
333  std::array<Address, N> Addrs) {
334  // Non-volatile trivial fields are copied when flushTrivialFields is called.
335  if (!FK)
336  return asDerived().visitTrivial(QualType(AT, 0), FD, CurStructOffset,
337  Addrs);
338 
339  asDerived().flushTrivialFields(Addrs);
340  CodeGenFunction &CGF = *this->CGF;
341  ASTContext &Ctx = CGF.getContext();
342 
343  // Compute the end address.
344  QualType BaseEltQT;
345  std::array<Address, N> StartAddrs = Addrs;
346  for (unsigned I = 0; I < N; ++I)
347  StartAddrs[I] = getAddrWithOffset(Addrs[I], CurStructOffset, FD);
348  Address DstAddr = StartAddrs[DstIdx];
349  llvm::Value *NumElts = CGF.emitArrayLength(AT, BaseEltQT, DstAddr);
350  unsigned BaseEltSize = Ctx.getTypeSizeInChars(BaseEltQT).getQuantity();
351  llvm::Value *BaseEltSizeVal =
352  llvm::ConstantInt::get(NumElts->getType(), BaseEltSize);
353  llvm::Value *SizeInBytes =
354  CGF.Builder.CreateNUWMul(BaseEltSizeVal, NumElts);
355  Address BC = CGF.Builder.CreateBitCast(DstAddr, CGF.CGM.Int8PtrTy);
356  llvm::Value *DstArrayEnd =
357  CGF.Builder.CreateInBoundsGEP(BC.getPointer(), SizeInBytes);
358  DstArrayEnd = CGF.Builder.CreateBitCast(DstArrayEnd, CGF.CGM.Int8PtrPtrTy,
359  "dstarray.end");
360  llvm::BasicBlock *PreheaderBB = CGF.Builder.GetInsertBlock();
361 
362  // Create the header block and insert the phi instructions.
363  llvm::BasicBlock *HeaderBB = CGF.createBasicBlock("loop.header");
364  CGF.EmitBlock(HeaderBB);
365  llvm::PHINode *PHIs[N];
366 
367  for (unsigned I = 0; I < N; ++I) {
368  PHIs[I] = CGF.Builder.CreatePHI(CGF.CGM.Int8PtrPtrTy, 2, "addr.cur");
369  PHIs[I]->addIncoming(StartAddrs[I].getPointer(), PreheaderBB);
370  }
371 
372  // Create the exit and loop body blocks.
373  llvm::BasicBlock *ExitBB = CGF.createBasicBlock("loop.exit");
374  llvm::BasicBlock *LoopBB = CGF.createBasicBlock("loop.body");
375 
376  // Emit the comparison and conditional branch instruction that jumps to
377  // either the exit or the loop body.
378  llvm::Value *Done =
379  CGF.Builder.CreateICmpEQ(PHIs[DstIdx], DstArrayEnd, "done");
380  CGF.Builder.CreateCondBr(Done, ExitBB, LoopBB);
381 
382  // Visit the element of the array in the loop body.
383  CGF.EmitBlock(LoopBB);
384  QualType EltQT = AT->getElementType();
385  CharUnits EltSize = Ctx.getTypeSizeInChars(EltQT);
386  std::array<Address, N> NewAddrs = Addrs;
387 
388  for (unsigned I = 0; I < N; ++I)
389  NewAddrs[I] = Address(
390  PHIs[I], StartAddrs[I].getAlignment().alignmentAtOffset(EltSize));
391 
392  EltQT = IsVolatile ? EltQT.withVolatile() : EltQT;
393  this->asDerived().visitWithKind(FK, EltQT, nullptr, CharUnits::Zero(),
394  NewAddrs);
395 
396  LoopBB = CGF.Builder.GetInsertBlock();
397 
398  for (unsigned I = 0; I < N; ++I) {
399  // Instrs to update the destination and source addresses.
400  // Update phi instructions.
401  NewAddrs[I] = getAddrWithOffset(NewAddrs[I], EltSize);
402  PHIs[I]->addIncoming(NewAddrs[I].getPointer(), LoopBB);
403  }
404 
405  // Insert an unconditional branch to the header block.
406  CGF.Builder.CreateBr(HeaderBB);
407  CGF.EmitBlock(ExitBB);
408  }
409 
410  /// Return an address with the specified offset from the passed address.
411  Address getAddrWithOffset(Address Addr, CharUnits Offset) {
412  assert(Addr.isValid() && "invalid address");
413  if (Offset.getQuantity() == 0)
414  return Addr;
415  Addr = CGF->Builder.CreateBitCast(Addr, CGF->CGM.Int8PtrTy);
416  Addr = CGF->Builder.CreateConstInBoundsGEP(Addr, Offset.getQuantity());
417  return CGF->Builder.CreateBitCast(Addr, CGF->CGM.Int8PtrPtrTy);
418  }
419 
420  Address getAddrWithOffset(Address Addr, CharUnits StructFieldOffset,
421  const FieldDecl *FD) {
422  return getAddrWithOffset(Addr, StructFieldOffset +
423  asDerived().getFieldOffset(FD));
424  }
425 
426  template <size_t N>
427  llvm::Function *
428  getFunction(StringRef FuncName, QualType QT, std::array<Address, N> Addrs,
429  std::array<CharUnits, N> Alignments, CodeGenModule &CGM) {
430  // If the special function already exists in the module, return it.
431  if (llvm::Function *F = CGM.getModule().getFunction(FuncName)) {
432  bool WrongType = false;
433  if (!F->getReturnType()->isVoidTy())
434  WrongType = true;
435  else {
436  for (const llvm::Argument &Arg : F->args())
437  if (Arg.getType() != CGM.Int8PtrPtrTy)
438  WrongType = true;
439  }
440 
441  if (WrongType) {
442  std::string FuncName = F->getName();
443  SourceLocation Loc = QT->castAs<RecordType>()->getDecl()->getLocation();
444  CGM.Error(Loc, "special function " + FuncName +
445  " for non-trivial C struct has incorrect type");
446  return nullptr;
447  }
448  return F;
449  }
450 
451  ASTContext &Ctx = CGM.getContext();
452  FunctionArgList Args;
453  const CGFunctionInfo &FI = getFunctionInfo<N>(CGM, Args);
454  llvm::FunctionType *FuncTy = CGM.getTypes().GetFunctionType(FI);
455  llvm::Function *F =
456  llvm::Function::Create(FuncTy, llvm::GlobalValue::LinkOnceODRLinkage,
457  FuncName, &CGM.getModule());
458  F->setVisibility(llvm::GlobalValue::HiddenVisibility);
459  CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F);
460  CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
461  IdentifierInfo *II = &Ctx.Idents.get(FuncName);
464  II, Ctx.getFunctionType(Ctx.VoidTy, llvm::None, {}), nullptr,
465  SC_PrivateExtern, false, false);
466  CodeGenFunction NewCGF(CGM);
467  setCGF(&NewCGF);
468  CGF->StartFunction(FD, Ctx.VoidTy, F, FI, Args);
469 
470  for (unsigned I = 0; I < N; ++I) {
471  llvm::Value *V = CGF->Builder.CreateLoad(CGF->GetAddrOfLocalVar(Args[I]));
472  Addrs[I] = Address(V, Alignments[I]);
473  }
474 
475  asDerived().visitStructFields(QT, CharUnits::Zero(), Addrs);
476  CGF->FinishFunction();
477  return F;
478  }
479 
480  template <size_t N>
481  void callFunc(StringRef FuncName, QualType QT, std::array<Address, N> Addrs,
482  CodeGenFunction &CallerCGF) {
483  std::array<CharUnits, N> Alignments;
484  llvm::Value *Ptrs[N];
485 
486  for (unsigned I = 0; I < N; ++I) {
487  Alignments[I] = Addrs[I].getAlignment();
488  Ptrs[I] =
489  CallerCGF.Builder.CreateBitCast(Addrs[I], CallerCGF.CGM.Int8PtrPtrTy)
490  .getPointer();
491  }
492 
493  if (llvm::Function *F =
494  getFunction(FuncName, QT, Addrs, Alignments, CallerCGF.CGM))
495  CallerCGF.EmitNounwindRuntimeCall(F, Ptrs);
496  }
497 
498  Derived &asDerived() { return static_cast<Derived &>(*this); }
499 
500  void setCGF(CodeGenFunction *F) { CGF = F; }
501 
502  CodeGenFunction *CGF = nullptr;
503 };
504 
505 template <class Derived, bool IsMove>
506 struct GenBinaryFunc : CopyStructVisitor<Derived, IsMove>,
507  GenFuncBase<Derived> {
508  GenBinaryFunc(ASTContext &Ctx) : CopyStructVisitor<Derived, IsMove>(Ctx) {}
509 
510  void flushTrivialFields(std::array<Address, 2> Addrs) {
511  CharUnits Size = this->End - this->Start;
512 
513  if (Size.getQuantity() == 0)
514  return;
515 
516  Address DstAddr = this->getAddrWithOffset(Addrs[DstIdx], this->Start);
517  Address SrcAddr = this->getAddrWithOffset(Addrs[SrcIdx], this->Start);
518 
519  // Emit memcpy.
520  if (Size.getQuantity() >= 16 || !llvm::isPowerOf2_32(Size.getQuantity())) {
521  llvm::Value *SizeVal =
522  llvm::ConstantInt::get(this->CGF->SizeTy, Size.getQuantity());
523  DstAddr =
524  this->CGF->Builder.CreateElementBitCast(DstAddr, this->CGF->Int8Ty);
525  SrcAddr =
526  this->CGF->Builder.CreateElementBitCast(SrcAddr, this->CGF->Int8Ty);
527  this->CGF->Builder.CreateMemCpy(DstAddr, SrcAddr, SizeVal, false);
528  } else {
529  llvm::Type *Ty = llvm::Type::getIntNTy(
530  this->CGF->getLLVMContext(),
531  Size.getQuantity() * this->CGF->getContext().getCharWidth());
532  DstAddr = this->CGF->Builder.CreateElementBitCast(DstAddr, Ty);
533  SrcAddr = this->CGF->Builder.CreateElementBitCast(SrcAddr, Ty);
534  llvm::Value *SrcVal = this->CGF->Builder.CreateLoad(SrcAddr, false);
535  this->CGF->Builder.CreateStore(SrcVal, DstAddr, false);
536  }
537 
538  this->Start = this->End = CharUnits::Zero();
539  }
540 
541  template <class... Ts>
542  void visitVolatileTrivial(QualType FT, const FieldDecl *FD, CharUnits Offset,
543  std::array<Address, 2> Addrs) {
544  LValue DstLV, SrcLV;
545  if (FD) {
546  QualType RT = QualType(FD->getParent()->getTypeForDecl(), 0);
547  llvm::PointerType *PtrTy = this->CGF->ConvertType(RT)->getPointerTo();
548  Address DstAddr = this->getAddrWithOffset(Addrs[DstIdx], Offset);
549  LValue DstBase = this->CGF->MakeAddrLValue(
550  this->CGF->Builder.CreateBitCast(DstAddr, PtrTy), FT);
551  DstLV = this->CGF->EmitLValueForField(DstBase, FD);
552  Address SrcAddr = this->getAddrWithOffset(Addrs[SrcIdx], Offset);
553  LValue SrcBase = this->CGF->MakeAddrLValue(
554  this->CGF->Builder.CreateBitCast(SrcAddr, PtrTy), FT);
555  SrcLV = this->CGF->EmitLValueForField(SrcBase, FD);
556  } else {
557  llvm::PointerType *Ty = this->CGF->ConvertType(FT)->getPointerTo();
558  Address DstAddr = this->CGF->Builder.CreateBitCast(Addrs[DstIdx], Ty);
559  Address SrcAddr = this->CGF->Builder.CreateBitCast(Addrs[SrcIdx], Ty);
560  DstLV = this->CGF->MakeAddrLValue(DstAddr, FT);
561  SrcLV = this->CGF->MakeAddrLValue(SrcAddr, FT);
562  }
563  RValue SrcVal = this->CGF->EmitLoadOfLValue(SrcLV, SourceLocation());
564  this->CGF->EmitStoreThroughLValue(SrcVal, DstLV);
565  }
566 };
567 
568 // These classes that emit the special functions for a non-trivial struct.
569 struct GenDestructor : StructVisitor<GenDestructor>,
570  GenFuncBase<GenDestructor>,
571  DestructedTypeVisitor<GenDestructor> {
573  GenDestructor(ASTContext &Ctx) : StructVisitor<GenDestructor>(Ctx) {}
574 
575  void visitWithKind(QualType::DestructionKind DK, QualType FT,
576  const FieldDecl *FD, CharUnits CurStructOffset,
577  std::array<Address, 1> Addrs) {
578  if (const auto *AT = getContext().getAsArrayType(FT)) {
579  visitArray(DK, AT, FT.isVolatileQualified(), FD, CurStructOffset, Addrs);
580  return;
581  }
582 
583  Super::visitWithKind(DK, FT, FD, CurStructOffset, Addrs);
584  }
585 
586  void visitARCStrong(QualType QT, const FieldDecl *FD,
587  CharUnits CurStructOffset, std::array<Address, 1> Addrs) {
588  CGF->destroyARCStrongImprecise(
589  *CGF, getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
590  }
591 
592  void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
593  std::array<Address, 1> Addrs) {
594  CGF->destroyARCWeak(
595  *CGF, getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
596  }
597 
599  std::array<Address, 1> Addrs) {
600  CGF->callCStructDestructor(
601  CGF->MakeAddrLValue(getAddrWithOffset(Addrs[DstIdx], Offset), FT));
602  }
603 };
604 
605 struct GenDefaultInitialize
606  : StructVisitor<GenDefaultInitialize>,
607  GenFuncBase<GenDefaultInitialize>,
608  DefaultInitializedTypeVisitor<GenDefaultInitialize> {
610  typedef GenFuncBase<GenDefaultInitialize> GenFuncBaseTy;
611 
612  GenDefaultInitialize(ASTContext &Ctx)
613  : StructVisitor<GenDefaultInitialize>(Ctx) {}
614 
615  void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
616  const FieldDecl *FD, CharUnits CurStructOffset,
617  std::array<Address, 1> Addrs) {
618  if (const auto *AT = getContext().getAsArrayType(FT)) {
619  visitArray(PDIK, AT, FT.isVolatileQualified(), FD, CurStructOffset,
620  Addrs);
621  return;
622  }
623 
624  Super::visitWithKind(PDIK, FT, FD, CurStructOffset, Addrs);
625  }
626 
627  void visitARCStrong(QualType QT, const FieldDecl *FD,
628  CharUnits CurStructOffset, std::array<Address, 1> Addrs) {
629  CGF->EmitNullInitialization(
630  getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
631  }
632 
633  void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
634  std::array<Address, 1> Addrs) {
635  CGF->EmitNullInitialization(
636  getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
637  }
638 
639  template <class FieldKind, size_t... Is>
640  void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
641  const FieldDecl *FD, CharUnits CurStructOffset,
642  std::array<Address, 1> Addrs) {
643  if (!FK)
644  return visitTrivial(QualType(AT, 0), FD, CurStructOffset, Addrs);
645 
646  ASTContext &Ctx = getContext();
647  CharUnits Size = Ctx.getTypeSizeInChars(QualType(AT, 0));
648  QualType EltTy = Ctx.getBaseElementType(QualType(AT, 0));
649 
650  if (Size < CharUnits::fromQuantity(16) || EltTy->getAs<RecordType>()) {
651  GenFuncBaseTy::visitArray(FK, AT, IsVolatile, FD, CurStructOffset, Addrs);
652  return;
653  }
654 
655  llvm::Constant *SizeVal = CGF->Builder.getInt64(Size.getQuantity());
656  Address DstAddr = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
657  Address Loc = CGF->Builder.CreateElementBitCast(DstAddr, CGF->Int8Ty);
658  CGF->Builder.CreateMemSet(Loc, CGF->Builder.getInt8(0), SizeVal,
659  IsVolatile);
660  }
661 
663  std::array<Address, 1> Addrs) {
664  CGF->callCStructDefaultConstructor(
665  CGF->MakeAddrLValue(getAddrWithOffset(Addrs[DstIdx], Offset), FT));
666  }
667 };
668 
669 struct GenCopyConstructor : GenBinaryFunc<GenCopyConstructor, false> {
670  GenCopyConstructor(ASTContext &Ctx)
671  : GenBinaryFunc<GenCopyConstructor, false>(Ctx) {}
672 
673  void visitARCStrong(QualType QT, const FieldDecl *FD,
674  CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
675  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
676  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
677  llvm::Value *SrcVal = CGF->EmitLoadOfScalar(
678  Addrs[SrcIdx], QT.isVolatileQualified(), QT, SourceLocation());
679  llvm::Value *Val = CGF->EmitARCRetain(QT, SrcVal);
680  CGF->EmitStoreOfScalar(Val, CGF->MakeAddrLValue(Addrs[DstIdx], QT), true);
681  }
682 
683  void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
684  std::array<Address, 2> Addrs) {
685  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
686  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
687  CGF->EmitARCCopyWeak(Addrs[DstIdx], Addrs[SrcIdx]);
688  }
689 
691  std::array<Address, 2> Addrs) {
692  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
693  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
694  CGF->callCStructCopyConstructor(CGF->MakeAddrLValue(Addrs[DstIdx], FT),
695  CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
696  }
697 };
698 
699 struct GenMoveConstructor : GenBinaryFunc<GenMoveConstructor, true> {
700  GenMoveConstructor(ASTContext &Ctx)
701  : GenBinaryFunc<GenMoveConstructor, true>(Ctx) {}
702 
703  void visitARCStrong(QualType QT, const FieldDecl *FD,
704  CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
705  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
706  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
707  LValue SrcLV = CGF->MakeAddrLValue(Addrs[SrcIdx], QT);
708  llvm::Value *SrcVal =
709  CGF->EmitLoadOfLValue(SrcLV, SourceLocation()).getScalarVal();
710  CGF->EmitStoreOfScalar(getNullForVariable(SrcLV.getAddress()), SrcLV);
711  CGF->EmitStoreOfScalar(SrcVal, CGF->MakeAddrLValue(Addrs[DstIdx], QT),
712  /* isInitialization */ true);
713  }
714 
715  void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
716  std::array<Address, 2> Addrs) {
717  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
718  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
719  CGF->EmitARCMoveWeak(Addrs[DstIdx], Addrs[SrcIdx]);
720  }
721 
723  std::array<Address, 2> Addrs) {
724  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
725  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
726  CGF->callCStructMoveConstructor(CGF->MakeAddrLValue(Addrs[DstIdx], FT),
727  CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
728  }
729 };
730 
731 struct GenCopyAssignment : GenBinaryFunc<GenCopyAssignment, false> {
732  GenCopyAssignment(ASTContext &Ctx)
733  : GenBinaryFunc<GenCopyAssignment, false>(Ctx) {}
734 
735  void visitARCStrong(QualType QT, const FieldDecl *FD,
736  CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
737  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
738  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
739  llvm::Value *SrcVal = CGF->EmitLoadOfScalar(
740  Addrs[SrcIdx], QT.isVolatileQualified(), QT, SourceLocation());
741  CGF->EmitARCStoreStrong(CGF->MakeAddrLValue(Addrs[DstIdx], QT), SrcVal,
742  false);
743  }
744 
745  void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
746  std::array<Address, 2> Addrs) {
747  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
748  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
749  CGF->emitARCCopyAssignWeak(QT, Addrs[DstIdx], Addrs[SrcIdx]);
750  }
751 
753  std::array<Address, 2> Addrs) {
754  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
755  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
756  CGF->callCStructCopyAssignmentOperator(
757  CGF->MakeAddrLValue(Addrs[DstIdx], FT),
758  CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
759  }
760 };
761 
762 struct GenMoveAssignment : GenBinaryFunc<GenMoveAssignment, true> {
763  GenMoveAssignment(ASTContext &Ctx)
764  : GenBinaryFunc<GenMoveAssignment, true>(Ctx) {}
765 
766  void visitARCStrong(QualType QT, const FieldDecl *FD,
767  CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
768  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
769  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
770  LValue SrcLV = CGF->MakeAddrLValue(Addrs[SrcIdx], QT);
771  llvm::Value *SrcVal =
772  CGF->EmitLoadOfLValue(SrcLV, SourceLocation()).getScalarVal();
773  CGF->EmitStoreOfScalar(getNullForVariable(SrcLV.getAddress()), SrcLV);
774  LValue DstLV = CGF->MakeAddrLValue(Addrs[DstIdx], QT);
775  llvm::Value *DstVal =
776  CGF->EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal();
777  CGF->EmitStoreOfScalar(SrcVal, DstLV);
778  CGF->EmitARCRelease(DstVal, ARCImpreciseLifetime);
779  }
780 
781  void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
782  std::array<Address, 2> Addrs) {
783  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
784  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
785  CGF->emitARCMoveAssignWeak(QT, Addrs[DstIdx], Addrs[SrcIdx]);
786  }
787 
789  std::array<Address, 2> Addrs) {
790  Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
791  Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
792  CGF->callCStructMoveAssignmentOperator(
793  CGF->MakeAddrLValue(Addrs[DstIdx], FT),
794  CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
795  }
796 };
797 
798 } // namespace
799 
801  Address Addr, QualType Type) {
802  CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Type));
803 }
804 
805 // Default-initialize a variable that is a non-trivial struct or an array of
806 // such structure.
808  GenDefaultInitialize Gen(getContext());
809  Address DstPtr = Builder.CreateBitCast(Dst.getAddress(), CGM.Int8PtrPtrTy);
810  Gen.setCGF(this);
811  QualType QT = Dst.getType();
812  QT = Dst.isVolatile() ? QT.withVolatile() : QT;
813  Gen.visit(QT, nullptr, CharUnits::Zero(), std::array<Address, 1>({{DstPtr}}));
814 }
815 
816 template <class G, size_t N>
817 static void callSpecialFunction(G &&Gen, StringRef FuncName, QualType QT,
818  bool IsVolatile, CodeGenFunction &CGF,
819  std::array<Address, N> Addrs) {
820  for (unsigned I = 0; I < N; ++I)
821  Addrs[I] = CGF.Builder.CreateBitCast(Addrs[I], CGF.CGM.Int8PtrPtrTy);
822  QT = IsVolatile ? QT.withVolatile() : QT;
823  Gen.callFunc(FuncName, QT, Addrs, CGF);
824 }
825 
826 template <size_t N> std::array<Address, N> createNullAddressArray();
827 
828 template <> std::array<Address, 1> createNullAddressArray() {
829  return std::array<Address, 1>({{Address(nullptr, CharUnits::Zero())}});
830 }
831 
832 template <> std::array<Address, 2> createNullAddressArray() {
833  return std::array<Address, 2>({{Address(nullptr, CharUnits::Zero()),
834  Address(nullptr, CharUnits::Zero())}});
835 }
836 
837 template <class G, size_t N>
838 static llvm::Function *
839 getSpecialFunction(G &&Gen, StringRef FuncName, QualType QT, bool IsVolatile,
840  std::array<CharUnits, N> Alignments, CodeGenModule &CGM) {
841  QT = IsVolatile ? QT.withVolatile() : QT;
842  // The following call requires an array of addresses as arguments, but doesn't
843  // actually use them (it overwrites them with the addresses of the arguments
844  // of the created function).
845  return Gen.getFunction(FuncName, QT, createNullAddressArray<N>(), Alignments,
846  CGM);
847 }
848 
849 // Functions to emit calls to the special functions of a non-trivial C struct.
851  bool IsVolatile = Dst.isVolatile();
852  Address DstPtr = Dst.getAddress();
853  QualType QT = Dst.getType();
854  GenDefaultInitializeFuncName GenName(DstPtr.getAlignment(), getContext());
855  std::string FuncName = GenName.getName(QT, IsVolatile);
856  callSpecialFunction(GenDefaultInitialize(getContext()), FuncName, QT,
857  IsVolatile, *this, std::array<Address, 1>({{DstPtr}}));
858 }
859 
861  QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx) {
862  GenBinaryFuncName<false> GenName("", Alignment, Alignment, Ctx);
863  return GenName.getName(QT, IsVolatile);
864 }
865 
867  CharUnits Alignment,
868  bool IsVolatile,
869  ASTContext &Ctx) {
870  GenDestructorFuncName GenName("", Alignment, Ctx);
871  return GenName.getName(QT, IsVolatile);
872 }
873 
875  bool IsVolatile = Dst.isVolatile();
876  Address DstPtr = Dst.getAddress();
877  QualType QT = Dst.getType();
878  GenDestructorFuncName GenName("__destructor_", DstPtr.getAlignment(),
879  getContext());
880  std::string FuncName = GenName.getName(QT, IsVolatile);
881  callSpecialFunction(GenDestructor(getContext()), FuncName, QT, IsVolatile,
882  *this, std::array<Address, 1>({{DstPtr}}));
883 }
884 
886  bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
887  Address DstPtr = Dst.getAddress(), SrcPtr = Src.getAddress();
888  QualType QT = Dst.getType();
889  GenBinaryFuncName<false> GenName("__copy_constructor_", DstPtr.getAlignment(),
890  SrcPtr.getAlignment(), getContext());
891  std::string FuncName = GenName.getName(QT, IsVolatile);
892  callSpecialFunction(GenCopyConstructor(getContext()), FuncName, QT,
893  IsVolatile, *this,
894  std::array<Address, 2>({{DstPtr, SrcPtr}}));
895 }
896 
898 
899 ) {
900  bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
901  Address DstPtr = Dst.getAddress(), SrcPtr = Src.getAddress();
902  QualType QT = Dst.getType();
903  GenBinaryFuncName<false> GenName("__copy_assignment_", DstPtr.getAlignment(),
904  SrcPtr.getAlignment(), getContext());
905  std::string FuncName = GenName.getName(QT, IsVolatile);
906  callSpecialFunction(GenCopyAssignment(getContext()), FuncName, QT, IsVolatile,
907  *this, std::array<Address, 2>({{DstPtr, SrcPtr}}));
908 }
909 
911  bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
912  Address DstPtr = Dst.getAddress(), SrcPtr = Src.getAddress();
913  QualType QT = Dst.getType();
914  GenBinaryFuncName<true> GenName("__move_constructor_", DstPtr.getAlignment(),
915  SrcPtr.getAlignment(), getContext());
916  std::string FuncName = GenName.getName(QT, IsVolatile);
917  callSpecialFunction(GenMoveConstructor(getContext()), FuncName, QT,
918  IsVolatile, *this,
919  std::array<Address, 2>({{DstPtr, SrcPtr}}));
920 }
921 
923 
924 ) {
925  bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
926  Address DstPtr = Dst.getAddress(), SrcPtr = Src.getAddress();
927  QualType QT = Dst.getType();
928  GenBinaryFuncName<true> GenName("__move_assignment_", DstPtr.getAlignment(),
929  SrcPtr.getAlignment(), getContext());
930  std::string FuncName = GenName.getName(QT, IsVolatile);
931  callSpecialFunction(GenMoveAssignment(getContext()), FuncName, QT, IsVolatile,
932  *this, std::array<Address, 2>({{DstPtr, SrcPtr}}));
933 }
934 
936  CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT) {
937  ASTContext &Ctx = CGM.getContext();
938  GenDefaultInitializeFuncName GenName(DstAlignment, Ctx);
939  std::string FuncName = GenName.getName(QT, IsVolatile);
940  return getSpecialFunction(GenDefaultInitialize(Ctx), FuncName, QT, IsVolatile,
941  std::array<CharUnits, 1>({{DstAlignment}}), CGM);
942 }
943 
945  CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
946  bool IsVolatile, QualType QT) {
947  ASTContext &Ctx = CGM.getContext();
948  GenBinaryFuncName<false> GenName("__copy_constructor_", DstAlignment,
949  SrcAlignment, Ctx);
950  std::string FuncName = GenName.getName(QT, IsVolatile);
951  return getSpecialFunction(
952  GenCopyConstructor(Ctx), FuncName, QT, IsVolatile,
953  std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
954 }
955 
957  CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
958  bool IsVolatile, QualType QT) {
959  ASTContext &Ctx = CGM.getContext();
960  GenBinaryFuncName<true> GenName("__move_constructor_", DstAlignment,
961  SrcAlignment, Ctx);
962  std::string FuncName = GenName.getName(QT, IsVolatile);
963  return getSpecialFunction(
964  GenMoveConstructor(Ctx), FuncName, QT, IsVolatile,
965  std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
966 }
967 
969  CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
970  bool IsVolatile, QualType QT) {
971  ASTContext &Ctx = CGM.getContext();
972  GenBinaryFuncName<false> GenName("__copy_assignment_", DstAlignment,
973  SrcAlignment, Ctx);
974  std::string FuncName = GenName.getName(QT, IsVolatile);
975  return getSpecialFunction(
976  GenCopyAssignment(Ctx), FuncName, QT, IsVolatile,
977  std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
978 }
979 
981  CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
982  bool IsVolatile, QualType QT) {
983  ASTContext &Ctx = CGM.getContext();
984  GenBinaryFuncName<true> GenName("__move_assignment_", DstAlignment,
985  SrcAlignment, Ctx);
986  std::string FuncName = GenName.getName(QT, IsVolatile);
987  return getSpecialFunction(
988  GenMoveAssignment(Ctx), FuncName, QT, IsVolatile,
989  std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
990 }
991 
993  CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT) {
994  ASTContext &Ctx = CGM.getContext();
995  GenDestructorFuncName GenName("__destructor_", DstAlignment, Ctx);
996  std::string FuncName = GenName.getName(QT, IsVolatile);
997  return getSpecialFunction(GenDestructor(Ctx), FuncName, QT, IsVolatile,
998  std::array<CharUnits, 1>({{DstAlignment}}), CGM);
999 }
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:652
static uint64_t getFieldOffset(const ASTContext &C, const FieldDecl *FD)
Represents a function declaration or definition.
Definition: Decl.h:1748
static FunctionDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation NLoc, DeclarationName N, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool isInlineSpecified=false, bool hasWrittenPrototype=true, ConstexprSpecKind ConstexprKind=CSK_unspecified)
Definition: Decl.h:1895
Other implicit parameter.
Definition: Decl.h:1524
CanQualType VoidPtrTy
Definition: ASTContext.h:1042
A (possibly-)qualified type.
Definition: Type.h:643
bool isBlockPointerType() const
Definition: Type.h:6359
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D...
const Type * getTypeForDecl() const
Definition: Decl.h:2924
StringRef P
unsigned getFieldIndex() const
Returns the index of this field within its record, as appropriate for passing to ASTRecordLayout::get...
Definition: Decl.cpp:3916
bool isVolatile() const
Definition: CGValue.h:300
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined...
Definition: Decl.h:2789
The base class of the type hierarchy.
Definition: Type.h:1418
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2829
QualType getElementType() const
Definition: Type.h:2864
Objects with "hidden" visibility are not seen by the dynamic linker.
Definition: Visibility.h:36
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6818
llvm::Value * getPointer() const
Definition: Address.h:37
std::string getName(ArrayRef< StringRef > Parts) const
Get the platform-specific name separator.
Represents a struct/union/class.
Definition: Decl.h:3619
static llvm::Function * getSpecialFunction(G &&Gen, StringRef FuncName, QualType QT, bool IsVolatile, std::array< CharUnits, N > Alignments, CodeGenModule &CGM)
One of these records is kept for each identifier that is lexed.
Address getAddress() const
Definition: CGValue.h:326
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
field_range fields() const
Definition: Decl.h:3810
Represents a member of a struct/union/class.
Definition: Decl.h:2605
static uint64_t getFieldSize(const FieldDecl *FD, QualType FT, ASTContext &Ctx)
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:52
IdentifierTable & Idents
Definition: ASTContext.h:569
uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const
Return number of constant array elements.
bool isBitField() const
Determines whether this field is a bitfield.
Definition: Decl.h:2683
void defaultInitNonTrivialCStructVar(LValue Dst)
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:37
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:66
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6197
llvm::Function * getNonTrivialCStructCopyConstructor(CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, bool IsVolatile, QualType QT)
Returns the copy constructor for a C struct with non-trivially copyable fields, generating it if nece...
unsigned getBitWidthValue(const ASTContext &Ctx) const
Definition: Decl.cpp:3906
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
void callCStructMoveConstructor(LValue Dst, LValue Src)
PrimitiveDefaultInitializeKind
Definition: Type.h:1078
static std::string getNonTrivialCopyConstructorStr(QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx)
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition: Decl.cpp:4524
bool isValid() const
Definition: Address.h:35
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:178
unsigned Offset
Definition: Format.cpp:1711
void callCStructMoveAssignmentOperator(LValue Dst, LValue Src)
SourceLocation End
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:6883
#define V(N, I)
Definition: ASTContext.h:2912
std::array< Address, N > createNullAddressArray()
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:62
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Definition: ASTContext.h:1382
llvm::Function * getNonTrivialCStructMoveAssignmentOperator(CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, bool IsVolatile, QualType QT)
Return the move assignment operator for a C struct with non-trivially copyable fields, generating it if necessary.
ASTContext & getContext() const
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:40
void callCStructCopyAssignmentOperator(LValue Dst, LValue Src)
The l-value was considered opaque, so the alignment was determined from a type.
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:141
Encodes a location in the source.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
CanQualType VoidTy
Definition: ASTContext.h:1014
An aligned address.
Definition: Address.h:24
QualType getType() const
Definition: CGValue.h:263
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can&#39;t be done.
llvm::Function * getNonTrivialCStructDestructor(CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT)
Returns the destructor for a C struct with non-trivially copyable fields, generating it if necessary...
static void callSpecialFunction(G &&Gen, StringRef FuncName, QualType QT, bool IsVolatile, CodeGenFunction &CGF, std::array< Address, N > Addrs)
FunctionArgList - Type for representing both the decl and type of parameters to a function...
Definition: CGCall.h:358
QualType withVolatile() const
Definition: Type.h:823
CGFunctionInfo - Class to encapsulate the information about a function definition.
This class organizes the cross-function state that is used while generating LLVM code.
Dataflow Directional Tag Classes.
llvm::Function * getNonTrivialCStructDefaultConstructor(CodeGenModule &GCM, CharUnits DstAlignment, bool IsVolatile, QualType QT)
Returns the default constructor for a C struct with non-trivially copyable fields, generating it if necessary.
llvm::Function * getNonTrivialCStructCopyAssignmentOperator(CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, bool IsVolatile, QualType QT)
Returns the copy assignment operator for a C struct with non-trivially copyable fields, generating it if necessary.
llvm::Module & getModule() const
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4423
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:51
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:450
static Destroyer destroyNonTrivialCStruct
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
Definition: ASTContext.h:2079
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
void callCStructCopyConstructor(LValue Dst, LValue Src)
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1007
static std::string getNonTrivialDestructorStr(QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx)
static llvm::Constant * getNullForVariable(Address addr)
Given the address of a variable of pointer type, find the correct null to store into it...
Definition: CGObjC.cpp:39
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
LValue - This represents an lvalue references.
Definition: CGValue.h:166
llvm::Function * getNonTrivialCStructMoveConstructor(CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment, bool IsVolatile, QualType QT)
Returns the move constructor for a C struct with non-trivially copyable fields, generating it if nece...
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it&#39;s a VLA, and drill down to the base elem...
Represents the canonical version of C arrays with a specified constant size.
Definition: Type.h:2889
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr *> VL, ArrayRef< Expr *> PL, ArrayRef< Expr *> IL, Expr *Step, Expr *CalcStep, Stmt *PreInit, Expr *PostUpdate)
Creates clause with a list of variables VL and a linear step Step.