diff options
Diffstat (limited to 'clang/lib/Sema/SemaLambda.cpp')
-rw-r--r-- | clang/lib/Sema/SemaLambda.cpp | 96 |
1 files changed, 38 insertions, 58 deletions
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index a8f109df284..80d497f4e50 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -699,18 +699,11 @@ void Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) { } } -QualType Sema::performLambdaInitCaptureInitialization(SourceLocation Loc, - bool ByRef, - IdentifierInfo *Id, - Expr *&Init) { - - // We do not need to distinguish between direct-list-initialization - // and copy-list-initialization here, because we will always deduce - // std::initializer_list<T>, and direct- and copy-list-initialization - // always behave the same for such a type. - // FIXME: We should model whether an '=' was present. - const bool IsDirectInit = isa<ParenListExpr>(Init) || isa<InitListExpr>(Init); - +QualType Sema::buildLambdaInitCaptureInitialization(SourceLocation Loc, + bool ByRef, + IdentifierInfo *Id, + bool IsDirectInit, + Expr *&Init) { // Create an 'auto' or 'auto&' TypeSourceInfo that we can use to // deduce against. QualType DeductType = Context.getAutoDeductType(); @@ -723,50 +716,16 @@ QualType Sema::performLambdaInitCaptureInitialization(SourceLocation Loc, } TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType); - // Are we a non-list direct initialization? - ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); - - Expr *DeduceInit = Init; - // Initializer could be a C++ direct-initializer. Deduction only works if it - // contains exactly one expression. - if (CXXDirectInit) { - if (CXXDirectInit->getNumExprs() == 0) { - Diag(CXXDirectInit->getLocStart(), diag::err_init_capture_no_expression) - << DeclarationName(Id) << TSI->getType() << Loc; - return QualType(); - } else if (CXXDirectInit->getNumExprs() > 1) { - Diag(CXXDirectInit->getExpr(1)->getLocStart(), - diag::err_init_capture_multiple_expressions) - << DeclarationName(Id) << TSI->getType() << Loc; - return QualType(); - } else { - DeduceInit = CXXDirectInit->getExpr(0); - if (isa<InitListExpr>(DeduceInit)) - Diag(CXXDirectInit->getLocStart(), diag::err_init_capture_paren_braces) - << DeclarationName(Id) << Loc; - } - } - - // Now deduce against the initialization expression and store the deduced - // type below. - QualType DeducedType; - if (DeduceAutoType(TSI, DeduceInit, DeducedType) == DAR_Failed) { - if (isa<InitListExpr>(Init)) - Diag(Loc, diag::err_init_capture_deduction_failure_from_init_list) - << DeclarationName(Id) - << (DeduceInit->getType().isNull() ? TSI->getType() - : DeduceInit->getType()) - << DeduceInit->getSourceRange(); - else - Diag(Loc, diag::err_init_capture_deduction_failure) - << DeclarationName(Id) << TSI->getType() - << (DeduceInit->getType().isNull() ? TSI->getType() - : DeduceInit->getType()) - << DeduceInit->getSourceRange(); - } + // Deduce the type of the init capture. + QualType DeducedType = deduceVarTypeFromInitializer( + /*VarDecl*/nullptr, DeclarationName(Id), DeductType, TSI, + SourceRange(Loc, Loc), IsDirectInit, Init); if (DeducedType.isNull()) return QualType(); + // Are we a non-list direct initialization? + ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); + // Perform initialization analysis and ensure any implicit conversions // (such as lvalue-to-rvalue) are enforced. InitializedEntity Entity = @@ -803,9 +762,10 @@ QualType Sema::performLambdaInitCaptureInitialization(SourceLocation Loc, return DeducedType; } -VarDecl *Sema::createLambdaInitCaptureVarDecl(SourceLocation Loc, - QualType InitCaptureType, IdentifierInfo *Id, Expr *Init) { - +VarDecl *Sema::createLambdaInitCaptureVarDecl(SourceLocation Loc, + QualType InitCaptureType, + IdentifierInfo *Id, + unsigned InitStyle, Expr *Init) { TypeSourceInfo *TSI = Context.getTrivialTypeSourceInfo(InitCaptureType, Loc); // Create a dummy variable representing the init-capture. This is not actually @@ -816,6 +776,8 @@ VarDecl *Sema::createLambdaInitCaptureVarDecl(SourceLocation Loc, Loc, Id, InitCaptureType, TSI, SC_Auto); NewVD->setInitCapture(true); NewVD->setReferenced(true); + // FIXME: Pass in a VarDecl::InitializationStyle. + NewVD->setInitStyle(static_cast<VarDecl::InitializationStyle>(InitStyle)); NewVD->markUsed(Context); NewVD->setInit(Init); return NewVD; @@ -1014,8 +976,23 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, // in this case. if (C->InitCaptureType.get().isNull()) continue; - Var = createLambdaInitCaptureVarDecl(C->Loc, C->InitCaptureType.get(), - C->Id, C->Init.get()); + + unsigned InitStyle; + switch (C->InitKind) { + case LambdaCaptureInitKind::NoInit: + llvm_unreachable("not an init-capture?"); + case LambdaCaptureInitKind::CopyInit: + InitStyle = VarDecl::CInit; + break; + case LambdaCaptureInitKind::DirectInit: + InitStyle = VarDecl::CallInit; + break; + case LambdaCaptureInitKind::ListInit: + InitStyle = VarDecl::ListInit; + break; + } + Var = createLambdaInitCaptureVarDecl(C->Loc, C->InitCaptureType.get(), + C->Id, InitStyle, C->Init.get()); // C++1y [expr.prim.lambda]p11: // An init-capture behaves as if it declares and explicitly // captures a variable [...] whose declarative region is the @@ -1023,6 +1000,9 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, if (Var) PushOnScopeChains(Var, CurScope, false); } else { + assert(C->InitKind == LambdaCaptureInitKind::NoInit && + "init capture has valid but null init?"); + // C++11 [expr.prim.lambda]p8: // If a lambda-capture includes a capture-default that is &, the // identifiers in the lambda-capture shall not be preceded by &. |