clang  3.8.0
CGDecl.cpp
Go to the documentation of this file.
1 //===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This contains code to emit Decl nodes as LLVM code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CodeGenFunction.h"
15 #include "CGBlocks.h"
16 #include "CGCleanup.h"
17 #include "CGDebugInfo.h"
18 #include "CGOpenCLRuntime.h"
19 #include "CodeGenModule.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/CharUnits.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclObjC.h"
25 #include "clang/Basic/TargetInfo.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/GlobalVariable.h"
30 #include "llvm/IR/Intrinsics.h"
31 #include "llvm/IR/Type.h"
32 using namespace clang;
33 using namespace CodeGen;
34 
35 
37  switch (D.getKind()) {
38  case Decl::BuiltinTemplate:
39  case Decl::TranslationUnit:
40  case Decl::ExternCContext:
41  case Decl::Namespace:
42  case Decl::UnresolvedUsingTypename:
43  case Decl::ClassTemplateSpecialization:
44  case Decl::ClassTemplatePartialSpecialization:
45  case Decl::VarTemplateSpecialization:
46  case Decl::VarTemplatePartialSpecialization:
47  case Decl::TemplateTypeParm:
48  case Decl::UnresolvedUsingValue:
49  case Decl::NonTypeTemplateParm:
50  case Decl::CXXMethod:
51  case Decl::CXXConstructor:
52  case Decl::CXXDestructor:
53  case Decl::CXXConversion:
54  case Decl::Field:
55  case Decl::MSProperty:
56  case Decl::IndirectField:
57  case Decl::ObjCIvar:
58  case Decl::ObjCAtDefsField:
59  case Decl::ParmVar:
60  case Decl::ImplicitParam:
61  case Decl::ClassTemplate:
62  case Decl::VarTemplate:
63  case Decl::FunctionTemplate:
64  case Decl::TypeAliasTemplate:
65  case Decl::TemplateTemplateParm:
66  case Decl::ObjCMethod:
67  case Decl::ObjCCategory:
68  case Decl::ObjCProtocol:
69  case Decl::ObjCInterface:
70  case Decl::ObjCCategoryImpl:
71  case Decl::ObjCImplementation:
72  case Decl::ObjCProperty:
73  case Decl::ObjCCompatibleAlias:
74  case Decl::AccessSpec:
75  case Decl::LinkageSpec:
76  case Decl::ObjCPropertyImpl:
77  case Decl::FileScopeAsm:
78  case Decl::Friend:
79  case Decl::FriendTemplate:
80  case Decl::Block:
81  case Decl::Captured:
82  case Decl::ClassScopeFunctionSpecialization:
83  case Decl::UsingShadow:
84  case Decl::ObjCTypeParam:
85  llvm_unreachable("Declaration should not be in declstmts!");
86  case Decl::Function: // void X();
87  case Decl::Record: // struct/union/class X;
88  case Decl::Enum: // enum X;
89  case Decl::EnumConstant: // enum ? { X = ? }
90  case Decl::CXXRecord: // struct/union/class X; [C++]
91  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
92  case Decl::Label: // __label__ x;
93  case Decl::Import:
94  case Decl::OMPThreadPrivate:
95  case Decl::Empty:
96  // None of these decls require codegen support.
97  return;
98 
99  case Decl::NamespaceAlias:
100  if (CGDebugInfo *DI = getDebugInfo())
101  DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
102  return;
103  case Decl::Using: // using X; [C++]
104  if (CGDebugInfo *DI = getDebugInfo())
105  DI->EmitUsingDecl(cast<UsingDecl>(D));
106  return;
107  case Decl::UsingDirective: // using namespace X; [C++]
108  if (CGDebugInfo *DI = getDebugInfo())
109  DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
110  return;
111  case Decl::Var: {
112  const VarDecl &VD = cast<VarDecl>(D);
113  assert(VD.isLocalVarDecl() &&
114  "Should not see file-scope variables inside a function!");
115  return EmitVarDecl(VD);
116  }
117 
118  case Decl::Typedef: // typedef int X;
119  case Decl::TypeAlias: { // using X = int; [C++0x]
120  const TypedefNameDecl &TD = cast<TypedefNameDecl>(D);
121  QualType Ty = TD.getUnderlyingType();
122 
123  if (Ty->isVariablyModifiedType())
125  }
126  }
127 }
128 
129 /// EmitVarDecl - This method handles emission of any variable declaration
130 /// inside a function, including static vars etc.
132  if (D.isStaticLocal()) {
133  llvm::GlobalValue::LinkageTypes Linkage =
134  CGM.getLLVMLinkageVarDefinition(&D, /*isConstant=*/false);
135 
136  // FIXME: We need to force the emission/use of a guard variable for
137  // some variables even if we can constant-evaluate them because
138  // we can't guarantee every translation unit will constant-evaluate them.
139 
140  return EmitStaticVarDecl(D, Linkage);
141  }
142 
143  if (D.hasExternalStorage())
144  // Don't emit it now, allow it to be emitted lazily on its first use.
145  return;
146 
149 
150  assert(D.hasLocalStorage());
151  return EmitAutoVarDecl(D);
152 }
153 
154 static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D) {
155  if (CGM.getLangOpts().CPlusPlus)
156  return CGM.getMangledName(&D).str();
157 
158  // If this isn't C++, we don't need a mangled name, just a pretty one.
159  assert(!D.isExternallyVisible() && "name shouldn't matter");
160  std::string ContextName;
161  const DeclContext *DC = D.getDeclContext();
162  if (auto *CD = dyn_cast<CapturedDecl>(DC))
163  DC = cast<DeclContext>(CD->getNonClosureContext());
164  if (const auto *FD = dyn_cast<FunctionDecl>(DC))
165  ContextName = CGM.getMangledName(FD);
166  else if (const auto *BD = dyn_cast<BlockDecl>(DC))
167  ContextName = CGM.getBlockMangledName(GlobalDecl(), BD);
168  else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC))
169  ContextName = OMD->getSelector().getAsString();
170  else
171  llvm_unreachable("Unknown context for static var decl");
172 
173  ContextName += "." + D.getNameAsString();
174  return ContextName;
175 }
176 
178  const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) {
179  // In general, we don't always emit static var decls once before we reference
180  // them. It is possible to reference them before emitting the function that
181  // contains them, and it is possible to emit the containing function multiple
182  // times.
183  if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D])
184  return ExistingGV;
185 
186  QualType Ty = D.getType();
187  assert(Ty->isConstantSizeType() && "VLAs can't be static");
188 
189  // Use the label if the variable is renamed with the asm-label extension.
190  std::string Name;
191  if (D.hasAttr<AsmLabelAttr>())
192  Name = getMangledName(&D);
193  else
194  Name = getStaticDeclName(*this, D);
195 
197  unsigned AddrSpace =
198  GetGlobalVarAddressSpace(&D, getContext().getTargetAddressSpace(Ty));
199 
200  // Local address space cannot have an initializer.
201  llvm::Constant *Init = nullptr;
203  Init = EmitNullConstant(Ty);
204  else
205  Init = llvm::UndefValue::get(LTy);
206 
207  llvm::GlobalVariable *GV =
208  new llvm::GlobalVariable(getModule(), LTy,
210  Init, Name, nullptr,
211  llvm::GlobalVariable::NotThreadLocal,
212  AddrSpace);
213  GV->setAlignment(getContext().getDeclAlign(&D).getQuantity());
214  setGlobalVisibility(GV, &D);
215 
216  if (supportsCOMDAT() && GV->isWeakForLinker())
217  GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
218 
219  if (D.getTLSKind())
220  setTLSMode(GV, D);
221 
222  if (D.isExternallyVisible()) {
223  if (D.hasAttr<DLLImportAttr>())
224  GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
225  else if (D.hasAttr<DLLExportAttr>())
226  GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
227  }
228 
229  // Make sure the result is of the correct type.
230  unsigned ExpectedAddrSpace = getContext().getTargetAddressSpace(Ty);
231  llvm::Constant *Addr = GV;
232  if (AddrSpace != ExpectedAddrSpace) {
233  llvm::PointerType *PTy = llvm::PointerType::get(LTy, ExpectedAddrSpace);
234  Addr = llvm::ConstantExpr::getAddrSpaceCast(GV, PTy);
235  }
236 
237  setStaticLocalDeclAddress(&D, Addr);
238 
239  // Ensure that the static local gets initialized by making sure the parent
240  // function gets emitted eventually.
241  const Decl *DC = cast<Decl>(D.getDeclContext());
242 
243  // We can't name blocks or captured statements directly, so try to emit their
244  // parents.
245  if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) {
246  DC = DC->getNonClosureContext();
247  // FIXME: Ensure that global blocks get emitted.
248  if (!DC)
249  return Addr;
250  }
251 
252  GlobalDecl GD;
253  if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
254  GD = GlobalDecl(CD, Ctor_Base);
255  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
256  GD = GlobalDecl(DD, Dtor_Base);
257  else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
258  GD = GlobalDecl(FD);
259  else {
260  // Don't do anything for Obj-C method decls or global closures. We should
261  // never defer them.
262  assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl");
263  }
264  if (GD.getDecl())
265  (void)GetAddrOfGlobal(GD);
266 
267  return Addr;
268 }
269 
270 /// hasNontrivialDestruction - Determine whether a type's destruction is
271 /// non-trivial. If so, and the variable uses static initialization, we must
272 /// register its destructor to run on exit.
275  return RD && !RD->hasTrivialDestructor();
276 }
277 
278 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
279 /// global variable that has already been created for it. If the initializer
280 /// has a different type than GV does, this may free GV and return a different
281 /// one. Otherwise it just returns GV.
282 llvm::GlobalVariable *
284  llvm::GlobalVariable *GV) {
285  llvm::Constant *Init = CGM.EmitConstantInit(D, this);
286 
287  // If constant emission failed, then this should be a C++ static
288  // initializer.
289  if (!Init) {
290  if (!getLangOpts().CPlusPlus)
291  CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
292  else if (Builder.GetInsertBlock()) {
293  // Since we have a static initializer, this global variable can't
294  // be constant.
295  GV->setConstant(false);
296 
297  EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
298  }
299  return GV;
300  }
301 
302  // The initializer may differ in type from the global. Rewrite
303  // the global to match the initializer. (We have to do this
304  // because some types, like unions, can't be completely represented
305  // in the LLVM type system.)
306  if (GV->getType()->getElementType() != Init->getType()) {
307  llvm::GlobalVariable *OldGV = GV;
308 
309  GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(),
310  OldGV->isConstant(),
311  OldGV->getLinkage(), Init, "",
312  /*InsertBefore*/ OldGV,
313  OldGV->getThreadLocalMode(),
315  GV->setVisibility(OldGV->getVisibility());
316  GV->setComdat(OldGV->getComdat());
317 
318  // Steal the name of the old global
319  GV->takeName(OldGV);
320 
321  // Replace all uses of the old global with the new global
322  llvm::Constant *NewPtrForOldDecl =
323  llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
324  OldGV->replaceAllUsesWith(NewPtrForOldDecl);
325 
326  // Erase the old global, since it is no longer used.
327  OldGV->eraseFromParent();
328  }
329 
330  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
331  GV->setInitializer(Init);
332 
334  // We have a constant initializer, but a nontrivial destructor. We still
335  // need to perform a guarded "initialization" in order to register the
336  // destructor.
337  EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
338  }
339 
340  return GV;
341 }
342 
344  llvm::GlobalValue::LinkageTypes Linkage) {
345  // Check to see if we already have a global variable for this
346  // declaration. This can happen when double-emitting function
347  // bodies, e.g. with complete and base constructors.
348  llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
349  CharUnits alignment = getContext().getDeclAlign(&D);
350 
351  // Store into LocalDeclMap before generating initializer to handle
352  // circular references.
353  setAddrOfLocalVar(&D, Address(addr, alignment));
354 
355  // We can't have a VLA here, but we can have a pointer to a VLA,
356  // even though that doesn't really make any sense.
357  // Make sure to evaluate VLA bounds now so that we have them for later.
358  if (D.getType()->isVariablyModifiedType())
360 
361  // Save the type in case adding the initializer forces a type change.
362  llvm::Type *expectedType = addr->getType();
363 
364  llvm::GlobalVariable *var =
365  cast<llvm::GlobalVariable>(addr->stripPointerCasts());
366  // If this value has an initializer, emit it.
367  if (D.getInit())
368  var = AddInitializerToStaticVarDecl(D, var);
369 
370  var->setAlignment(alignment.getQuantity());
371 
372  if (D.hasAttr<AnnotateAttr>())
373  CGM.AddGlobalAnnotations(&D, var);
374 
375  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
376  var->setSection(SA->getName());
377 
378  if (D.hasAttr<UsedAttr>())
379  CGM.addUsedGlobal(var);
380 
381  // We may have to cast the constant because of the initializer
382  // mismatch above.
383  //
384  // FIXME: It is really dangerous to store this in the map; if anyone
385  // RAUW's the GV uses of this constant will be invalid.
386  llvm::Constant *castedAddr =
387  llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
388  if (var != castedAddr)
389  LocalDeclMap.find(&D)->second = Address(castedAddr, alignment);
390  CGM.setStaticLocalDeclAddress(&D, castedAddr);
391 
393 
394  // Emit global variable debug descriptor for static vars.
395  CGDebugInfo *DI = getDebugInfo();
396  if (DI &&
398  DI->setLocation(D.getLocation());
399  DI->EmitGlobalVariable(var, &D);
400  }
401 }
402 
403 namespace {
404  struct DestroyObject final : EHScopeStack::Cleanup {
405  DestroyObject(Address addr, QualType type,
406  CodeGenFunction::Destroyer *destroyer,
407  bool useEHCleanupForArray)
408  : addr(addr), type(type), destroyer(destroyer),
409  useEHCleanupForArray(useEHCleanupForArray) {}
410 
411  Address addr;
412  QualType type;
413  CodeGenFunction::Destroyer *destroyer;
414  bool useEHCleanupForArray;
415 
416  void Emit(CodeGenFunction &CGF, Flags flags) override {
417  // Don't use an EH cleanup recursively from an EH cleanup.
418  bool useEHCleanupForArray =
419  flags.isForNormalCleanup() && this->useEHCleanupForArray;
420 
421  CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
422  }
423  };
424 
425  struct DestroyNRVOVariable final : EHScopeStack::Cleanup {
426  DestroyNRVOVariable(Address addr,
427  const CXXDestructorDecl *Dtor,
428  llvm::Value *NRVOFlag)
429  : Dtor(Dtor), NRVOFlag(NRVOFlag), Loc(addr) {}
430 
431  const CXXDestructorDecl *Dtor;
432  llvm::Value *NRVOFlag;
433  Address Loc;
434 
435  void Emit(CodeGenFunction &CGF, Flags flags) override {
436  // Along the exceptions path we always execute the dtor.
437  bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
438 
439  llvm::BasicBlock *SkipDtorBB = nullptr;
440  if (NRVO) {
441  // If we exited via NRVO, we skip the destructor call.
442  llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
443  SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
444  llvm::Value *DidNRVO =
445  CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
446  CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
447  CGF.EmitBlock(RunDtorBB);
448  }
449 
451  /*ForVirtualBase=*/false,
452  /*Delegating=*/false,
453  Loc);
454 
455  if (NRVO) CGF.EmitBlock(SkipDtorBB);
456  }
457  };
458 
459  struct CallStackRestore final : EHScopeStack::Cleanup {
460  Address Stack;
461  CallStackRestore(Address Stack) : Stack(Stack) {}
462  void Emit(CodeGenFunction &CGF, Flags flags) override {
464  llvm::Value *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
465  CGF.Builder.CreateCall(F, V);
466  }
467  };
468 
469  struct ExtendGCLifetime final : EHScopeStack::Cleanup {
470  const VarDecl &Var;
471  ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
472 
473  void Emit(CodeGenFunction &CGF, Flags flags) override {
474  // Compute the address of the local variable, in case it's a
475  // byref or something.
476  DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
477  Var.getType(), VK_LValue, SourceLocation());
478  llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
479  SourceLocation());
480  CGF.EmitExtendGCLifetime(value);
481  }
482  };
483 
484  struct CallCleanupFunction final : EHScopeStack::Cleanup {
485  llvm::Constant *CleanupFn;
486  const CGFunctionInfo &FnInfo;
487  const VarDecl &Var;
488 
489  CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
490  const VarDecl *Var)
491  : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
492 
493  void Emit(CodeGenFunction &CGF, Flags flags) override {
494  DeclRefExpr DRE(const_cast<VarDecl*>(&Var), false,
495  Var.getType(), VK_LValue, SourceLocation());
496  // Compute the address of the local variable, in case it's a byref
497  // or something.
498  llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer();
499 
500  // In some cases, the type of the function argument will be different from
501  // the type of the pointer. An example of this is
502  // void f(void* arg);
503  // __attribute__((cleanup(f))) void *g;
504  //
505  // To fix this we insert a bitcast here.
506  QualType ArgTy = FnInfo.arg_begin()->type;
507  llvm::Value *Arg =
508  CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
509 
510  CallArgList Args;
511  Args.add(RValue::get(Arg),
512  CGF.getContext().getPointerType(Var.getType()));
513  CGF.EmitCall(FnInfo, CleanupFn, ReturnValueSlot(), Args);
514  }
515  };
516 
517  /// A cleanup to call @llvm.lifetime.end.
518  class CallLifetimeEnd final : public EHScopeStack::Cleanup {
519  llvm::Value *Addr;
520  llvm::Value *Size;
521  public:
522  CallLifetimeEnd(Address addr, llvm::Value *size)
523  : Addr(addr.getPointer()), Size(size) {}
524 
525  void Emit(CodeGenFunction &CGF, Flags flags) override {
526  CGF.EmitLifetimeEnd(Size, Addr);
527  }
528  };
529 }
530 
531 /// EmitAutoVarWithLifetime - Does the setup required for an automatic
532 /// variable with lifetime.
533 static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var,
534  Address addr,
535  Qualifiers::ObjCLifetime lifetime) {
536  switch (lifetime) {
538  llvm_unreachable("present but none");
539 
541  // nothing to do
542  break;
543 
544  case Qualifiers::OCL_Strong: {
545  CodeGenFunction::Destroyer *destroyer =
546  (var.hasAttr<ObjCPreciseLifetimeAttr>()
549 
550  CleanupKind cleanupKind = CGF.getARCCleanupKind();
551  CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
552  cleanupKind & EHCleanup);
553  break;
554  }
556  // nothing to do
557  break;
558 
560  // __weak objects always get EH cleanups; otherwise, exceptions
561  // could cause really nasty crashes instead of mere leaks.
562  CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
564  /*useEHCleanup*/ true);
565  break;
566  }
567 }
568 
569 static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
570  if (const Expr *e = dyn_cast<Expr>(s)) {
571  // Skip the most common kinds of expressions that make
572  // hierarchy-walking expensive.
573  s = e = e->IgnoreParenCasts();
574 
575  if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
576  return (ref->getDecl() == &var);
577  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
578  const BlockDecl *block = be->getBlockDecl();
579  for (const auto &I : block->captures()) {
580  if (I.getVariable() == &var)
581  return true;
582  }
583  }
584  }
585 
586  for (const Stmt *SubStmt : s->children())
587  // SubStmt might be null; as in missing decl or conditional of an if-stmt.
588  if (SubStmt && isAccessedBy(var, SubStmt))
589  return true;
590 
591  return false;
592 }
593 
594 static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
595  if (!decl) return false;
596  if (!isa<VarDecl>(decl)) return false;
597  const VarDecl *var = cast<VarDecl>(decl);
598  return isAccessedBy(*var, e);
599 }
600 
602  const LValue &destLV, const Expr *init) {
603  bool needsCast = false;
604 
605  while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) {
606  switch (castExpr->getCastKind()) {
607  // Look through casts that don't require representation changes.
608  case CK_NoOp:
609  case CK_BitCast:
611  needsCast = true;
612  break;
613 
614  // If we find an l-value to r-value cast from a __weak variable,
615  // emit this operation as a copy or move.
616  case CK_LValueToRValue: {
617  const Expr *srcExpr = castExpr->getSubExpr();
618  if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
619  return false;
620 
621  // Emit the source l-value.
622  LValue srcLV = CGF.EmitLValue(srcExpr);
623 
624  // Handle a formal type change to avoid asserting.
625  auto srcAddr = srcLV.getAddress();
626  if (needsCast) {
627  srcAddr = CGF.Builder.CreateElementBitCast(srcAddr,
628  destLV.getAddress().getElementType());
629  }
630 
631  // If it was an l-value, use objc_copyWeak.
632  if (srcExpr->getValueKind() == VK_LValue) {
633  CGF.EmitARCCopyWeak(destLV.getAddress(), srcAddr);
634  } else {
635  assert(srcExpr->getValueKind() == VK_XValue);
636  CGF.EmitARCMoveWeak(destLV.getAddress(), srcAddr);
637  }
638  return true;
639  }
640 
641  // Stop at anything else.
642  default:
643  return false;
644  }
645 
646  init = castExpr->getSubExpr();
647  continue;
648  }
649  return false;
650 }
651 
653  LValue &lvalue,
654  const VarDecl *var) {
655  lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(), var));
656 }
657 
658 void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
659  LValue lvalue, bool capturedByInit) {
660  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
661  if (!lifetime) {
662  llvm::Value *value = EmitScalarExpr(init);
663  if (capturedByInit)
664  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
665  EmitStoreThroughLValue(RValue::get(value), lvalue, true);
666  return;
667  }
668 
669  if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
670  init = DIE->getExpr();
671 
672  // If we're emitting a value with lifetime, we have to do the
673  // initialization *before* we leave the cleanup scopes.
674  if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(init)) {
675  enterFullExpression(ewc);
676  init = ewc->getSubExpr();
677  }
679 
680  // We have to maintain the illusion that the variable is
681  // zero-initialized. If the variable might be accessed in its
682  // initializer, zero-initialize before running the initializer, then
683  // actually perform the initialization with an assign.
684  bool accessedByInit = false;
685  if (lifetime != Qualifiers::OCL_ExplicitNone)
686  accessedByInit = (capturedByInit || isAccessedBy(D, init));
687  if (accessedByInit) {
688  LValue tempLV = lvalue;
689  // Drill down to the __block object if necessary.
690  if (capturedByInit) {
691  // We can use a simple GEP for this because it can't have been
692  // moved yet.
694  cast<VarDecl>(D),
695  /*follow*/ false));
696  }
697 
698  auto ty = cast<llvm::PointerType>(tempLV.getAddress().getElementType());
699  llvm::Value *zero = llvm::ConstantPointerNull::get(ty);
700 
701  // If __weak, we want to use a barrier under certain conditions.
702  if (lifetime == Qualifiers::OCL_Weak)
703  EmitARCInitWeak(tempLV.getAddress(), zero);
704 
705  // Otherwise just do a simple store.
706  else
707  EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
708  }
709 
710  // Emit the initializer.
711  llvm::Value *value = nullptr;
712 
713  switch (lifetime) {
715  llvm_unreachable("present but none");
716 
718  // nothing to do
719  value = EmitScalarExpr(init);
720  break;
721 
722  case Qualifiers::OCL_Strong: {
723  value = EmitARCRetainScalarExpr(init);
724  break;
725  }
726 
727  case Qualifiers::OCL_Weak: {
728  // If it's not accessed by the initializer, try to emit the
729  // initialization with a copy or move.
730  if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) {
731  return;
732  }
733 
734  // No way to optimize a producing initializer into this. It's not
735  // worth optimizing for, because the value will immediately
736  // disappear in the common case.
737  value = EmitScalarExpr(init);
738 
739  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
740  if (accessedByInit)
741  EmitARCStoreWeak(lvalue.getAddress(), value, /*ignored*/ true);
742  else
743  EmitARCInitWeak(lvalue.getAddress(), value);
744  return;
745  }
746 
749  break;
750  }
751 
752  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
753 
754  // If the variable might have been accessed by its initializer, we
755  // might have to initialize with a barrier. We have to do this for
756  // both __weak and __strong, but __weak got filtered out above.
757  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
758  llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
759  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
761  return;
762  }
763 
764  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
765 }
766 
767 /// EmitScalarInit - Initialize the given lvalue with the given object.
769  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
770  if (!lifetime)
771  return EmitStoreThroughLValue(RValue::get(init), lvalue, true);
772 
773  switch (lifetime) {
775  llvm_unreachable("present but none");
776 
778  // nothing to do
779  break;
780 
782  init = EmitARCRetain(lvalue.getType(), init);
783  break;
784 
786  // Initialize and then skip the primitive store.
787  EmitARCInitWeak(lvalue.getAddress(), init);
788  return;
789 
791  init = EmitARCRetainAutorelease(lvalue.getType(), init);
792  break;
793  }
794 
795  EmitStoreOfScalar(init, lvalue, /* isInitialization */ true);
796 }
797 
798 /// canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the
799 /// non-zero parts of the specified initializer with equal or fewer than
800 /// NumStores scalar stores.
801 static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init,
802  unsigned &NumStores) {
803  // Zero and Undef never requires any extra stores.
804  if (isa<llvm::ConstantAggregateZero>(Init) ||
805  isa<llvm::ConstantPointerNull>(Init) ||
806  isa<llvm::UndefValue>(Init))
807  return true;
808  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
809  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
810  isa<llvm::ConstantExpr>(Init))
811  return Init->isNullValue() || NumStores--;
812 
813  // See if we can emit each element.
814  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
815  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
816  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
817  if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
818  return false;
819  }
820  return true;
821  }
822 
823  if (llvm::ConstantDataSequential *CDS =
824  dyn_cast<llvm::ConstantDataSequential>(Init)) {
825  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
826  llvm::Constant *Elt = CDS->getElementAsConstant(i);
827  if (!canEmitInitWithFewStoresAfterMemset(Elt, NumStores))
828  return false;
829  }
830  return true;
831  }
832 
833  // Anything else is hard and scary.
834  return false;
835 }
836 
837 /// emitStoresForInitAfterMemset - For inits that
838 /// canEmitInitWithFewStoresAfterMemset returned true for, emit the scalar
839 /// stores that would be required.
840 static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc,
841  bool isVolatile, CGBuilderTy &Builder) {
842  assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
843  "called emitStoresForInitAfterMemset for zero or undef value.");
844 
845  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
846  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
847  isa<llvm::ConstantExpr>(Init)) {
848  Builder.CreateDefaultAlignedStore(Init, Loc, isVolatile);
849  return;
850  }
851 
852  if (llvm::ConstantDataSequential *CDS =
853  dyn_cast<llvm::ConstantDataSequential>(Init)) {
854  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
855  llvm::Constant *Elt = CDS->getElementAsConstant(i);
856 
857  // If necessary, get a pointer to the element and emit it.
858  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
860  Elt, Builder.CreateConstGEP2_32(Init->getType(), Loc, 0, i),
861  isVolatile, Builder);
862  }
863  return;
864  }
865 
866  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
867  "Unknown value type!");
868 
869  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
870  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
871 
872  // If necessary, get a pointer to the element and emit it.
873  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
875  Elt, Builder.CreateConstGEP2_32(Init->getType(), Loc, 0, i),
876  isVolatile, Builder);
877  }
878 }
879 
880 
881 /// shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset
882 /// plus some stores to initialize a local variable instead of using a memcpy
883 /// from a constant global. It is beneficial to use memset if the global is all
884 /// zeros, or mostly zeros and large.
885 static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init,
886  uint64_t GlobalSize) {
887  // If a global is all zeros, always use a memset.
888  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
889 
890  // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
891  // do it if it will require 6 or fewer scalar stores.
892  // TODO: Should budget depends on the size? Avoiding a large global warrants
893  // plopping in more stores.
894  unsigned StoreBudget = 6;
895  uint64_t SizeLimit = 32;
896 
897  return GlobalSize > SizeLimit &&
898  canEmitInitWithFewStoresAfterMemset(Init, StoreBudget);
899 }
900 
901 /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
902 /// variable declaration with auto, register, or no storage class specifier.
903 /// These turn into simple stack objects, or GlobalValues depending on target.
905  AutoVarEmission emission = EmitAutoVarAlloca(D);
906  EmitAutoVarInit(emission);
907  EmitAutoVarCleanups(emission);
908 }
909 
910 /// Emit a lifetime.begin marker if some criteria are satisfied.
911 /// \return a pointer to the temporary size Value if a marker was emitted, null
912 /// otherwise
914  llvm::Value *Addr) {
915  // For now, only in optimized builds.
916  if (CGM.getCodeGenOpts().OptimizationLevel == 0)
917  return nullptr;
918 
919  // Disable lifetime markers in msan builds.
920  // FIXME: Remove this when msan works with lifetime markers.
921  if (getLangOpts().Sanitize.has(SanitizerKind::Memory))
922  return nullptr;
923 
924  llvm::Value *SizeV = llvm::ConstantInt::get(Int64Ty, Size);
925  Addr = Builder.CreateBitCast(Addr, Int8PtrTy);
926  llvm::CallInst *C =
927  Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
928  C->setDoesNotThrow();
929  return SizeV;
930 }
931 
933  Addr = Builder.CreateBitCast(Addr, Int8PtrTy);
934  llvm::CallInst *C =
935  Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr});
936  C->setDoesNotThrow();
937 }
938 
939 /// EmitAutoVarAlloca - Emit the alloca and debug information for a
940 /// local variable. Does not emit initialization or destruction.
943  QualType Ty = D.getType();
944 
945  AutoVarEmission emission(D);
946 
947  bool isByRef = D.hasAttr<BlocksAttr>();
948  emission.IsByRef = isByRef;
949 
950  CharUnits alignment = getContext().getDeclAlign(&D);
951 
952  // If the type is variably-modified, emit all the VLA sizes for it.
953  if (Ty->isVariablyModifiedType())
955 
956  Address address = Address::invalid();
957  if (Ty->isConstantSizeType()) {
958  bool NRVO = getLangOpts().ElideConstructors &&
959  D.isNRVOVariable();
960 
961  // If this value is an array or struct with a statically determinable
962  // constant initializer, there are optimizations we can do.
963  //
964  // TODO: We should constant-evaluate the initializer of any variable,
965  // as long as it is initialized by a constant expression. Currently,
966  // isConstantInitializer produces wrong answers for structs with
967  // reference or bitfield members, and a few other cases, and checking
968  // for POD-ness protects us from some of these.
969  if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
970  (D.isConstexpr() ||
971  ((Ty.isPODType(getContext()) ||
973  D.getInit()->isConstantInitializer(getContext(), false)))) {
974 
975  // If the variable's a const type, and it's neither an NRVO
976  // candidate nor a __block variable and has no mutable members,
977  // emit it as a global instead.
978  if (CGM.getCodeGenOpts().MergeAllConstants && !NRVO && !isByRef &&
979  CGM.isTypeConstant(Ty, true)) {
981 
982  // Signal this condition to later callbacks.
983  emission.Addr = Address::invalid();
984  assert(emission.wasEmittedAsGlobal());
985  return emission;
986  }
987 
988  // Otherwise, tell the initialization code that we're in this case.
989  emission.IsConstantAggregate = true;
990  }
991 
992  // A normal fixed sized variable becomes an alloca in the entry block,
993  // unless it's an NRVO variable.
994 
995  if (NRVO) {
996  // The named return value optimization: allocate this variable in the
997  // return slot, so that we can elide the copy when returning this
998  // variable (C++0x [class.copy]p34).
999  address = ReturnValue;
1000 
1001  if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1002  if (!cast<CXXRecordDecl>(RecordTy->getDecl())->hasTrivialDestructor()) {
1003  // Create a flag that is used to indicate when the NRVO was applied
1004  // to this variable. Set it to zero to indicate that NRVO was not
1005  // applied.
1006  llvm::Value *Zero = Builder.getFalse();
1007  Address NRVOFlag =
1008  CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo");
1010  Builder.CreateStore(Zero, NRVOFlag);
1011 
1012  // Record the NRVO flag for this variable.
1013  NRVOFlags[&D] = NRVOFlag.getPointer();
1014  emission.NRVOFlag = NRVOFlag.getPointer();
1015  }
1016  }
1017  } else {
1018  CharUnits allocaAlignment;
1019  llvm::Type *allocaTy;
1020  if (isByRef) {
1021  auto &byrefInfo = getBlockByrefInfo(&D);
1022  allocaTy = byrefInfo.Type;
1023  allocaAlignment = byrefInfo.ByrefAlignment;
1024  } else {
1025  allocaTy = ConvertTypeForMem(Ty);
1026  allocaAlignment = alignment;
1027  }
1028 
1029  // Create the alloca. Note that we set the name separately from
1030  // building the instruction so that it's there even in no-asserts
1031  // builds.
1032  address = CreateTempAlloca(allocaTy, allocaAlignment);
1033  address.getPointer()->setName(D.getName());
1034 
1035  // Don't emit lifetime markers for MSVC catch parameters. The lifetime of
1036  // the catch parameter starts in the catchpad instruction, and we can't
1037  // insert code in those basic blocks.
1038  bool IsMSCatchParam =
1040 
1041  // Emit a lifetime intrinsic if meaningful. There's no point
1042  // in doing this if we don't have a valid insertion point (?).
1043  if (HaveInsertPoint() && !IsMSCatchParam) {
1044  uint64_t size = CGM.getDataLayout().getTypeAllocSize(allocaTy);
1045  emission.SizeForLifetimeMarkers =
1046  EmitLifetimeStart(size, address.getPointer());
1047  } else {
1048  assert(!emission.useLifetimeMarkers());
1049  }
1050  }
1051  } else {
1053 
1054  if (!DidCallStackSave) {
1055  // Save the stack.
1056  Address Stack =
1057  CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
1058 
1059  llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
1060  llvm::Value *V = Builder.CreateCall(F);
1061  Builder.CreateStore(V, Stack);
1062 
1063  DidCallStackSave = true;
1064 
1065  // Push a cleanup block and restore the stack there.
1066  // FIXME: in general circumstances, this should be an EH cleanup.
1068  }
1069 
1070  llvm::Value *elementCount;
1071  QualType elementType;
1072  std::tie(elementCount, elementType) = getVLASize(Ty);
1073 
1074  llvm::Type *llvmTy = ConvertTypeForMem(elementType);
1075 
1076  // Allocate memory for the array.
1077  llvm::AllocaInst *vla = Builder.CreateAlloca(llvmTy, elementCount, "vla");
1078  vla->setAlignment(alignment.getQuantity());
1079 
1080  address = Address(vla, alignment);
1081  }
1082 
1083  setAddrOfLocalVar(&D, address);
1084  emission.Addr = address;
1085 
1086  // Emit debug info for local var declaration.
1087  if (HaveInsertPoint())
1088  if (CGDebugInfo *DI = getDebugInfo()) {
1089  if (CGM.getCodeGenOpts().getDebugInfo()
1091  DI->setLocation(D.getLocation());
1092  DI->EmitDeclareOfAutoVariable(&D, address.getPointer(), Builder);
1093  }
1094  }
1095 
1096  if (D.hasAttr<AnnotateAttr>())
1097  EmitVarAnnotations(&D, address.getPointer());
1098 
1099  return emission;
1100 }
1101 
1102 /// Determines whether the given __block variable is potentially
1103 /// captured by the given expression.
1104 static bool isCapturedBy(const VarDecl &var, const Expr *e) {
1105  // Skip the most common kinds of expressions that make
1106  // hierarchy-walking expensive.
1107  e = e->IgnoreParenCasts();
1108 
1109  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
1110  const BlockDecl *block = be->getBlockDecl();
1111  for (const auto &I : block->captures()) {
1112  if (I.getVariable() == &var)
1113  return true;
1114  }
1115 
1116  // No need to walk into the subexpressions.
1117  return false;
1118  }
1119 
1120  if (const StmtExpr *SE = dyn_cast<StmtExpr>(e)) {
1121  const CompoundStmt *CS = SE->getSubStmt();
1122  for (const auto *BI : CS->body())
1123  if (const auto *E = dyn_cast<Expr>(BI)) {
1124  if (isCapturedBy(var, E))
1125  return true;
1126  }
1127  else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1128  // special case declarations
1129  for (const auto *I : DS->decls()) {
1130  if (const auto *VD = dyn_cast<VarDecl>((I))) {
1131  const Expr *Init = VD->getInit();
1132  if (Init && isCapturedBy(var, Init))
1133  return true;
1134  }
1135  }
1136  }
1137  else
1138  // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1139  // Later, provide code to poke into statements for capture analysis.
1140  return true;
1141  return false;
1142  }
1143 
1144  for (const Stmt *SubStmt : e->children())
1145  if (isCapturedBy(var, cast<Expr>(SubStmt)))
1146  return true;
1147 
1148  return false;
1149 }
1150 
1151 /// \brief Determine whether the given initializer is trivial in the sense
1152 /// that it requires no code to be generated.
1154  if (!Init)
1155  return true;
1156 
1157  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1158  if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1159  if (Constructor->isTrivial() &&
1160  Constructor->isDefaultConstructor() &&
1161  !Construct->requiresZeroInitialization())
1162  return true;
1163 
1164  return false;
1165 }
1167  assert(emission.Variable && "emission was not valid!");
1168 
1169  // If this was emitted as a global constant, we're done.
1170  if (emission.wasEmittedAsGlobal()) return;
1171 
1172  const VarDecl &D = *emission.Variable;
1174  QualType type = D.getType();
1175 
1176  // If this local has an initializer, emit it now.
1177  const Expr *Init = D.getInit();
1178 
1179  // If we are at an unreachable point, we don't need to emit the initializer
1180  // unless it contains a label.
1181  if (!HaveInsertPoint()) {
1182  if (!Init || !ContainsLabel(Init)) return;
1184  }
1185 
1186  // Initialize the structure of a __block variable.
1187  if (emission.IsByRef)
1188  emitByrefStructureInit(emission);
1189 
1190  if (isTrivialInitializer(Init))
1191  return;
1192 
1193  // Check whether this is a byref variable that's potentially
1194  // captured and moved by its own initializer. If so, we'll need to
1195  // emit the initializer first, then copy into the variable.
1196  bool capturedByInit = emission.IsByRef && isCapturedBy(D, Init);
1197 
1198  Address Loc =
1199  capturedByInit ? emission.Addr : emission.getObjectAddress(*this);
1200 
1201  llvm::Constant *constant = nullptr;
1202  if (emission.IsConstantAggregate || D.isConstexpr()) {
1203  assert(!capturedByInit && "constant init contains a capturing block?");
1204  constant = CGM.EmitConstantInit(D, this);
1205  }
1206 
1207  if (!constant) {
1208  LValue lv = MakeAddrLValue(Loc, type);
1209  lv.setNonGC(true);
1210  return EmitExprAsInit(Init, &D, lv, capturedByInit);
1211  }
1212 
1213  if (!emission.IsConstantAggregate) {
1214  // For simple scalar/complex initialization, store the value directly.
1215  LValue lv = MakeAddrLValue(Loc, type);
1216  lv.setNonGC(true);
1217  return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1218  }
1219 
1220  // If this is a simple aggregate initialization, we can optimize it
1221  // in various ways.
1222  bool isVolatile = type.isVolatileQualified();
1223 
1224  llvm::Value *SizeVal =
1225  llvm::ConstantInt::get(IntPtrTy,
1226  getContext().getTypeSizeInChars(type).getQuantity());
1227 
1228  llvm::Type *BP = Int8PtrTy;
1229  if (Loc.getType() != BP)
1230  Loc = Builder.CreateBitCast(Loc, BP);
1231 
1232  // If the initializer is all or mostly zeros, codegen with memset then do
1233  // a few stores afterward.
1235  CGM.getDataLayout().getTypeAllocSize(constant->getType()))) {
1236  Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0), SizeVal,
1237  isVolatile);
1238  // Zero and undef don't require a stores.
1239  if (!constant->isNullValue() && !isa<llvm::UndefValue>(constant)) {
1240  Loc = Builder.CreateBitCast(Loc, constant->getType()->getPointerTo());
1241  emitStoresForInitAfterMemset(constant, Loc.getPointer(),
1242  isVolatile, Builder);
1243  }
1244  } else {
1245  // Otherwise, create a temporary global with the initializer then
1246  // memcpy from the global to the alloca.
1247  std::string Name = getStaticDeclName(CGM, D);
1248  llvm::GlobalVariable *GV =
1249  new llvm::GlobalVariable(CGM.getModule(), constant->getType(), true,
1250  llvm::GlobalValue::PrivateLinkage,
1251  constant, Name);
1252  GV->setAlignment(Loc.getAlignment().getQuantity());
1253  GV->setUnnamedAddr(true);
1254 
1255  Address SrcPtr = Address(GV, Loc.getAlignment());
1256  if (SrcPtr.getType() != BP)
1257  SrcPtr = Builder.CreateBitCast(SrcPtr, BP);
1258 
1259  Builder.CreateMemCpy(Loc, SrcPtr, SizeVal, isVolatile);
1260  }
1261 }
1262 
1263 /// Emit an expression as an initializer for a variable at the given
1264 /// location. The expression is not necessarily the normal
1265 /// initializer for the variable, and the address is not necessarily
1266 /// its normal location.
1267 ///
1268 /// \param init the initializing expression
1269 /// \param var the variable to act as if we're initializing
1270 /// \param loc the address to initialize; its type is a pointer
1271 /// to the LLVM mapping of the variable's type
1272 /// \param alignment the alignment of the address
1273 /// \param capturedByInit true if the variable is a __block variable
1274 /// whose address is potentially changed by the initializer
1276  LValue lvalue, bool capturedByInit) {
1277  QualType type = D->getType();
1278 
1279  if (type->isReferenceType()) {
1280  RValue rvalue = EmitReferenceBindingToExpr(init);
1281  if (capturedByInit)
1282  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1283  EmitStoreThroughLValue(rvalue, lvalue, true);
1284  return;
1285  }
1286  switch (getEvaluationKind(type)) {
1287  case TEK_Scalar:
1288  EmitScalarInit(init, D, lvalue, capturedByInit);
1289  return;
1290  case TEK_Complex: {
1291  ComplexPairTy complex = EmitComplexExpr(init);
1292  if (capturedByInit)
1293  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1294  EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1295  return;
1296  }
1297  case TEK_Aggregate:
1298  if (type->isAtomicType()) {
1299  EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1300  } else {
1301  // TODO: how can we delay here if D is captured by its initializer?
1302  EmitAggExpr(init, AggValueSlot::forLValue(lvalue,
1306  }
1307  return;
1308  }
1309  llvm_unreachable("bad evaluation kind");
1310 }
1311 
1312 /// Enter a destroy cleanup for the given local variable.
1314  const CodeGenFunction::AutoVarEmission &emission,
1315  QualType::DestructionKind dtorKind) {
1316  assert(dtorKind != QualType::DK_none);
1317 
1318  // Note that for __block variables, we want to destroy the
1319  // original stack object, not the possibly forwarded object.
1320  Address addr = emission.getObjectAddress(*this);
1321 
1322  const VarDecl *var = emission.Variable;
1323  QualType type = var->getType();
1324 
1325  CleanupKind cleanupKind = NormalAndEHCleanup;
1326  CodeGenFunction::Destroyer *destroyer = nullptr;
1327 
1328  switch (dtorKind) {
1329  case QualType::DK_none:
1330  llvm_unreachable("no cleanup for trivially-destructible variable");
1331 
1333  // If there's an NRVO flag on the emission, we need a different
1334  // cleanup.
1335  if (emission.NRVOFlag) {
1336  assert(!type->isArrayType());
1338  EHStack.pushCleanup<DestroyNRVOVariable>(cleanupKind, addr,
1339  dtor, emission.NRVOFlag);
1340  return;
1341  }
1342  break;
1343 
1345  // Suppress cleanups for pseudo-strong variables.
1346  if (var->isARCPseudoStrong()) return;
1347 
1348  // Otherwise, consider whether to use an EH cleanup or not.
1349  cleanupKind = getARCCleanupKind();
1350 
1351  // Use the imprecise destroyer by default.
1352  if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
1354  break;
1355 
1357  break;
1358  }
1359 
1360  // If we haven't chosen a more specific destroyer, use the default.
1361  if (!destroyer) destroyer = getDestroyer(dtorKind);
1362 
1363  // Use an EH cleanup in array destructors iff the destructor itself
1364  // is being pushed as an EH cleanup.
1365  bool useEHCleanup = (cleanupKind & EHCleanup);
1366  EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
1367  useEHCleanup);
1368 }
1369 
1371  assert(emission.Variable && "emission was not valid!");
1372 
1373  // If this was emitted as a global constant, we're done.
1374  if (emission.wasEmittedAsGlobal()) return;
1375 
1376  // If we don't have an insertion point, we're done. Sema prevents
1377  // us from jumping into any of these scopes anyway.
1378  if (!HaveInsertPoint()) return;
1379 
1380  const VarDecl &D = *emission.Variable;
1381 
1382  // Make sure we call @llvm.lifetime.end. This needs to happen
1383  // *last*, so the cleanup needs to be pushed *first*.
1384  if (emission.useLifetimeMarkers()) {
1385  EHStack.pushCleanup<CallLifetimeEnd>(NormalCleanup,
1386  emission.getAllocatedAddress(),
1387  emission.getSizeForLifetimeMarkers());
1388  EHCleanupScope &cleanup = cast<EHCleanupScope>(*EHStack.begin());
1389  cleanup.setLifetimeMarker();
1390  }
1391 
1392  // Check the type for a cleanup.
1393  if (QualType::DestructionKind dtorKind = D.getType().isDestructedType())
1394  emitAutoVarTypeCleanup(emission, dtorKind);
1395 
1396  // In GC mode, honor objc_precise_lifetime.
1397  if (getLangOpts().getGC() != LangOptions::NonGC &&
1398  D.hasAttr<ObjCPreciseLifetimeAttr>()) {
1399  EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
1400  }
1401 
1402  // Handle the cleanup attribute.
1403  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
1404  const FunctionDecl *FD = CA->getFunctionDecl();
1405 
1406  llvm::Constant *F = CGM.GetAddrOfFunction(FD);
1407  assert(F && "Could not find function!");
1408 
1410  EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
1411  }
1412 
1413  // If this is a block variable, call _Block_object_destroy
1414  // (on the unforwarded address).
1415  if (emission.IsByRef)
1416  enterByrefCleanup(emission);
1417 }
1418 
1421  switch (kind) {
1422  case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
1424  return destroyCXXObject;
1426  return destroyARCStrongPrecise;
1428  return destroyARCWeak;
1429  }
1430  llvm_unreachable("Unknown DestructionKind");
1431 }
1432 
1433 /// pushEHDestroy - Push the standard destructor for the given type as
1434 /// an EH-only cleanup.
1436  Address addr, QualType type) {
1437  assert(dtorKind && "cannot push destructor for trivial type");
1438  assert(needsEHCleanup(dtorKind));
1439 
1440  pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
1441 }
1442 
1443 /// pushDestroy - Push the standard destructor for the given type as
1444 /// at least a normal cleanup.
1446  Address addr, QualType type) {
1447  assert(dtorKind && "cannot push destructor for trivial type");
1448 
1449  CleanupKind cleanupKind = getCleanupKind(dtorKind);
1450  pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
1451  cleanupKind & EHCleanup);
1452 }
1453 
1455  QualType type, Destroyer *destroyer,
1456  bool useEHCleanupForArray) {
1457  pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
1458  destroyer, useEHCleanupForArray);
1459 }
1460 
1462  EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
1463 }
1464 
1466  CleanupKind cleanupKind, Address addr, QualType type,
1467  Destroyer *destroyer, bool useEHCleanupForArray) {
1468  assert(!isInConditionalBranch() &&
1469  "performing lifetime extension from within conditional");
1470 
1471  // Push an EH-only cleanup for the object now.
1472  // FIXME: When popping normal cleanups, we need to keep this EH cleanup
1473  // around in case a temporary's destructor throws an exception.
1474  if (cleanupKind & EHCleanup)
1475  EHStack.pushCleanup<DestroyObject>(
1476  static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
1477  destroyer, useEHCleanupForArray);
1478 
1479  // Remember that we need to push a full cleanup for the object at the
1480  // end of the full-expression.
1481  pushCleanupAfterFullExpr<DestroyObject>(
1482  cleanupKind, addr, type, destroyer, useEHCleanupForArray);
1483 }
1484 
1485 /// emitDestroy - Immediately perform the destruction of the given
1486 /// object.
1487 ///
1488 /// \param addr - the address of the object; a type*
1489 /// \param type - the type of the object; if an array type, all
1490 /// objects are destroyed in reverse order
1491 /// \param destroyer - the function to call to destroy individual
1492 /// elements
1493 /// \param useEHCleanupForArray - whether an EH cleanup should be
1494 /// used when destroying array elements, in case one of the
1495 /// destructions throws an exception
1497  Destroyer *destroyer,
1498  bool useEHCleanupForArray) {
1499  const ArrayType *arrayType = getContext().getAsArrayType(type);
1500  if (!arrayType)
1501  return destroyer(*this, addr, type);
1502 
1503  llvm::Value *length = emitArrayLength(arrayType, type, addr);
1504 
1505  CharUnits elementAlign =
1506  addr.getAlignment()
1507  .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
1508 
1509  // Normally we have to check whether the array is zero-length.
1510  bool checkZeroLength = true;
1511 
1512  // But if the array length is constant, we can suppress that.
1513  if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
1514  // ...and if it's constant zero, we can just skip the entire thing.
1515  if (constLength->isZero()) return;
1516  checkZeroLength = false;
1517  }
1518 
1519  llvm::Value *begin = addr.getPointer();
1520  llvm::Value *end = Builder.CreateInBoundsGEP(begin, length);
1521  emitArrayDestroy(begin, end, type, elementAlign, destroyer,
1522  checkZeroLength, useEHCleanupForArray);
1523 }
1524 
1525 /// emitArrayDestroy - Destroys all the elements of the given array,
1526 /// beginning from last to first. The array cannot be zero-length.
1527 ///
1528 /// \param begin - a type* denoting the first element of the array
1529 /// \param end - a type* denoting one past the end of the array
1530 /// \param elementType - the element type of the array
1531 /// \param destroyer - the function to call to destroy elements
1532 /// \param useEHCleanup - whether to push an EH cleanup to destroy
1533 /// the remaining elements in case the destruction of a single
1534 /// element throws
1536  llvm::Value *end,
1537  QualType elementType,
1538  CharUnits elementAlign,
1539  Destroyer *destroyer,
1540  bool checkZeroLength,
1541  bool useEHCleanup) {
1542  assert(!elementType->isArrayType());
1543 
1544  // The basic structure here is a do-while loop, because we don't
1545  // need to check for the zero-element case.
1546  llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
1547  llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
1548 
1549  if (checkZeroLength) {
1550  llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
1551  "arraydestroy.isempty");
1552  Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
1553  }
1554 
1555  // Enter the loop body, making that address the current address.
1556  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
1557  EmitBlock(bodyBB);
1558  llvm::PHINode *elementPast =
1559  Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
1560  elementPast->addIncoming(end, entryBB);
1561 
1562  // Shift the address back by one element.
1563  llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
1564  llvm::Value *element = Builder.CreateInBoundsGEP(elementPast, negativeOne,
1565  "arraydestroy.element");
1566 
1567  if (useEHCleanup)
1568  pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign,
1569  destroyer);
1570 
1571  // Perform the actual destruction there.
1572  destroyer(*this, Address(element, elementAlign), elementType);
1573 
1574  if (useEHCleanup)
1575  PopCleanupBlock();
1576 
1577  // Check whether we've reached the end.
1578  llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
1579  Builder.CreateCondBr(done, doneBB, bodyBB);
1580  elementPast->addIncoming(element, Builder.GetInsertBlock());
1581 
1582  // Done.
1583  EmitBlock(doneBB);
1584 }
1585 
1586 /// Perform partial array destruction as if in an EH cleanup. Unlike
1587 /// emitArrayDestroy, the element type here may still be an array type.
1590  QualType type, CharUnits elementAlign,
1591  CodeGenFunction::Destroyer *destroyer) {
1592  // If the element type is itself an array, drill down.
1593  unsigned arrayDepth = 0;
1594  while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
1595  // VLAs don't require a GEP index to walk into.
1596  if (!isa<VariableArrayType>(arrayType))
1597  arrayDepth++;
1598  type = arrayType->getElementType();
1599  }
1600 
1601  if (arrayDepth) {
1602  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
1603 
1604  SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero);
1605  begin = CGF.Builder.CreateInBoundsGEP(begin, gepIndices, "pad.arraybegin");
1606  end = CGF.Builder.CreateInBoundsGEP(end, gepIndices, "pad.arrayend");
1607  }
1608 
1609  // Destroy the array. We don't ever need an EH cleanup because we
1610  // assume that we're in an EH cleanup ourselves, so a throwing
1611  // destructor causes an immediate terminate.
1612  CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer,
1613  /*checkZeroLength*/ true, /*useEHCleanup*/ false);
1614 }
1615 
1616 namespace {
1617  /// RegularPartialArrayDestroy - a cleanup which performs a partial
1618  /// array destroy where the end pointer is regularly determined and
1619  /// does not need to be loaded from a local.
1620  class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup {
1621  llvm::Value *ArrayBegin;
1622  llvm::Value *ArrayEnd;
1623  QualType ElementType;
1624  CodeGenFunction::Destroyer *Destroyer;
1625  CharUnits ElementAlign;
1626  public:
1627  RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
1628  QualType elementType, CharUnits elementAlign,
1629  CodeGenFunction::Destroyer *destroyer)
1630  : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
1631  ElementType(elementType), Destroyer(destroyer),
1632  ElementAlign(elementAlign) {}
1633 
1634  void Emit(CodeGenFunction &CGF, Flags flags) override {
1635  emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
1636  ElementType, ElementAlign, Destroyer);
1637  }
1638  };
1639 
1640  /// IrregularPartialArrayDestroy - a cleanup which performs a
1641  /// partial array destroy where the end pointer is irregularly
1642  /// determined and must be loaded from a local.
1643  class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup {
1644  llvm::Value *ArrayBegin;
1645  Address ArrayEndPointer;
1646  QualType ElementType;
1647  CodeGenFunction::Destroyer *Destroyer;
1648  CharUnits ElementAlign;
1649  public:
1650  IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
1651  Address arrayEndPointer,
1652  QualType elementType,
1653  CharUnits elementAlign,
1654  CodeGenFunction::Destroyer *destroyer)
1655  : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
1656  ElementType(elementType), Destroyer(destroyer),
1657  ElementAlign(elementAlign) {}
1658 
1659  void Emit(CodeGenFunction &CGF, Flags flags) override {
1660  llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
1661  emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
1662  ElementType, ElementAlign, Destroyer);
1663  }
1664  };
1665 }
1666 
1667 /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
1668 /// already-constructed elements of the given array. The cleanup
1669 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1670 ///
1671 /// \param elementType - the immediate element type of the array;
1672 /// possibly still an array type
1674  Address arrayEndPointer,
1675  QualType elementType,
1676  CharUnits elementAlign,
1677  Destroyer *destroyer) {
1678  pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
1679  arrayBegin, arrayEndPointer,
1680  elementType, elementAlign,
1681  destroyer);
1682 }
1683 
1684 /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
1685 /// already-constructed elements of the given array. The cleanup
1686 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
1687 ///
1688 /// \param elementType - the immediate element type of the array;
1689 /// possibly still an array type
1691  llvm::Value *arrayEnd,
1692  QualType elementType,
1693  CharUnits elementAlign,
1694  Destroyer *destroyer) {
1695  pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
1696  arrayBegin, arrayEnd,
1697  elementType, elementAlign,
1698  destroyer);
1699 }
1700 
1701 /// Lazily declare the @llvm.lifetime.start intrinsic.
1703  if (LifetimeStartFn) return LifetimeStartFn;
1704  LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
1705  llvm::Intrinsic::lifetime_start);
1706  return LifetimeStartFn;
1707 }
1708 
1709 /// Lazily declare the @llvm.lifetime.end intrinsic.
1711  if (LifetimeEndFn) return LifetimeEndFn;
1712  LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
1713  llvm::Intrinsic::lifetime_end);
1714  return LifetimeEndFn;
1715 }
1716 
1717 namespace {
1718  /// A cleanup to perform a release of an object at the end of a
1719  /// function. This is used to balance out the incoming +1 of a
1720  /// ns_consumed argument when we can't reasonably do that just by
1721  /// not doing the initial retain for a __block argument.
1722  struct ConsumeARCParameter final : EHScopeStack::Cleanup {
1723  ConsumeARCParameter(llvm::Value *param,
1724  ARCPreciseLifetime_t precise)
1725  : Param(param), Precise(precise) {}
1726 
1727  llvm::Value *Param;
1728  ARCPreciseLifetime_t Precise;
1729 
1730  void Emit(CodeGenFunction &CGF, Flags flags) override {
1731  CGF.EmitARCRelease(Param, Precise);
1732  }
1733  };
1734 }
1735 
1736 /// Emit an alloca (or GlobalValue depending on target)
1737 /// for the specified parameter and set up LocalDeclMap.
1739  unsigned ArgNo) {
1740  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
1741  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
1742  "Invalid argument to EmitParmDecl");
1743 
1744  Arg.getAnyValue()->setName(D.getName());
1745 
1746  QualType Ty = D.getType();
1747 
1748  // Use better IR generation for certain implicit parameters.
1749  if (auto IPD = dyn_cast<ImplicitParamDecl>(&D)) {
1750  // The only implicit argument a block has is its literal.
1751  // We assume this is always passed directly.
1752  if (BlockInfo) {
1753  setBlockContextParameter(IPD, ArgNo, Arg.getDirectValue());
1754  return;
1755  }
1756  }
1757 
1758  Address DeclPtr = Address::invalid();
1759  bool DoStore = false;
1760  bool IsScalar = hasScalarEvaluationKind(Ty);
1761  // If we already have a pointer to the argument, reuse the input pointer.
1762  if (Arg.isIndirect()) {
1763  DeclPtr = Arg.getIndirectAddress();
1764  // If we have a prettier pointer type at this point, bitcast to that.
1765  unsigned AS = DeclPtr.getType()->getAddressSpace();
1766  llvm::Type *IRTy = ConvertTypeForMem(Ty)->getPointerTo(AS);
1767  if (DeclPtr.getType() != IRTy)
1768  DeclPtr = Builder.CreateBitCast(DeclPtr, IRTy, D.getName());
1769 
1770  // Push a destructor cleanup for this parameter if the ABI requires it.
1771  // Don't push a cleanup in a thunk for a method that will also emit a
1772  // cleanup.
1773  if (!IsScalar && !CurFuncIsThunk &&
1775  const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
1776  if (RD && RD->hasNonTrivialDestructor())
1778  }
1779  } else {
1780  // Otherwise, create a temporary to hold the value.
1781  DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
1782  D.getName() + ".addr");
1783  DoStore = true;
1784  }
1785 
1786  llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr);
1787 
1788  LValue lv = MakeAddrLValue(DeclPtr, Ty);
1789  if (IsScalar) {
1790  Qualifiers qs = Ty.getQualifiers();
1791  if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
1792  // We honor __attribute__((ns_consumed)) for types with lifetime.
1793  // For __strong, it's handled by just skipping the initial retain;
1794  // otherwise we have to balance out the initial +1 with an extra
1795  // cleanup to do the release at the end of the function.
1796  bool isConsumed = D.hasAttr<NSConsumedAttr>();
1797 
1798  // 'self' is always formally __strong, but if this is not an
1799  // init method then we don't want to retain it.
1800  if (D.isARCPseudoStrong()) {
1801  const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CurCodeDecl);
1802  assert(&D == method->getSelfDecl());
1803  assert(lt == Qualifiers::OCL_Strong);
1804  assert(qs.hasConst());
1805  assert(method->getMethodFamily() != OMF_init);
1806  (void) method;
1808  }
1809 
1810  if (lt == Qualifiers::OCL_Strong) {
1811  if (!isConsumed) {
1812  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
1813  // use objc_storeStrong(&dest, value) for retaining the
1814  // object. But first, store a null into 'dest' because
1815  // objc_storeStrong attempts to release its old value.
1816  llvm::Value *Null = CGM.EmitNullConstant(D.getType());
1817  EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
1818  EmitARCStoreStrongCall(lv.getAddress(), ArgVal, true);
1819  DoStore = false;
1820  }
1821  else
1822  // Don't use objc_retainBlock for block pointers, because we
1823  // don't want to Block_copy something just because we got it
1824  // as a parameter.
1825  ArgVal = EmitARCRetainNonBlock(ArgVal);
1826  }
1827  } else {
1828  // Push the cleanup for a consumed parameter.
1829  if (isConsumed) {
1830  ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
1832  EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
1833  precise);
1834  }
1835 
1836  if (lt == Qualifiers::OCL_Weak) {
1837  EmitARCInitWeak(DeclPtr, ArgVal);
1838  DoStore = false; // The weak init is a store, no need to do two.
1839  }
1840  }
1841 
1842  // Enter the cleanup scope.
1843  EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
1844  }
1845  }
1846 
1847  // Store the initial value into the alloca.
1848  if (DoStore)
1849  EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
1850 
1851  setAddrOfLocalVar(&D, DeclPtr);
1852 
1853  // Emit debug info for param declaration.
1854  if (CGDebugInfo *DI = getDebugInfo()) {
1855  if (CGM.getCodeGenOpts().getDebugInfo()
1857  DI->EmitDeclareOfArgVariable(&D, DeclPtr.getPointer(), ArgNo, Builder);
1858  }
1859  }
1860 
1861  if (D.hasAttr<AnnotateAttr>())
1862  EmitVarAnnotations(&D, DeclPtr.getPointer());
1863 }
unsigned getAddressSpace() const
Return the address space of this type.
Definition: Type.h:5204
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
ReturnValueSlot - Contains the address where the return value of a function can be stored...
Definition: CGCall.h:151
Defines the clang::ASTContext interface.
llvm::StoreInst * CreateDefaultAlignedStore(llvm::Value *Val, llvm::Value *Addr, bool IsVolatile=false)
Definition: CGBuilder.h:148
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1483
CK_LValueToRValue - A conversion which causes the extraction of an r-value from the operand gl-value...
StringRef getName() const
getName - Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:169
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:343
llvm::Value * EmitARCRetainAutoreleaseScalarExpr(const Expr *expr)
Definition: CGObjC.cpp:2759
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:1420
A (possibly-)qualified type.
Definition: Type.h:575
void EmitExtendGCLifetime(llvm::Value *object)
EmitExtendGCLifetime - Given a pointer to an Objective-C object, make sure it survives garbage collec...
Definition: CGObjC.cpp:2879
llvm::Value * getPointer() const
Definition: CGValue.h:327
llvm::Type * ConvertTypeForMem(QualType T)
void EmitVarDecl(const VarDecl &D)
EmitVarDecl - Emit a local variable declaration.
Definition: CGDecl.cpp:131
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
Address getAllocatedAddress() const
Returns the raw, allocated address, which is not necessarily the address of the object itself...
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1263
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after...
Definition: Type.h:1003
llvm::Module & getModule() const
static AggValueSlot forLValue(const LValue &LV, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, IsZeroed_t isZeroed=IsNotZeroed)
Definition: CGValue.h:524
bool isInConditionalBranch() const
isInConditionalBranch - Return true if we're currently emitting one branch or the other of a conditio...
const TargetInfo & getTarget() const
SanitizerSet Sanitize
Set of enabled sanitizers.
Definition: LangOptions.h:79
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateTempAlloca - This creates a alloca and inserts it into the entry block.
Definition: CGExpr.cpp:66
Defines the SourceManager interface.
static bool canEmitInitWithFewStoresAfterMemset(llvm::Constant *Init, unsigned &NumStores)
canEmitInitWithFewStoresAfterMemset - Decide whether we can emit the non-zero parts of the specified ...
Definition: CGDecl.cpp:801
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:171
bool isRecordType() const
Definition: Type.h:5362
void emitAutoVarTypeCleanup(const AutoVarEmission &emission, QualType::DestructionKind dtorKind)
Enter a destroy cleanup for the given local variable.
Definition: CGDecl.cpp:1313
QualType getUnderlyingType() const
Definition: Decl.h:2566
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:77
Address getAddress() const
Definition: CGValue.h:331
const Decl * CurCodeDecl
CurCodeDecl - This is the inner-most code context, which includes blocks.
bool hasNonTrivialDestructor() const
Determine whether this class has a non-trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1269
void EmitAutoVarDecl(const VarDecl &D)
EmitAutoVarDecl - Emit an auto variable declaration.
Definition: CGDecl.cpp:904
const llvm::DataLayout & getDataLayout() const
static Destroyer destroyARCStrongPrecise
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition: CGDecl.cpp:1465
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
Definition: CGExpr.cpp:1568
Emit only debug info necessary for generating line number tables (-gline-tables-only).
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:2424
const Expr * getInit() const
Definition: Decl.h:1070
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1149
void EmitARCCopyWeak(Address dst, Address src)
void @objc_copyWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2270
const LangOptions & getLangOpts() const
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition: CGObjC.cpp:1948
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2134
VarDecl - An instance of this class is created to represent a variable declaration or definition...
Definition: Decl.h:699
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:52
bool areArgsDestroyedLeftToRightInCallee() const
Are arguments to a call destroyed left to right in the callee? This is a fundamental language change...
Definition: TargetCXXABI.h:217
ObjCLifetime getObjCLifetime() const
Definition: Type.h:290
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:51
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
capture_range captures()
Definition: Decl.h:3509
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:2847
iterator begin() const
Definition: Type.h:4072
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have...
Definition: Linkage.h:25
The collection of all-type qualifiers we support.
Definition: Type.h:116
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
emitDestroy - Immediately perform the destruction of the given object.
Definition: CGDecl.cpp:1496
void emitByrefStructureInit(const AutoVarEmission &emission)
Initialize the structural components of a __block variable, i.e.
Definition: CGBlocks.cpp:2143
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4381
bool hasAttr() const
Definition: DeclBase.h:498
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: Type.h:1766
void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
EmitExprAsInit - Emits the code necessary to initialize a location in memory with the given initializ...
Definition: CGDecl.cpp:1275
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
Definition: CGExpr.cpp:460
bool isReferenceType() const
Definition: Type.h:5314
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Definition: ASTMatchers.h:259
void pushEHDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushEHDestroy - Push the standard destructor for the given type as an EH-only cleanup.
Definition: CGDecl.cpp:1435
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:81
void reportGlobalToASan(llvm::GlobalVariable *GV, const VarDecl &D, bool IsDynInit=false)
RValue EmitCall(const CGFunctionInfo &FnInfo, llvm::Value *Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, CGCalleeInfo CalleeInfo=CGCalleeInfo(), llvm::Instruction **callOrInvoke=nullptr)
EmitCall - Generate a call of the given function, expecting the given result type, and using the given argument list which specifies both the LLVM arguments and the types they were derived from.
Definition: CGCall.cpp:3159
CleanupKind getCleanupKind(QualType::DestructionKind kind)
const Decl * getDecl() const
Definition: GlobalDecl.h:60
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition: Decl.h:917
void setNonGC(bool Value)
Definition: CGValue.h:271
T * getAttr() const
Definition: DeclBase.h:495
llvm::Constant * getLLVMLifetimeStartFn()
Lazily declare the .lifetime.start intrinsic.
Definition: CGDecl.cpp:1702
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition: CGObjC.cpp:2082
void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, llvm::Value *arrayEnd, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushRegularPartialArrayCleanup - Push an EH cleanup to destroy already-constructed elements of the gi...
Definition: CGDecl.cpp:1690
static bool hasScalarEvaluationKind(QualType T)
iterator begin() const
Returns an iterator pointing to the innermost EH scope.
Definition: CGCleanup.h:566
static void drillIntoBlockVariable(CodeGenFunction &CGF, LValue &lvalue, const VarDecl *var)
Definition: CGDecl.cpp:652
Base object ctor.
Definition: ABI.h:27
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
Definition: CGBuilder.h:176
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
static bool shouldUseMemSetPlusStoresToInitialize(llvm::Constant *Init, uint64_t GlobalSize)
shouldUseMemSetPlusStoresToInitialize - Decide whether we should use memset plus some stores to initi...
Definition: CGDecl.cpp:885
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition: CGValue.h:260
ObjCMethodFamily getMethodFamily() const
Determines the family of this method.
Definition: DeclObjC.cpp:885
An x-value expression is a reference to an object with independent storage but which can be "moved"...
Definition: Specifiers.h:111
void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl)
Emit information about a global variable.
void setStaticLocalDeclAddress(const VarDecl *D, llvm::Constant *C)
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind...
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:272
std::string getNameAsString() const
getNameAsString - Get a human-readable name for the declaration, even if it is one of the special kin...
Definition: Decl.h:184
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2464
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:38
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
llvm::Value * EmitARCRetainScalarExpr(const Expr *expr)
EmitARCRetainScalarExpr - Semantically equivalent to EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a best-effort attempt to peephole expressions that naturally produce retained objects.
Definition: CGObjC.cpp:2743
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:85
void EmitAtomicInit(Expr *E, LValue lvalue)
Definition: CGAtomic.cpp:1835
llvm::Constant * GetAddrOfGlobal(GlobalDecl GD, bool IsForDefinition=false)
bool hasConst() const
Definition: Type.h:229
bool isStaticLocal() const
isStaticLocal - Returns true if a variable with function scope is a static local variable.
Definition: Decl.h:908
iterator end() const
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
void setGlobalVisibility(llvm::GlobalValue *GV, const NamedDecl *D) const
Set the visibility for the given LLVM GlobalValue.
detail::InMemoryDirectory::const_iterator I
QualType getType() const
Definition: Decl.h:530
This object can be modified without requiring retains or releases.
Definition: Type.h:137
bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor)
isTypeConstant - Determine whether an object of this type can be emitted as a constant.
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource AlignSource=AlignmentSource::Type)
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:539
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition: CGDecl.cpp:942
const CodeGen::CGBlockInfo * BlockInfo
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:980
RValue - This trivial value class is used to represent the result of an expression that is evaluated...
Definition: CGValue.h:38
void setAddress(Address address)
Definition: CGValue.h:332
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
std::vector< bool > & Stack
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
i8* @objc_storeWeak(i8** addr, i8* value) Returns value.
Definition: CGObjC.cpp:2214
void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, bool PerformInit)
Emit code in this function to perform a guarded variable initialization.
Definition: CGDeclCXX.cpp:237
static TypeEvaluationKind getEvaluationKind(QualType T)
hasAggregateLLVMType - Return true if the specified AST type will map into an aggregate LLVM type or ...
llvm::Value * getPointer() const
Definition: Address.h:38
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:155
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3369
ValueDecl - Represent the declaration of a variable (in which case it is an lvalue) a function (in wh...
Definition: Decl.h:521
Expr - This represents one expression.
Definition: Expr.h:104
void EmitARCMoveWeak(Address dst, Address src)
void @objc_moveWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2261
void EmitAutoVarInit(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1166
static Address invalid()
Definition: Address.h:35
CK_BitCast - A conversion which causes a bit pattern of one type to be reinterpreted as a bit pattern...
void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV)
Add global annotations that are set on D, for the global GV.
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited...
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource AlignSource=AlignmentSource::Type, llvm::MDNode *TBAAInfo=nullptr, bool isInit=false, QualType TBAABaseTy=QualType(), uint64_t TBAAOffset=0, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
Definition: CGExpr.cpp:1348
bool isAtomicType() const
Definition: Type.h:5387
virtual void EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, const VarDecl &D)
Emit the IR required for a work-group-local variable declaration, and add an entry to CGF's LocalDecl...
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4580
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2345
Kind getKind() const
Definition: DeclBase.h:387
DeclContext * getDeclContext()
Definition: DeclBase.h:393
ASTContext & getContext() const
ImplicitParamDecl * getSelfDecl() const
Definition: DeclObjC.h:411
void add(RValue rvalue, QualType type, bool needscopy=false)
Definition: CGCall.h:81
Base object dtor.
Definition: ABI.h:37
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant)
Returns LLVM linkage for a declarator.
bool isExceptionVariable() const
Determine whether this variable is the exception variable in a C++ catch statememt or an Objective-C ...
Definition: Decl.h:1140
bool isExternallyVisible() const
Definition: Decl.h:280
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:294
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, QualType elementType, CharUnits elementAlign, Destroyer *destroyer, bool checkZeroLength, bool useEHCleanup)
emitArrayDestroy - Destroys all the elements of the given array, beginning from last to first...
Definition: CGDecl.cpp:1535
EHCleanupScope(bool isNormal, bool isEH, bool isActive, unsigned cleanupSize, unsigned fixupDepth, EHScopeStack::stable_iterator enclosingNormal, EHScopeStack::stable_iterator enclosingEH)
Definition: CGCleanup.h:287
static bool hasNontrivialDestruction(QualType T)
hasNontrivialDestruction - Determine whether a type's destruction is non-trivial. ...
Definition: CGDecl.cpp:273
static bool isCapturedBy(const VarDecl &var, const Expr *e)
Determines whether the given __block variable is potentially captured by the given expression...
Definition: CGDecl.cpp:1104
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:28
The l-value was considered opaque, so the alignment was determined from a type.
void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, llvm::Value *ptr)
Definition: CGBlocks.cpp:1098
void enterByrefCleanup(const AutoVarEmission &emission)
Enter a cleanup to destroy a __block variable.
Definition: CGBlocks.cpp:2276
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:177
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
There is no lifetime qualification on this type.
Definition: Type.h:133
Address CreateBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Definition: CGBuilder.h:168
Assigning into this object requires the old value to be released and the new value to be retained...
Definition: Type.h:144
llvm::GlobalVariable * AddInitializerToStaticVarDecl(const VarDecl &D, llvm::GlobalVariable *GV)
AddInitializerToStaticVarDecl - Add the initializer for 'D' to the global variable that has already b...
Definition: CGDecl.cpp:283
Kind
ASTContext & getContext() const
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushDestroy - Push the standard destructor for the given type as at least a normal cleanup...
Definition: CGDecl.cpp:1445
Encodes a location in the source.
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go...
body_range body()
Definition: Stmt.h:569
void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
Definition: CGObjC.cpp:2036
LValue EmitDeclRefLValue(const DeclRefExpr *E)
Definition: CGExpr.cpp:2033
static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D)
Definition: CGDecl.cpp:154
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource AlignSource=AlignmentSource::Type, llvm::MDNode *TBAAInfo=nullptr, QualType TBAABaseTy=QualType(), uint64_t TBAAOffset=0, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
Definition: CGExpr.cpp:1236
bool isConstant(ASTContext &Ctx) const
Definition: Type.h:712
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6.7.5p3.
Definition: Type.cpp:1876
bool isLocalVarDecl() const
isLocalVarDecl - Returns true for local variable declarations other than parameters.
Definition: Decl.h:955
static void emitPartialArrayDestroy(CodeGenFunction &CGF, llvm::Value *begin, llvm::Value *end, QualType type, CharUnits elementAlign, CodeGenFunction::Destroyer *destroyer)
Perform partial array destruction as if in an EH cleanup.
Definition: CGDecl.cpp:1588
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
BuildByrefInfo - This routine changes a __block variable declared as T x into:
Definition: CGBlocks.cpp:2055
llvm::Value * EmitLifetimeStart(uint64_t Size, llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition: CGDecl.cpp:913
static bool isAccessedBy(const VarDecl &var, const Stmt *s)
Definition: CGDecl.cpp:569
bool isTrivialInitializer(const Expr *Init)
Determine whether the given initializer is trivial in the sense that it requires no code to be genera...
Definition: CGDecl.cpp:1153
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:190
const CodeGenOptions & getCodeGenOpts() const
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition: Type.h:5640
An aligned address.
Definition: Address.h:25
const LangOptions & getLangOpts() const
static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var, Address addr, Qualifiers::ObjCLifetime lifetime)
EmitAutoVarWithLifetime - Does the setup required for an automatic variable with lifetime.
Definition: CGDecl.cpp:533
Complete object dtor.
Definition: ABI.h:36
void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo)
EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Definition: CGDecl.cpp:1738
Assigning into this object requires a lifetime extension.
Definition: Type.h:150
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:3358
llvm::Value * EmitARCRetain(QualType type, llvm::Value *value)
Produce the code to do a retain.
Definition: CGObjC.cpp:1939
llvm::Constant * getLLVMLifetimeEndFn()
Lazily declare the .lifetime.end intrinsic.
Definition: CGDecl.cpp:1710
void enterFullExpression(const ExprWithCleanups *E)
void EmitDecl(const Decl &D)
EmitDecl - Emit a declaration.
Definition: CGDecl.cpp:36
static Destroyer destroyARCStrongImprecise
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:193
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type, returning the result.
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:2526
QualType getType() const
Definition: Expr.h:125
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
CGFunctionInfo - Class to encapsulate the information about a function definition.
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:67
This class organizes the cross-function state that is used while generating LLVM code.
static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction &CGF, SourceLocation TemporaryLocation)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:569
Address getObjectAddress(CodeGenFunction &CGF) const
Returns the address of the object within this declaration.
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This)
Definition: CGClass.cpp:2269
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1121
Address CreateMemTemp(QualType T, const Twine &Name="tmp")
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignment...
Definition: CGExpr.cpp:97
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1308
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchers.h:1723
const CGFunctionInfo & arrangeFunctionDeclaration(const FunctionDecl *FD)
Arrange the argument and result information for the declaration or definition of the given function...
Definition: CGCall.cpp:287
void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const
Set the TLS mode for the given LLVM GlobalValue for the thread-local variable declaration D...
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:78
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks, lambdas, etc.
Definition: DeclBase.cpp:815
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
CK_BlockPointerToObjCPointerCast - Casting a block pointer to an ObjC pointer.
detail::InMemoryDirectory::const_iterator E
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:121
unsigned GetGlobalVarAddressSpace(const VarDecl *D, unsigned AddrSpace)
Return the address space of the underlying global variable for D, as determined by its declaration...
static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, const LValue &destLV, const Expr *init)
Definition: CGDecl.cpp:601
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type. ...
Definition: CGExprAgg.cpp:1401
bool isConstantInitializer(ASTContext &Ctx, bool ForRef, const Expr **Culprit=nullptr) const
isConstantInitializer - Returns true if this expression can be emitted to IR as a constant...
Definition: Expr.cpp:2752
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1370
llvm::PointerType * getType() const
Return the type of the pointer value.
Definition: Address.h:44
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:3544
bool TypeAlias
Whether this template specialization type is a substituted type alias.
Definition: Type.h:4005
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:5675
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
llvm::LoadInst * CreateFlagLoad(llvm::Value *Addr, const llvm::Twine &Name="")
Emit a load from an i1 flag variable.
Definition: CGBuilder.h:155
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:52
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition: CGDecl.cpp:658
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO)...
Definition: Decl.h:1158
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1522
llvm::Value * EmitARCRetainAutorelease(QualType type, llvm::Value *value)
Do a fused retain/autorelease of the given object.
Definition: CGObjC.cpp:2174
StringRef getMangledName(GlobalDecl GD)
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:33
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:367
void EmitARCInitWeak(Address addr, llvm::Value *value)
i8* @objc_initWeak(i8** addr, i8* value) Returns value.
Definition: CGObjC.cpp:2226
ARCPreciseLifetime_t
Does an ARC strong l-value have precise lifetime?
Definition: CGValue.h:119
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type, returning the result.
llvm::Constant * EmitConstantInit(const VarDecl &D, CodeGenFunction *CGF=nullptr)
Try to emit the initializer for the given declaration as a constant; returns 0 if the expression cann...
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1024
Reading or writing from this object requires a barrier call.
Definition: Type.h:147
bool isPODType(ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition: Type.cpp:1961
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang's AST.
Definition: ASTMatchers.h:1695
llvm::DenseMap< const VarDecl *, llvm::Value * > NRVOFlags
A mapping from NRVO variables to the flags used to indicate when the NRVO has been applied to this va...
static void emitStoresForInitAfterMemset(llvm::Constant *Init, llvm::Value *Loc, bool isVolatile, CGBuilderTy &Builder)
emitStoresForInitAfterMemset - For inits that canEmitInitWithFewStoresAfterMemset returned true for...
Definition: CGDecl.cpp:840
bool isARCPseudoStrong() const
Determine whether this variable is an ARC pseudo-__strong variable.
Definition: Decl.h:1180
Represents a C++ struct/union/class.
Definition: DeclCXX.h:285
BoundNodesTreeBuilder *const Builder
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
bool isObjCObjectPointerType() const
Definition: Type.h:5377
llvm::Type * ConvertType(QualType T)
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression...
Definition: CGExpr.cpp:944
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
void pushStackRestore(CleanupKind kind, Address SPMem)
Definition: CGDecl.cpp:1461
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition: Decl.h:1189
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:43
bool isArrayType() const
Definition: Type.h:5344
std::pair< llvm::Value *, QualType > getVLASize(const VariableArrayType *vla)
getVLASize - Returns an LLVM value that corresponds to the size, in non-variably-sized elements...
Defines the clang::TargetInfo interface.
QualType getType() const
Definition: CGValue.h:258
void setLocation(SourceLocation Loc)
Update the current source location.
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.h:2180
CK_NoOp - A conversion which does not affect the type other than (possibly) adding qualifiers...
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:922
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition: Expr.h:400
static RValue get(llvm::Value *V)
Definition: CGValue.h:85
void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr)
Definition: CGDecl.cpp:932
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:106
SourceLocation getLocation() const
Definition: DeclBase.h:384
LValue - This represents an lvalue references.
Definition: CGValue.h:152
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:144
void EmitVarAnnotations(const VarDecl *D, llvm::Value *V)
Emit local annotations for the local variable V, declared by D.
SanitizerMetadata * getSanitizerMetadata()
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it's a VLA, and drill down to the base elem...
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, bool IsForDefinition=false)
Return the address of the given function.
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:56
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:586
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, bool followForward=true)
BuildBlockByrefAddress - Computes the location of the data in a variable which is declared as __block...
Definition: CGBlocks.cpp:2018
void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, Address arrayEndPointer, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy already-constructed elements of the ...
Definition: CGDecl.cpp:1673
bool hasLocalStorage() const
hasLocalStorage - Returns true if a variable with function scope is a non-static local variable...
Definition: Decl.h:891
Expr * IgnoreParens() LLVM_READONLY
IgnoreParens - Ignore parentheses.
Definition: Expr.cpp:2433
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:5116
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)