CGNonTrivialStruct.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. #include "CodeGenFunction.h"
  14. #include "CodeGenModule.h"
  15. #include "clang/AST/NonTrivialTypeVisitor.h"
  16. #include "clang/CodeGen/CodeGenABITypes.h"
  17. #include "llvm/Support/ScopedPrinter.h"
  18. #include <array>
  19. using namespace clang;
  20. using namespace CodeGen;
  21. // Return the size of a field in number of bits.
  22. static uint64_t getFieldSize(const FieldDecl *FD, QualType FT,
  23. ASTContext &Ctx) {
  24. if (FD && FD->isBitField())
  25. return FD->getBitWidthValue(Ctx);
  26. return Ctx.getTypeSize(FT);
  27. }
  28. namespace {
  29. enum { DstIdx = 0, SrcIdx = 1 };
  30. const char *ValNameStr[2] = {"dst", "src"};
  31. template <class Derived> struct StructVisitor {
  32. StructVisitor(ASTContext &Ctx) : Ctx(Ctx) {}
  33. template <class... Ts>
  34. void visitStructFields(QualType QT, CharUnits CurStructOffset, Ts... Args) {
  35. const RecordDecl *RD = QT->castAs<RecordType>()->getDecl();
  36. // Iterate over the fields of the struct.
  37. for (const FieldDecl *FD : RD->fields()) {
  38. QualType FT = FD->getType();
  39. FT = QT.isVolatileQualified() ? FT.withVolatile() : FT;
  40. asDerived().visit(FT, FD, CurStructOffset, Args...);
  41. }
  42. asDerived().flushTrivialFields(Args...);
  43. }
  44. template <class... Ts> void visitTrivial(Ts... Args) {}
  45. template <class... Ts> void visitCXXDestructor(Ts... Args) {
  46. llvm_unreachable("field of a C++ struct type is not expected");
  47. }
  48. template <class... Ts> void flushTrivialFields(Ts... Args) {}
  49. uint64_t getFieldOffsetInBits(const FieldDecl *FD) {
  50. return FD ? Ctx.getASTRecordLayout(FD->getParent())
  51. .getFieldOffset(FD->getFieldIndex())
  52. : 0;
  53. }
  54. CharUnits getFieldOffset(const FieldDecl *FD) {
  55. return Ctx.toCharUnitsFromBits(getFieldOffsetInBits(FD));
  56. }
  57. Derived &asDerived() { return static_cast<Derived &>(*this); }
  58. ASTContext &getContext() { return Ctx; }
  59. ASTContext &Ctx;
  60. };
  61. template <class Derived, bool IsMove>
  62. struct CopyStructVisitor : StructVisitor<Derived>,
  63. CopiedTypeVisitor<Derived, IsMove> {
  64. using StructVisitor<Derived>::asDerived;
  65. using Super = CopiedTypeVisitor<Derived, IsMove>;
  66. CopyStructVisitor(ASTContext &Ctx) : StructVisitor<Derived>(Ctx) {}
  67. template <class... Ts>
  68. void preVisit(QualType::PrimitiveCopyKind PCK, QualType FT,
  69. const FieldDecl *FD, CharUnits CurStructOffset, Ts &&... Args) {
  70. if (PCK)
  71. asDerived().flushTrivialFields(std::forward<Ts>(Args)...);
  72. }
  73. template <class... Ts>
  74. void visitWithKind(QualType::PrimitiveCopyKind PCK, QualType FT,
  75. const FieldDecl *FD, CharUnits CurStructOffset,
  76. Ts &&... Args) {
  77. if (const auto *AT = asDerived().getContext().getAsArrayType(FT)) {
  78. asDerived().visitArray(PCK, AT, FT.isVolatileQualified(), FD,
  79. CurStructOffset, std::forward<Ts>(Args)...);
  80. return;
  81. }
  82. Super::visitWithKind(PCK, FT, FD, CurStructOffset,
  83. std::forward<Ts>(Args)...);
  84. }
  85. template <class... Ts>
  86. void visitTrivial(QualType FT, const FieldDecl *FD, CharUnits CurStructOffset,
  87. Ts... Args) {
  88. assert(!FT.isVolatileQualified() && "volatile field not expected");
  89. ASTContext &Ctx = asDerived().getContext();
  90. uint64_t FieldSize = getFieldSize(FD, FT, Ctx);
  91. // Ignore zero-sized fields.
  92. if (FieldSize == 0)
  93. return;
  94. uint64_t FStartInBits = asDerived().getFieldOffsetInBits(FD);
  95. uint64_t FEndInBits = FStartInBits + FieldSize;
  96. uint64_t RoundedFEnd = llvm::alignTo(FEndInBits, Ctx.getCharWidth());
  97. // Set Start if this is the first field of a sequence of trivial fields.
  98. if (Start == End)
  99. Start = CurStructOffset + Ctx.toCharUnitsFromBits(FStartInBits);
  100. End = CurStructOffset + Ctx.toCharUnitsFromBits(RoundedFEnd);
  101. }
  102. CharUnits Start = CharUnits::Zero(), End = CharUnits::Zero();
  103. };
  104. // This function creates the mangled name of a special function of a non-trivial
  105. // C struct. Since there is no ODR in C, the function is mangled based on the
  106. // struct contents and not the name. The mangled name has the following
  107. // structure:
  108. //
  109. // <function-name> ::= <prefix> <alignment-info> "_" <struct-field-info>
  110. // <prefix> ::= "__destructor_" | "__default_constructor_" |
  111. // "__copy_constructor_" | "__move_constructor_" |
  112. // "__copy_assignment_" | "__move_assignment_"
  113. // <alignment-info> ::= <dst-alignment> ["_" <src-alignment>]
  114. // <struct-field-info> ::= <field-info>+
  115. // <field-info> ::= <struct-or-scalar-field-info> | <array-field-info>
  116. // <struct-or-scalar-field-info> ::= "_S" <struct-field-info> |
  117. // <strong-field-info> | <trivial-field-info>
  118. // <array-field-info> ::= "_AB" <array-offset> "s" <element-size> "n"
  119. // <num-elements> <innermost-element-info> "_AE"
  120. // <innermost-element-info> ::= <struct-or-scalar-field-info>
  121. // <strong-field-info> ::= "_s" ["b"] ["v"] <field-offset>
  122. // <trivial-field-info> ::= "_t" ["v"] <field-offset> "_" <field-size>
  123. template <class Derived> struct GenFuncNameBase {
  124. std::string getVolatileOffsetStr(bool IsVolatile, CharUnits Offset) {
  125. std::string S;
  126. if (IsVolatile)
  127. S = "v";
  128. S += llvm::to_string(Offset.getQuantity());
  129. return S;
  130. }
  131. void visitARCStrong(QualType FT, const FieldDecl *FD,
  132. CharUnits CurStructOffset) {
  133. appendStr("_s");
  134. if (FT->isBlockPointerType())
  135. appendStr("b");
  136. CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
  137. appendStr(getVolatileOffsetStr(FT.isVolatileQualified(), FieldOffset));
  138. }
  139. void visitARCWeak(QualType FT, const FieldDecl *FD,
  140. CharUnits CurStructOffset) {
  141. appendStr("_w");
  142. CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
  143. appendStr(getVolatileOffsetStr(FT.isVolatileQualified(), FieldOffset));
  144. }
  145. void visitStruct(QualType QT, const FieldDecl *FD,
  146. CharUnits CurStructOffset) {
  147. CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
  148. appendStr("_S");
  149. asDerived().visitStructFields(QT, FieldOffset);
  150. }
  151. template <class FieldKind>
  152. void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
  153. const FieldDecl *FD, CharUnits CurStructOffset) {
  154. // String for non-volatile trivial fields is emitted when
  155. // flushTrivialFields is called.
  156. if (!FK)
  157. return asDerived().visitTrivial(QualType(AT, 0), FD, CurStructOffset);
  158. asDerived().flushTrivialFields();
  159. CharUnits FieldOffset = CurStructOffset + asDerived().getFieldOffset(FD);
  160. ASTContext &Ctx = asDerived().getContext();
  161. const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
  162. unsigned NumElts = Ctx.getConstantArrayElementCount(CAT);
  163. QualType EltTy = Ctx.getBaseElementType(CAT);
  164. CharUnits EltSize = Ctx.getTypeSizeInChars(EltTy);
  165. appendStr("_AB" + llvm::to_string(FieldOffset.getQuantity()) + "s" +
  166. llvm::to_string(EltSize.getQuantity()) + "n" +
  167. llvm::to_string(NumElts));
  168. EltTy = IsVolatile ? EltTy.withVolatile() : EltTy;
  169. asDerived().visitWithKind(FK, EltTy, nullptr, FieldOffset);
  170. appendStr("_AE");
  171. }
  172. void appendStr(StringRef Str) { Name += Str; }
  173. std::string getName(QualType QT, bool IsVolatile) {
  174. QT = IsVolatile ? QT.withVolatile() : QT;
  175. asDerived().visitStructFields(QT, CharUnits::Zero());
  176. return Name;
  177. }
  178. Derived &asDerived() { return static_cast<Derived &>(*this); }
  179. std::string Name;
  180. };
  181. template <class Derived>
  182. struct GenUnaryFuncName : StructVisitor<Derived>, GenFuncNameBase<Derived> {
  183. GenUnaryFuncName(StringRef Prefix, CharUnits DstAlignment, ASTContext &Ctx)
  184. : StructVisitor<Derived>(Ctx) {
  185. this->appendStr(Prefix);
  186. this->appendStr(llvm::to_string(DstAlignment.getQuantity()));
  187. }
  188. };
  189. // Helper function to create a null constant.
  190. static llvm::Constant *getNullForVariable(Address Addr) {
  191. llvm::Type *Ty = Addr.getElementType();
  192. return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(Ty));
  193. }
  194. template <bool IsMove>
  195. struct GenBinaryFuncName : CopyStructVisitor<GenBinaryFuncName<IsMove>, IsMove>,
  196. GenFuncNameBase<GenBinaryFuncName<IsMove>> {
  197. GenBinaryFuncName(StringRef Prefix, CharUnits DstAlignment,
  198. CharUnits SrcAlignment, ASTContext &Ctx)
  199. : CopyStructVisitor<GenBinaryFuncName<IsMove>, IsMove>(Ctx) {
  200. this->appendStr(Prefix);
  201. this->appendStr(llvm::to_string(DstAlignment.getQuantity()));
  202. this->appendStr("_" + llvm::to_string(SrcAlignment.getQuantity()));
  203. }
  204. void flushTrivialFields() {
  205. if (this->Start == this->End)
  206. return;
  207. this->appendStr("_t" + llvm::to_string(this->Start.getQuantity()) + "w" +
  208. llvm::to_string((this->End - this->Start).getQuantity()));
  209. this->Start = this->End = CharUnits::Zero();
  210. }
  211. void visitVolatileTrivial(QualType FT, const FieldDecl *FD,
  212. CharUnits CurStructOffset) {
  213. // Zero-length bit-fields don't need to be copied/assigned.
  214. if (FD && FD->isZeroLengthBitField(this->Ctx))
  215. return;
  216. // Because volatile fields can be bit-fields and are individually copied,
  217. // their offset and width are in bits.
  218. uint64_t OffsetInBits =
  219. this->Ctx.toBits(CurStructOffset) + this->getFieldOffsetInBits(FD);
  220. this->appendStr("_tv" + llvm::to_string(OffsetInBits) + "w" +
  221. llvm::to_string(getFieldSize(FD, FT, this->Ctx)));
  222. }
  223. };
  224. struct GenDefaultInitializeFuncName
  225. : GenUnaryFuncName<GenDefaultInitializeFuncName>,
  226. DefaultInitializedTypeVisitor<GenDefaultInitializeFuncName> {
  227. using Super = DefaultInitializedTypeVisitor<GenDefaultInitializeFuncName>;
  228. GenDefaultInitializeFuncName(CharUnits DstAlignment, ASTContext &Ctx)
  229. : GenUnaryFuncName<GenDefaultInitializeFuncName>("__default_constructor_",
  230. DstAlignment, Ctx) {}
  231. void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
  232. const FieldDecl *FD, CharUnits CurStructOffset) {
  233. if (const auto *AT = getContext().getAsArrayType(FT)) {
  234. visitArray(PDIK, AT, FT.isVolatileQualified(), FD, CurStructOffset);
  235. return;
  236. }
  237. Super::visitWithKind(PDIK, FT, FD, CurStructOffset);
  238. }
  239. };
  240. struct GenDestructorFuncName : GenUnaryFuncName<GenDestructorFuncName>,
  241. DestructedTypeVisitor<GenDestructorFuncName> {
  242. using Super = DestructedTypeVisitor<GenDestructorFuncName>;
  243. GenDestructorFuncName(const char *Prefix, CharUnits DstAlignment,
  244. ASTContext &Ctx)
  245. : GenUnaryFuncName<GenDestructorFuncName>(Prefix, DstAlignment, Ctx) {}
  246. void visitWithKind(QualType::DestructionKind DK, QualType FT,
  247. const FieldDecl *FD, CharUnits CurStructOffset) {
  248. if (const auto *AT = getContext().getAsArrayType(FT)) {
  249. visitArray(DK, AT, FT.isVolatileQualified(), FD, CurStructOffset);
  250. return;
  251. }
  252. Super::visitWithKind(DK, FT, FD, CurStructOffset);
  253. }
  254. };
  255. // Helper function that creates CGFunctionInfo for an N-ary special function.
  256. template <size_t N>
  257. static const CGFunctionInfo &getFunctionInfo(CodeGenModule &CGM,
  258. FunctionArgList &Args) {
  259. ASTContext &Ctx = CGM.getContext();
  260. llvm::SmallVector<ImplicitParamDecl *, N> Params;
  261. QualType ParamTy = Ctx.getPointerType(Ctx.VoidPtrTy);
  262. for (unsigned I = 0; I < N; ++I)
  263. Params.push_back(ImplicitParamDecl::Create(
  264. Ctx, nullptr, SourceLocation(), &Ctx.Idents.get(ValNameStr[I]), ParamTy,
  265. ImplicitParamDecl::Other));
  266. for (auto &P : Params)
  267. Args.push_back(P);
  268. return CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, Args);
  269. }
  270. template <size_t N, size_t... Ints>
  271. static std::array<Address, N> getParamAddrs(std::index_sequence<Ints...> IntSeq,
  272. std::array<CharUnits, N> Alignments,
  273. FunctionArgList Args,
  274. CodeGenFunction *CGF) {
  275. return std::array<Address, N>{{
  276. Address(CGF->Builder.CreateLoad(CGF->GetAddrOfLocalVar(Args[Ints])),
  277. Alignments[Ints])...}};
  278. }
  279. // Template classes that are used as bases for classes that emit special
  280. // functions.
  281. template <class Derived> struct GenFuncBase {
  282. template <size_t N>
  283. void visitStruct(QualType FT, const FieldDecl *FD, CharUnits CurStructOffset,
  284. std::array<Address, N> Addrs) {
  285. this->asDerived().callSpecialFunction(
  286. FT, CurStructOffset + asDerived().getFieldOffset(FD), Addrs);
  287. }
  288. template <class FieldKind, size_t N>
  289. void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
  290. const FieldDecl *FD, CharUnits CurStructOffset,
  291. std::array<Address, N> Addrs) {
  292. // Non-volatile trivial fields are copied when flushTrivialFields is called.
  293. if (!FK)
  294. return asDerived().visitTrivial(QualType(AT, 0), FD, CurStructOffset,
  295. Addrs);
  296. asDerived().flushTrivialFields(Addrs);
  297. CodeGenFunction &CGF = *this->CGF;
  298. ASTContext &Ctx = CGF.getContext();
  299. // Compute the end address.
  300. QualType BaseEltQT;
  301. std::array<Address, N> StartAddrs = Addrs;
  302. for (unsigned I = 0; I < N; ++I)
  303. StartAddrs[I] = getAddrWithOffset(Addrs[I], CurStructOffset, FD);
  304. Address DstAddr = StartAddrs[DstIdx];
  305. llvm::Value *NumElts = CGF.emitArrayLength(AT, BaseEltQT, DstAddr);
  306. unsigned BaseEltSize = Ctx.getTypeSizeInChars(BaseEltQT).getQuantity();
  307. llvm::Value *BaseEltSizeVal =
  308. llvm::ConstantInt::get(NumElts->getType(), BaseEltSize);
  309. llvm::Value *SizeInBytes =
  310. CGF.Builder.CreateNUWMul(BaseEltSizeVal, NumElts);
  311. Address BC = CGF.Builder.CreateElementBitCast(DstAddr, CGF.CGM.Int8Ty);
  312. llvm::Value *DstArrayEnd =
  313. CGF.Builder.CreateInBoundsGEP(CGF.Int8Ty, BC.getPointer(), SizeInBytes);
  314. DstArrayEnd = CGF.Builder.CreateBitCast(
  315. DstArrayEnd, CGF.CGM.Int8PtrPtrTy, "dstarray.end");
  316. llvm::BasicBlock *PreheaderBB = CGF.Builder.GetInsertBlock();
  317. // Create the header block and insert the phi instructions.
  318. llvm::BasicBlock *HeaderBB = CGF.createBasicBlock("loop.header");
  319. CGF.EmitBlock(HeaderBB);
  320. llvm::PHINode *PHIs[N];
  321. for (unsigned I = 0; I < N; ++I) {
  322. PHIs[I] = CGF.Builder.CreatePHI(CGF.CGM.Int8PtrPtrTy, 2, "addr.cur");
  323. PHIs[I]->addIncoming(StartAddrs[I].getPointer(), PreheaderBB);
  324. }
  325. // Create the exit and loop body blocks.
  326. llvm::BasicBlock *ExitBB = CGF.createBasicBlock("loop.exit");
  327. llvm::BasicBlock *LoopBB = CGF.createBasicBlock("loop.body");
  328. // Emit the comparison and conditional branch instruction that jumps to
  329. // either the exit or the loop body.
  330. llvm::Value *Done =
  331. CGF.Builder.CreateICmpEQ(PHIs[DstIdx], DstArrayEnd, "done");
  332. CGF.Builder.CreateCondBr(Done, ExitBB, LoopBB);
  333. // Visit the element of the array in the loop body.
  334. CGF.EmitBlock(LoopBB);
  335. QualType EltQT = AT->getElementType();
  336. CharUnits EltSize = Ctx.getTypeSizeInChars(EltQT);
  337. std::array<Address, N> NewAddrs = Addrs;
  338. for (unsigned I = 0; I < N; ++I)
  339. NewAddrs[I] = Address(
  340. PHIs[I], StartAddrs[I].getAlignment().alignmentAtOffset(EltSize));
  341. EltQT = IsVolatile ? EltQT.withVolatile() : EltQT;
  342. this->asDerived().visitWithKind(FK, EltQT, nullptr, CharUnits::Zero(),
  343. NewAddrs);
  344. LoopBB = CGF.Builder.GetInsertBlock();
  345. for (unsigned I = 0; I < N; ++I) {
  346. // Instrs to update the destination and source addresses.
  347. // Update phi instructions.
  348. NewAddrs[I] = getAddrWithOffset(NewAddrs[I], EltSize);
  349. PHIs[I]->addIncoming(NewAddrs[I].getPointer(), LoopBB);
  350. }
  351. // Insert an unconditional branch to the header block.
  352. CGF.Builder.CreateBr(HeaderBB);
  353. CGF.EmitBlock(ExitBB);
  354. }
  355. /// Return an address with the specified offset from the passed address.
  356. Address getAddrWithOffset(Address Addr, CharUnits Offset) {
  357. assert(Addr.isValid() && "invalid address");
  358. if (Offset.getQuantity() == 0)
  359. return Addr;
  360. Addr = CGF->Builder.CreateElementBitCast(Addr, CGF->CGM.Int8Ty);
  361. Addr = CGF->Builder.CreateConstInBoundsGEP(Addr, Offset.getQuantity());
  362. return CGF->Builder.CreateElementBitCast(Addr, CGF->CGM.Int8PtrTy);
  363. }
  364. Address getAddrWithOffset(Address Addr, CharUnits StructFieldOffset,
  365. const FieldDecl *FD) {
  366. return getAddrWithOffset(Addr, StructFieldOffset +
  367. asDerived().getFieldOffset(FD));
  368. }
  369. template <size_t N>
  370. llvm::Function *getFunction(StringRef FuncName, QualType QT,
  371. std::array<CharUnits, N> Alignments,
  372. CodeGenModule &CGM) {
  373. // If the special function already exists in the module, return it.
  374. if (llvm::Function *F = CGM.getModule().getFunction(FuncName)) {
  375. bool WrongType = false;
  376. if (!F->getReturnType()->isVoidTy())
  377. WrongType = true;
  378. else {
  379. for (const llvm::Argument &Arg : F->args())
  380. if (Arg.getType() != CGM.Int8PtrPtrTy)
  381. WrongType = true;
  382. }
  383. if (WrongType) {
  384. std::string FuncName = std::string(F->getName());
  385. SourceLocation Loc = QT->castAs<RecordType>()->getDecl()->getLocation();
  386. CGM.Error(Loc, "special function " + FuncName +
  387. " for non-trivial C struct has incorrect type");
  388. return nullptr;
  389. }
  390. return F;
  391. }
  392. ASTContext &Ctx = CGM.getContext();
  393. FunctionArgList Args;
  394. const CGFunctionInfo &FI = getFunctionInfo<N>(CGM, Args);
  395. llvm::FunctionType *FuncTy = CGM.getTypes().GetFunctionType(FI);
  396. llvm::Function *F =
  397. llvm::Function::Create(FuncTy, llvm::GlobalValue::LinkOnceODRLinkage,
  398. FuncName, &CGM.getModule());
  399. F->setVisibility(llvm::GlobalValue::HiddenVisibility);
  400. CGM.SetLLVMFunctionAttributes(GlobalDecl(), FI, F, /*IsThunk=*/false);
  401. CGM.SetLLVMFunctionAttributesForDefinition(nullptr, F);
  402. CodeGenFunction NewCGF(CGM);
  403. setCGF(&NewCGF);
  404. CGF->StartFunction(GlobalDecl(), Ctx.VoidTy, F, FI, Args);
  405. auto AL = ApplyDebugLocation::CreateArtificial(*CGF);
  406. std::array<Address, N> Addrs =
  407. getParamAddrs<N>(std::make_index_sequence<N>{}, Alignments, Args, CGF);
  408. asDerived().visitStructFields(QT, CharUnits::Zero(), Addrs);
  409. CGF->FinishFunction();
  410. return F;
  411. }
  412. template <size_t N>
  413. void callFunc(StringRef FuncName, QualType QT, std::array<Address, N> Addrs,
  414. CodeGenFunction &CallerCGF) {
  415. std::array<CharUnits, N> Alignments;
  416. llvm::Value *Ptrs[N];
  417. for (unsigned I = 0; I < N; ++I) {
  418. Alignments[I] = Addrs[I].getAlignment();
  419. Ptrs[I] = CallerCGF.Builder.CreateElementBitCast(
  420. Addrs[I], CallerCGF.CGM.Int8PtrTy).getPointer();
  421. }
  422. if (llvm::Function *F =
  423. getFunction(FuncName, QT, Alignments, CallerCGF.CGM))
  424. CallerCGF.EmitNounwindRuntimeCall(F, Ptrs);
  425. }
  426. Derived &asDerived() { return static_cast<Derived &>(*this); }
  427. void setCGF(CodeGenFunction *F) { CGF = F; }
  428. CodeGenFunction *CGF = nullptr;
  429. };
  430. template <class Derived, bool IsMove>
  431. struct GenBinaryFunc : CopyStructVisitor<Derived, IsMove>,
  432. GenFuncBase<Derived> {
  433. GenBinaryFunc(ASTContext &Ctx) : CopyStructVisitor<Derived, IsMove>(Ctx) {}
  434. void flushTrivialFields(std::array<Address, 2> Addrs) {
  435. CharUnits Size = this->End - this->Start;
  436. if (Size.getQuantity() == 0)
  437. return;
  438. Address DstAddr = this->getAddrWithOffset(Addrs[DstIdx], this->Start);
  439. Address SrcAddr = this->getAddrWithOffset(Addrs[SrcIdx], this->Start);
  440. // Emit memcpy.
  441. if (Size.getQuantity() >= 16 || !llvm::isPowerOf2_32(Size.getQuantity())) {
  442. llvm::Value *SizeVal =
  443. llvm::ConstantInt::get(this->CGF->SizeTy, Size.getQuantity());
  444. DstAddr =
  445. this->CGF->Builder.CreateElementBitCast(DstAddr, this->CGF->Int8Ty);
  446. SrcAddr =
  447. this->CGF->Builder.CreateElementBitCast(SrcAddr, this->CGF->Int8Ty);
  448. this->CGF->Builder.CreateMemCpy(DstAddr, SrcAddr, SizeVal, false);
  449. } else {
  450. llvm::Type *Ty = llvm::Type::getIntNTy(
  451. this->CGF->getLLVMContext(),
  452. Size.getQuantity() * this->CGF->getContext().getCharWidth());
  453. DstAddr = this->CGF->Builder.CreateElementBitCast(DstAddr, Ty);
  454. SrcAddr = this->CGF->Builder.CreateElementBitCast(SrcAddr, Ty);
  455. llvm::Value *SrcVal = this->CGF->Builder.CreateLoad(SrcAddr, false);
  456. this->CGF->Builder.CreateStore(SrcVal, DstAddr, false);
  457. }
  458. this->Start = this->End = CharUnits::Zero();
  459. }
  460. template <class... Ts>
  461. void visitVolatileTrivial(QualType FT, const FieldDecl *FD, CharUnits Offset,
  462. std::array<Address, 2> Addrs) {
  463. LValue DstLV, SrcLV;
  464. if (FD) {
  465. // No need to copy zero-length bit-fields.
  466. if (FD->isZeroLengthBitField(this->CGF->getContext()))
  467. return;
  468. QualType RT = QualType(FD->getParent()->getTypeForDecl(), 0);
  469. llvm::Type *Ty = this->CGF->ConvertType(RT);
  470. Address DstAddr = this->getAddrWithOffset(Addrs[DstIdx], Offset);
  471. LValue DstBase = this->CGF->MakeAddrLValue(
  472. this->CGF->Builder.CreateElementBitCast(DstAddr, Ty), FT);
  473. DstLV = this->CGF->EmitLValueForField(DstBase, FD);
  474. Address SrcAddr = this->getAddrWithOffset(Addrs[SrcIdx], Offset);
  475. LValue SrcBase = this->CGF->MakeAddrLValue(
  476. this->CGF->Builder.CreateElementBitCast(SrcAddr, Ty), FT);
  477. SrcLV = this->CGF->EmitLValueForField(SrcBase, FD);
  478. } else {
  479. llvm::Type *Ty = this->CGF->ConvertTypeForMem(FT);
  480. Address DstAddr =
  481. this->CGF->Builder.CreateElementBitCast(Addrs[DstIdx], Ty);
  482. Address SrcAddr =
  483. this->CGF->Builder.CreateElementBitCast(Addrs[SrcIdx], Ty);
  484. DstLV = this->CGF->MakeAddrLValue(DstAddr, FT);
  485. SrcLV = this->CGF->MakeAddrLValue(SrcAddr, FT);
  486. }
  487. RValue SrcVal = this->CGF->EmitLoadOfLValue(SrcLV, SourceLocation());
  488. this->CGF->EmitStoreThroughLValue(SrcVal, DstLV);
  489. }
  490. };
  491. // These classes that emit the special functions for a non-trivial struct.
  492. struct GenDestructor : StructVisitor<GenDestructor>,
  493. GenFuncBase<GenDestructor>,
  494. DestructedTypeVisitor<GenDestructor> {
  495. using Super = DestructedTypeVisitor<GenDestructor>;
  496. GenDestructor(ASTContext &Ctx) : StructVisitor<GenDestructor>(Ctx) {}
  497. void visitWithKind(QualType::DestructionKind DK, QualType FT,
  498. const FieldDecl *FD, CharUnits CurStructOffset,
  499. std::array<Address, 1> Addrs) {
  500. if (const auto *AT = getContext().getAsArrayType(FT)) {
  501. visitArray(DK, AT, FT.isVolatileQualified(), FD, CurStructOffset, Addrs);
  502. return;
  503. }
  504. Super::visitWithKind(DK, FT, FD, CurStructOffset, Addrs);
  505. }
  506. void visitARCStrong(QualType QT, const FieldDecl *FD,
  507. CharUnits CurStructOffset, std::array<Address, 1> Addrs) {
  508. CGF->destroyARCStrongImprecise(
  509. *CGF, getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
  510. }
  511. void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
  512. std::array<Address, 1> Addrs) {
  513. CGF->destroyARCWeak(
  514. *CGF, getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
  515. }
  516. void callSpecialFunction(QualType FT, CharUnits Offset,
  517. std::array<Address, 1> Addrs) {
  518. CGF->callCStructDestructor(
  519. CGF->MakeAddrLValue(getAddrWithOffset(Addrs[DstIdx], Offset), FT));
  520. }
  521. };
  522. struct GenDefaultInitialize
  523. : StructVisitor<GenDefaultInitialize>,
  524. GenFuncBase<GenDefaultInitialize>,
  525. DefaultInitializedTypeVisitor<GenDefaultInitialize> {
  526. using Super = DefaultInitializedTypeVisitor<GenDefaultInitialize>;
  527. typedef GenFuncBase<GenDefaultInitialize> GenFuncBaseTy;
  528. GenDefaultInitialize(ASTContext &Ctx)
  529. : StructVisitor<GenDefaultInitialize>(Ctx) {}
  530. void visitWithKind(QualType::PrimitiveDefaultInitializeKind PDIK, QualType FT,
  531. const FieldDecl *FD, CharUnits CurStructOffset,
  532. std::array<Address, 1> Addrs) {
  533. if (const auto *AT = getContext().getAsArrayType(FT)) {
  534. visitArray(PDIK, AT, FT.isVolatileQualified(), FD, CurStructOffset,
  535. Addrs);
  536. return;
  537. }
  538. Super::visitWithKind(PDIK, FT, FD, CurStructOffset, Addrs);
  539. }
  540. void visitARCStrong(QualType QT, const FieldDecl *FD,
  541. CharUnits CurStructOffset, std::array<Address, 1> Addrs) {
  542. CGF->EmitNullInitialization(
  543. getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
  544. }
  545. void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
  546. std::array<Address, 1> Addrs) {
  547. CGF->EmitNullInitialization(
  548. getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD), QT);
  549. }
  550. template <class FieldKind, size_t... Is>
  551. void visitArray(FieldKind FK, const ArrayType *AT, bool IsVolatile,
  552. const FieldDecl *FD, CharUnits CurStructOffset,
  553. std::array<Address, 1> Addrs) {
  554. if (!FK)
  555. return visitTrivial(QualType(AT, 0), FD, CurStructOffset, Addrs);
  556. ASTContext &Ctx = getContext();
  557. CharUnits Size = Ctx.getTypeSizeInChars(QualType(AT, 0));
  558. QualType EltTy = Ctx.getBaseElementType(QualType(AT, 0));
  559. if (Size < CharUnits::fromQuantity(16) || EltTy->getAs<RecordType>()) {
  560. GenFuncBaseTy::visitArray(FK, AT, IsVolatile, FD, CurStructOffset, Addrs);
  561. return;
  562. }
  563. llvm::Constant *SizeVal = CGF->Builder.getInt64(Size.getQuantity());
  564. Address DstAddr = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  565. Address Loc = CGF->Builder.CreateElementBitCast(DstAddr, CGF->Int8Ty);
  566. CGF->Builder.CreateMemSet(Loc, CGF->Builder.getInt8(0), SizeVal,
  567. IsVolatile);
  568. }
  569. void callSpecialFunction(QualType FT, CharUnits Offset,
  570. std::array<Address, 1> Addrs) {
  571. CGF->callCStructDefaultConstructor(
  572. CGF->MakeAddrLValue(getAddrWithOffset(Addrs[DstIdx], Offset), FT));
  573. }
  574. };
  575. struct GenCopyConstructor : GenBinaryFunc<GenCopyConstructor, false> {
  576. GenCopyConstructor(ASTContext &Ctx)
  577. : GenBinaryFunc<GenCopyConstructor, false>(Ctx) {}
  578. void visitARCStrong(QualType QT, const FieldDecl *FD,
  579. CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
  580. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  581. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  582. llvm::Value *SrcVal = CGF->EmitLoadOfScalar(
  583. Addrs[SrcIdx], QT.isVolatileQualified(), QT, SourceLocation());
  584. llvm::Value *Val = CGF->EmitARCRetain(QT, SrcVal);
  585. CGF->EmitStoreOfScalar(Val, CGF->MakeAddrLValue(Addrs[DstIdx], QT), true);
  586. }
  587. void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
  588. std::array<Address, 2> Addrs) {
  589. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  590. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  591. CGF->EmitARCCopyWeak(Addrs[DstIdx], Addrs[SrcIdx]);
  592. }
  593. void callSpecialFunction(QualType FT, CharUnits Offset,
  594. std::array<Address, 2> Addrs) {
  595. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
  596. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
  597. CGF->callCStructCopyConstructor(CGF->MakeAddrLValue(Addrs[DstIdx], FT),
  598. CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
  599. }
  600. };
  601. struct GenMoveConstructor : GenBinaryFunc<GenMoveConstructor, true> {
  602. GenMoveConstructor(ASTContext &Ctx)
  603. : GenBinaryFunc<GenMoveConstructor, true>(Ctx) {}
  604. void visitARCStrong(QualType QT, const FieldDecl *FD,
  605. CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
  606. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  607. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  608. LValue SrcLV = CGF->MakeAddrLValue(Addrs[SrcIdx], QT);
  609. llvm::Value *SrcVal =
  610. CGF->EmitLoadOfLValue(SrcLV, SourceLocation()).getScalarVal();
  611. CGF->EmitStoreOfScalar(getNullForVariable(SrcLV.getAddress(*CGF)), SrcLV);
  612. CGF->EmitStoreOfScalar(SrcVal, CGF->MakeAddrLValue(Addrs[DstIdx], QT),
  613. /* isInitialization */ true);
  614. }
  615. void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
  616. std::array<Address, 2> Addrs) {
  617. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  618. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  619. CGF->EmitARCMoveWeak(Addrs[DstIdx], Addrs[SrcIdx]);
  620. }
  621. void callSpecialFunction(QualType FT, CharUnits Offset,
  622. std::array<Address, 2> Addrs) {
  623. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
  624. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
  625. CGF->callCStructMoveConstructor(CGF->MakeAddrLValue(Addrs[DstIdx], FT),
  626. CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
  627. }
  628. };
  629. struct GenCopyAssignment : GenBinaryFunc<GenCopyAssignment, false> {
  630. GenCopyAssignment(ASTContext &Ctx)
  631. : GenBinaryFunc<GenCopyAssignment, false>(Ctx) {}
  632. void visitARCStrong(QualType QT, const FieldDecl *FD,
  633. CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
  634. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  635. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  636. llvm::Value *SrcVal = CGF->EmitLoadOfScalar(
  637. Addrs[SrcIdx], QT.isVolatileQualified(), QT, SourceLocation());
  638. CGF->EmitARCStoreStrong(CGF->MakeAddrLValue(Addrs[DstIdx], QT), SrcVal,
  639. false);
  640. }
  641. void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
  642. std::array<Address, 2> Addrs) {
  643. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  644. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  645. CGF->emitARCCopyAssignWeak(QT, Addrs[DstIdx], Addrs[SrcIdx]);
  646. }
  647. void callSpecialFunction(QualType FT, CharUnits Offset,
  648. std::array<Address, 2> Addrs) {
  649. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
  650. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
  651. CGF->callCStructCopyAssignmentOperator(
  652. CGF->MakeAddrLValue(Addrs[DstIdx], FT),
  653. CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
  654. }
  655. };
  656. struct GenMoveAssignment : GenBinaryFunc<GenMoveAssignment, true> {
  657. GenMoveAssignment(ASTContext &Ctx)
  658. : GenBinaryFunc<GenMoveAssignment, true>(Ctx) {}
  659. void visitARCStrong(QualType QT, const FieldDecl *FD,
  660. CharUnits CurStructOffset, std::array<Address, 2> Addrs) {
  661. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  662. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  663. LValue SrcLV = CGF->MakeAddrLValue(Addrs[SrcIdx], QT);
  664. llvm::Value *SrcVal =
  665. CGF->EmitLoadOfLValue(SrcLV, SourceLocation()).getScalarVal();
  666. CGF->EmitStoreOfScalar(getNullForVariable(SrcLV.getAddress(*CGF)), SrcLV);
  667. LValue DstLV = CGF->MakeAddrLValue(Addrs[DstIdx], QT);
  668. llvm::Value *DstVal =
  669. CGF->EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal();
  670. CGF->EmitStoreOfScalar(SrcVal, DstLV);
  671. CGF->EmitARCRelease(DstVal, ARCImpreciseLifetime);
  672. }
  673. void visitARCWeak(QualType QT, const FieldDecl *FD, CharUnits CurStructOffset,
  674. std::array<Address, 2> Addrs) {
  675. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], CurStructOffset, FD);
  676. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], CurStructOffset, FD);
  677. CGF->emitARCMoveAssignWeak(QT, Addrs[DstIdx], Addrs[SrcIdx]);
  678. }
  679. void callSpecialFunction(QualType FT, CharUnits Offset,
  680. std::array<Address, 2> Addrs) {
  681. Addrs[DstIdx] = getAddrWithOffset(Addrs[DstIdx], Offset);
  682. Addrs[SrcIdx] = getAddrWithOffset(Addrs[SrcIdx], Offset);
  683. CGF->callCStructMoveAssignmentOperator(
  684. CGF->MakeAddrLValue(Addrs[DstIdx], FT),
  685. CGF->MakeAddrLValue(Addrs[SrcIdx], FT));
  686. }
  687. };
  688. } // namespace
  689. void CodeGenFunction::destroyNonTrivialCStruct(CodeGenFunction &CGF,
  690. Address Addr, QualType Type) {
  691. CGF.callCStructDestructor(CGF.MakeAddrLValue(Addr, Type));
  692. }
  693. // Default-initialize a variable that is a non-trivial struct or an array of
  694. // such structure.
  695. void CodeGenFunction::defaultInitNonTrivialCStructVar(LValue Dst) {
  696. GenDefaultInitialize Gen(getContext());
  697. Address DstPtr =
  698. Builder.CreateElementBitCast(Dst.getAddress(*this), CGM.Int8PtrTy);
  699. Gen.setCGF(this);
  700. QualType QT = Dst.getType();
  701. QT = Dst.isVolatile() ? QT.withVolatile() : QT;
  702. Gen.visit(QT, nullptr, CharUnits::Zero(), std::array<Address, 1>({{DstPtr}}));
  703. }
  704. template <class G, size_t N>
  705. static void callSpecialFunction(G &&Gen, StringRef FuncName, QualType QT,
  706. bool IsVolatile, CodeGenFunction &CGF,
  707. std::array<Address, N> Addrs) {
  708. auto SetArtificialLoc = ApplyDebugLocation::CreateArtificial(CGF);
  709. for (unsigned I = 0; I < N; ++I)
  710. Addrs[I] = CGF.Builder.CreateElementBitCast(Addrs[I], CGF.CGM.Int8PtrTy);
  711. QT = IsVolatile ? QT.withVolatile() : QT;
  712. Gen.callFunc(FuncName, QT, Addrs, CGF);
  713. }
  714. template <class G, size_t N>
  715. static llvm::Function *
  716. getSpecialFunction(G &&Gen, StringRef FuncName, QualType QT, bool IsVolatile,
  717. std::array<CharUnits, N> Alignments, CodeGenModule &CGM) {
  718. QT = IsVolatile ? QT.withVolatile() : QT;
  719. // The following call requires an array of addresses as arguments, but doesn't
  720. // actually use them (it overwrites them with the addresses of the arguments
  721. // of the created function).
  722. return Gen.getFunction(FuncName, QT, Alignments, CGM);
  723. }
  724. // Functions to emit calls to the special functions of a non-trivial C struct.
  725. void CodeGenFunction::callCStructDefaultConstructor(LValue Dst) {
  726. bool IsVolatile = Dst.isVolatile();
  727. Address DstPtr = Dst.getAddress(*this);
  728. QualType QT = Dst.getType();
  729. GenDefaultInitializeFuncName GenName(DstPtr.getAlignment(), getContext());
  730. std::string FuncName = GenName.getName(QT, IsVolatile);
  731. callSpecialFunction(GenDefaultInitialize(getContext()), FuncName, QT,
  732. IsVolatile, *this, std::array<Address, 1>({{DstPtr}}));
  733. }
  734. std::string CodeGenFunction::getNonTrivialCopyConstructorStr(
  735. QualType QT, CharUnits Alignment, bool IsVolatile, ASTContext &Ctx) {
  736. GenBinaryFuncName<false> GenName("", Alignment, Alignment, Ctx);
  737. return GenName.getName(QT, IsVolatile);
  738. }
  739. std::string CodeGenFunction::getNonTrivialDestructorStr(QualType QT,
  740. CharUnits Alignment,
  741. bool IsVolatile,
  742. ASTContext &Ctx) {
  743. GenDestructorFuncName GenName("", Alignment, Ctx);
  744. return GenName.getName(QT, IsVolatile);
  745. }
  746. void CodeGenFunction::callCStructDestructor(LValue Dst) {
  747. bool IsVolatile = Dst.isVolatile();
  748. Address DstPtr = Dst.getAddress(*this);
  749. QualType QT = Dst.getType();
  750. GenDestructorFuncName GenName("__destructor_", DstPtr.getAlignment(),
  751. getContext());
  752. std::string FuncName = GenName.getName(QT, IsVolatile);
  753. callSpecialFunction(GenDestructor(getContext()), FuncName, QT, IsVolatile,
  754. *this, std::array<Address, 1>({{DstPtr}}));
  755. }
  756. void CodeGenFunction::callCStructCopyConstructor(LValue Dst, LValue Src) {
  757. bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
  758. Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
  759. QualType QT = Dst.getType();
  760. GenBinaryFuncName<false> GenName("__copy_constructor_", DstPtr.getAlignment(),
  761. SrcPtr.getAlignment(), getContext());
  762. std::string FuncName = GenName.getName(QT, IsVolatile);
  763. callSpecialFunction(GenCopyConstructor(getContext()), FuncName, QT,
  764. IsVolatile, *this,
  765. std::array<Address, 2>({{DstPtr, SrcPtr}}));
  766. }
  767. void CodeGenFunction::callCStructCopyAssignmentOperator(LValue Dst, LValue Src
  768. ) {
  769. bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
  770. Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
  771. QualType QT = Dst.getType();
  772. GenBinaryFuncName<false> GenName("__copy_assignment_", DstPtr.getAlignment(),
  773. SrcPtr.getAlignment(), getContext());
  774. std::string FuncName = GenName.getName(QT, IsVolatile);
  775. callSpecialFunction(GenCopyAssignment(getContext()), FuncName, QT, IsVolatile,
  776. *this, std::array<Address, 2>({{DstPtr, SrcPtr}}));
  777. }
  778. void CodeGenFunction::callCStructMoveConstructor(LValue Dst, LValue Src) {
  779. bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
  780. Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
  781. QualType QT = Dst.getType();
  782. GenBinaryFuncName<true> GenName("__move_constructor_", DstPtr.getAlignment(),
  783. SrcPtr.getAlignment(), getContext());
  784. std::string FuncName = GenName.getName(QT, IsVolatile);
  785. callSpecialFunction(GenMoveConstructor(getContext()), FuncName, QT,
  786. IsVolatile, *this,
  787. std::array<Address, 2>({{DstPtr, SrcPtr}}));
  788. }
  789. void CodeGenFunction::callCStructMoveAssignmentOperator(LValue Dst, LValue Src
  790. ) {
  791. bool IsVolatile = Dst.isVolatile() || Src.isVolatile();
  792. Address DstPtr = Dst.getAddress(*this), SrcPtr = Src.getAddress(*this);
  793. QualType QT = Dst.getType();
  794. GenBinaryFuncName<true> GenName("__move_assignment_", DstPtr.getAlignment(),
  795. SrcPtr.getAlignment(), getContext());
  796. std::string FuncName = GenName.getName(QT, IsVolatile);
  797. callSpecialFunction(GenMoveAssignment(getContext()), FuncName, QT, IsVolatile,
  798. *this, std::array<Address, 2>({{DstPtr, SrcPtr}}));
  799. }
  800. llvm::Function *clang::CodeGen::getNonTrivialCStructDefaultConstructor(
  801. CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT) {
  802. ASTContext &Ctx = CGM.getContext();
  803. GenDefaultInitializeFuncName GenName(DstAlignment, Ctx);
  804. std::string FuncName = GenName.getName(QT, IsVolatile);
  805. return getSpecialFunction(GenDefaultInitialize(Ctx), FuncName, QT, IsVolatile,
  806. std::array<CharUnits, 1>({{DstAlignment}}), CGM);
  807. }
  808. llvm::Function *clang::CodeGen::getNonTrivialCStructCopyConstructor(
  809. CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
  810. bool IsVolatile, QualType QT) {
  811. ASTContext &Ctx = CGM.getContext();
  812. GenBinaryFuncName<false> GenName("__copy_constructor_", DstAlignment,
  813. SrcAlignment, Ctx);
  814. std::string FuncName = GenName.getName(QT, IsVolatile);
  815. return getSpecialFunction(
  816. GenCopyConstructor(Ctx), FuncName, QT, IsVolatile,
  817. std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
  818. }
  819. llvm::Function *clang::CodeGen::getNonTrivialCStructMoveConstructor(
  820. CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
  821. bool IsVolatile, QualType QT) {
  822. ASTContext &Ctx = CGM.getContext();
  823. GenBinaryFuncName<true> GenName("__move_constructor_", DstAlignment,
  824. SrcAlignment, Ctx);
  825. std::string FuncName = GenName.getName(QT, IsVolatile);
  826. return getSpecialFunction(
  827. GenMoveConstructor(Ctx), FuncName, QT, IsVolatile,
  828. std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
  829. }
  830. llvm::Function *clang::CodeGen::getNonTrivialCStructCopyAssignmentOperator(
  831. CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
  832. bool IsVolatile, QualType QT) {
  833. ASTContext &Ctx = CGM.getContext();
  834. GenBinaryFuncName<false> GenName("__copy_assignment_", DstAlignment,
  835. SrcAlignment, Ctx);
  836. std::string FuncName = GenName.getName(QT, IsVolatile);
  837. return getSpecialFunction(
  838. GenCopyAssignment(Ctx), FuncName, QT, IsVolatile,
  839. std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
  840. }
  841. llvm::Function *clang::CodeGen::getNonTrivialCStructMoveAssignmentOperator(
  842. CodeGenModule &CGM, CharUnits DstAlignment, CharUnits SrcAlignment,
  843. bool IsVolatile, QualType QT) {
  844. ASTContext &Ctx = CGM.getContext();
  845. GenBinaryFuncName<true> GenName("__move_assignment_", DstAlignment,
  846. SrcAlignment, Ctx);
  847. std::string FuncName = GenName.getName(QT, IsVolatile);
  848. return getSpecialFunction(
  849. GenMoveAssignment(Ctx), FuncName, QT, IsVolatile,
  850. std::array<CharUnits, 2>({{DstAlignment, SrcAlignment}}), CGM);
  851. }
  852. llvm::Function *clang::CodeGen::getNonTrivialCStructDestructor(
  853. CodeGenModule &CGM, CharUnits DstAlignment, bool IsVolatile, QualType QT) {
  854. ASTContext &Ctx = CGM.getContext();
  855. GenDestructorFuncName GenName("__destructor_", DstAlignment, Ctx);
  856. std::string FuncName = GenName.getName(QT, IsVolatile);
  857. return getSpecialFunction(GenDestructor(Ctx), FuncName, QT, IsVolatile,
  858. std::array<CharUnits, 1>({{DstAlignment}}), CGM);
  859. }