bool refersToEnclosingScope =
(CurContext != D->getDeclContext() &&
- D->getDeclContext()->isFunctionOrMethod());
+ D->getDeclContext()->isFunctionOrMethod()) ||
+ (isa<VarDecl>(D) &&
+ cast<VarDecl>(D)->isInitCapture());
DeclRefExpr *E;
if (isa<VarTemplateSpecializationDecl>(D)) {
assert(var->hasLocalStorage() && "capture added 'const' to non-local?");
// Decide whether the first capture was for a block or a lambda.
- DeclContext *DC = S.CurContext;
- while (DC->getParent() != var->getDeclContext())
+ DeclContext *DC = S.CurContext, *Prev = 0;
+ while (DC != var->getDeclContext()) {
+ Prev = DC;
DC = DC->getParent();
+ }
+ // Unless we have an init-capture, we've gone one step too far.
+ if (!var->isInitCapture())
+ DC = Prev;
return (isa<BlockDecl>(DC) ? NCCK_Block : NCCK_Lambda);
}
bool Nested = false;
DeclContext *DC = CurContext;
- if (Var->getDeclContext() == DC) return true;
+ if (!Var->isInitCapture() && Var->getDeclContext() == DC) return true;
if (!Var->hasLocalStorage()) return true;
bool HasBlocksAttr = Var->hasAttr<BlocksAttr>();
// used as a variable, and only exists as a way to name and refer to the
// init-capture.
// FIXME: Pass in separate source locations for '&' and identifier.
- VarDecl *NewVD = VarDecl::Create(Context, CurContext->getLexicalParent(), Loc,
+ VarDecl *NewVD = VarDecl::Create(Context, CurContext, Loc,
Loc, Id, TSI->getType(), TSI, SC_Auto);
NewVD->setInitCapture(true);
NewVD->setReferenced(true);
} ();
}
+// Ensure we can emit code for init-captures in global lambdas too.
+auto global_lambda = [a = 0] () mutable { return ++a; };
+int get_incremented() { return global_lambda(); }
+
// CHECK-LABEL: define internal void @"_ZZ1fvEN3$_0D2Ev"(
// CHECK: call void @_ZN1SD1Ev(