1*f4a2713aSLionel Sambuc //===--- SemaLambda.cpp - Semantic Analysis for C++11 Lambdas -------------===// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements semantic analysis for C++ lambda expressions. 11*f4a2713aSLionel Sambuc // 12*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13*f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h" 14*f4a2713aSLionel Sambuc #include "clang/AST/ASTLambda.h" 15*f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h" 16*f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h" 17*f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h" 18*f4a2713aSLionel Sambuc #include "clang/Sema/Initialization.h" 19*f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h" 20*f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h" 21*f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h" 22*f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h" 23*f4a2713aSLionel Sambuc #include "clang/Sema/SemaLambda.h" 24*f4a2713aSLionel Sambuc #include "TypeLocBuilder.h" 25*f4a2713aSLionel Sambuc using namespace clang; 26*f4a2713aSLionel Sambuc using namespace sema; 27*f4a2713aSLionel Sambuc 28*f4a2713aSLionel Sambuc // returns -1 if none of the lambdas on the scope stack can capture. 29*f4a2713aSLionel Sambuc // A lambda 'L' is capture-ready for a certain variable 'V' if, 30*f4a2713aSLionel Sambuc // - its enclosing context is non-dependent 31*f4a2713aSLionel Sambuc // - and if the chain of lambdas between L and the lambda in which 32*f4a2713aSLionel Sambuc // V is potentially used, call all capture or have captured V. 33*f4a2713aSLionel Sambuc static inline int GetScopeIndexOfNearestCaptureReadyLambda( 34*f4a2713aSLionel Sambuc ArrayRef<clang::sema::FunctionScopeInfo*> FunctionScopes, 35*f4a2713aSLionel Sambuc DeclContext *const CurContext, VarDecl *VD) { 36*f4a2713aSLionel Sambuc 37*f4a2713aSLionel Sambuc DeclContext *EnclosingDC = CurContext; 38*f4a2713aSLionel Sambuc // If VD is null, we are attempting to capture 'this' 39*f4a2713aSLionel Sambuc const bool IsCapturingThis = !VD; 40*f4a2713aSLionel Sambuc const bool IsCapturingVariable = !IsCapturingThis; 41*f4a2713aSLionel Sambuc int RetIndex = -1; 42*f4a2713aSLionel Sambuc unsigned CurScopeIndex = FunctionScopes.size() - 1; 43*f4a2713aSLionel Sambuc while (!EnclosingDC->isTranslationUnit() && 44*f4a2713aSLionel Sambuc EnclosingDC->isDependentContext() && isLambdaCallOperator(EnclosingDC)) { 45*f4a2713aSLionel Sambuc RetIndex = CurScopeIndex; 46*f4a2713aSLionel Sambuc clang::sema::LambdaScopeInfo *LSI = 47*f4a2713aSLionel Sambuc cast<sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex]); 48*f4a2713aSLionel Sambuc // We have crawled up to an intervening lambda that contains the 49*f4a2713aSLionel Sambuc // variable declaration - so not only does it not need to capture; 50*f4a2713aSLionel Sambuc // none of the enclosing lambdas need to capture it, and since all 51*f4a2713aSLionel Sambuc // other nested lambdas are dependent (otherwise we wouldn't have 52*f4a2713aSLionel Sambuc // arrived here) - we don't yet have a lambda that can capture the 53*f4a2713aSLionel Sambuc // variable. 54*f4a2713aSLionel Sambuc if (IsCapturingVariable && VD->getDeclContext()->Equals(EnclosingDC)) 55*f4a2713aSLionel Sambuc return -1; 56*f4a2713aSLionel Sambuc // All intervening lambda call operators have to be able to capture. 57*f4a2713aSLionel Sambuc // If they do not have a default implicit capture, check to see 58*f4a2713aSLionel Sambuc // if the entity has already been explicitly captured. 59*f4a2713aSLionel Sambuc // If even a single dependent enclosing lambda lacks the capability 60*f4a2713aSLionel Sambuc // to ever capture this variable, there is no further enclosing 61*f4a2713aSLionel Sambuc // non-dependent lambda that can capture this variable. 62*f4a2713aSLionel Sambuc if (LSI->ImpCaptureStyle == sema::LambdaScopeInfo::ImpCap_None) { 63*f4a2713aSLionel Sambuc if (IsCapturingVariable && !LSI->isCaptured(VD)) 64*f4a2713aSLionel Sambuc return -1; 65*f4a2713aSLionel Sambuc if (IsCapturingThis && !LSI->isCXXThisCaptured()) 66*f4a2713aSLionel Sambuc return -1; 67*f4a2713aSLionel Sambuc } 68*f4a2713aSLionel Sambuc EnclosingDC = getLambdaAwareParentOfDeclContext(EnclosingDC); 69*f4a2713aSLionel Sambuc --CurScopeIndex; 70*f4a2713aSLionel Sambuc } 71*f4a2713aSLionel Sambuc // If the enclosingDC is not dependent, then the immediately nested lambda 72*f4a2713aSLionel Sambuc // is capture-ready. 73*f4a2713aSLionel Sambuc if (!EnclosingDC->isDependentContext()) 74*f4a2713aSLionel Sambuc return RetIndex; 75*f4a2713aSLionel Sambuc return -1; 76*f4a2713aSLionel Sambuc } 77*f4a2713aSLionel Sambuc // Given a lambda's call operator and a variable (or null for 'this'), 78*f4a2713aSLionel Sambuc // compute the nearest enclosing lambda that is capture-ready (i.e 79*f4a2713aSLionel Sambuc // the enclosing context is not dependent, and all intervening lambdas can 80*f4a2713aSLionel Sambuc // either implicitly or explicitly capture Var) 81*f4a2713aSLionel Sambuc // 82*f4a2713aSLionel Sambuc // The approach is as follows, for the entity VD ('this' if null): 83*f4a2713aSLionel Sambuc // - start with the current lambda 84*f4a2713aSLionel Sambuc // - if it is non-dependent and can capture VD, return it. 85*f4a2713aSLionel Sambuc // - if it is dependent and has an implicit or explicit capture, check its parent 86*f4a2713aSLionel Sambuc // whether the parent is non-depdendent and all its intervening lambdas 87*f4a2713aSLionel Sambuc // can capture, if so return the child. 88*f4a2713aSLionel Sambuc // [Note: When we hit a generic lambda specialization, do not climb up 89*f4a2713aSLionel Sambuc // the scope stack any further since not only do we not need to, 90*f4a2713aSLionel Sambuc // the scope stack will often not be synchronized with any lambdas 91*f4a2713aSLionel Sambuc // enclosing the specialized generic lambda] 92*f4a2713aSLionel Sambuc // 93*f4a2713aSLionel Sambuc // Return the CallOperator of the capturable lambda and set function scope 94*f4a2713aSLionel Sambuc // index to the correct index within the function scope stack to correspond 95*f4a2713aSLionel Sambuc // to the capturable lambda. 96*f4a2713aSLionel Sambuc // If VarDecl *VD is null, we check for 'this' capture. 97*f4a2713aSLionel Sambuc CXXMethodDecl* clang::GetInnermostEnclosingCapturableLambda( 98*f4a2713aSLionel Sambuc ArrayRef<sema::FunctionScopeInfo*> FunctionScopes, 99*f4a2713aSLionel Sambuc unsigned &FunctionScopeIndex, 100*f4a2713aSLionel Sambuc DeclContext *const CurContext, VarDecl *VD, 101*f4a2713aSLionel Sambuc Sema &S) { 102*f4a2713aSLionel Sambuc 103*f4a2713aSLionel Sambuc const int IndexOfCaptureReadyLambda = 104*f4a2713aSLionel Sambuc GetScopeIndexOfNearestCaptureReadyLambda(FunctionScopes,CurContext, VD); 105*f4a2713aSLionel Sambuc if (IndexOfCaptureReadyLambda == -1) return 0; 106*f4a2713aSLionel Sambuc assert(IndexOfCaptureReadyLambda >= 0); 107*f4a2713aSLionel Sambuc const unsigned IndexOfCaptureReadyLambdaU = 108*f4a2713aSLionel Sambuc static_cast<unsigned>(IndexOfCaptureReadyLambda); 109*f4a2713aSLionel Sambuc sema::LambdaScopeInfo *const CaptureReadyLambdaLSI = 110*f4a2713aSLionel Sambuc cast<sema::LambdaScopeInfo>(FunctionScopes[IndexOfCaptureReadyLambdaU]); 111*f4a2713aSLionel Sambuc // If VD is null, we are attempting to capture 'this' 112*f4a2713aSLionel Sambuc const bool IsCapturingThis = !VD; 113*f4a2713aSLionel Sambuc const bool IsCapturingVariable = !IsCapturingThis; 114*f4a2713aSLionel Sambuc 115*f4a2713aSLionel Sambuc if (IsCapturingVariable) { 116*f4a2713aSLionel Sambuc // Now check to see if this lambda can truly capture, and also 117*f4a2713aSLionel Sambuc // if all enclosing lambdas of this lambda allow this capture. 118*f4a2713aSLionel Sambuc QualType CaptureType, DeclRefType; 119*f4a2713aSLionel Sambuc const bool CanCaptureVariable = !S.tryCaptureVariable(VD, 120*f4a2713aSLionel Sambuc /*ExprVarIsUsedInLoc*/SourceLocation(), clang::Sema::TryCapture_Implicit, 121*f4a2713aSLionel Sambuc /*EllipsisLoc*/ SourceLocation(), 122*f4a2713aSLionel Sambuc /*BuildAndDiagnose*/false, CaptureType, DeclRefType, 123*f4a2713aSLionel Sambuc &IndexOfCaptureReadyLambdaU); 124*f4a2713aSLionel Sambuc if (!CanCaptureVariable) return 0; 125*f4a2713aSLionel Sambuc } else { 126*f4a2713aSLionel Sambuc const bool CanCaptureThis = !S.CheckCXXThisCapture( 127*f4a2713aSLionel Sambuc CaptureReadyLambdaLSI->PotentialThisCaptureLocation, false, false, 128*f4a2713aSLionel Sambuc &IndexOfCaptureReadyLambdaU); 129*f4a2713aSLionel Sambuc if (!CanCaptureThis) return 0; 130*f4a2713aSLionel Sambuc } // end 'this' capture test 131*f4a2713aSLionel Sambuc FunctionScopeIndex = IndexOfCaptureReadyLambdaU; 132*f4a2713aSLionel Sambuc return CaptureReadyLambdaLSI->CallOperator; 133*f4a2713aSLionel Sambuc } 134*f4a2713aSLionel Sambuc 135*f4a2713aSLionel Sambuc static inline TemplateParameterList * 136*f4a2713aSLionel Sambuc getGenericLambdaTemplateParameterList(LambdaScopeInfo *LSI, Sema &SemaRef) { 137*f4a2713aSLionel Sambuc if (LSI->GLTemplateParameterList) 138*f4a2713aSLionel Sambuc return LSI->GLTemplateParameterList; 139*f4a2713aSLionel Sambuc 140*f4a2713aSLionel Sambuc if (LSI->AutoTemplateParams.size()) { 141*f4a2713aSLionel Sambuc SourceRange IntroRange = LSI->IntroducerRange; 142*f4a2713aSLionel Sambuc SourceLocation LAngleLoc = IntroRange.getBegin(); 143*f4a2713aSLionel Sambuc SourceLocation RAngleLoc = IntroRange.getEnd(); 144*f4a2713aSLionel Sambuc LSI->GLTemplateParameterList = TemplateParameterList::Create( 145*f4a2713aSLionel Sambuc SemaRef.Context, 146*f4a2713aSLionel Sambuc /*Template kw loc*/SourceLocation(), 147*f4a2713aSLionel Sambuc LAngleLoc, 148*f4a2713aSLionel Sambuc (NamedDecl**)LSI->AutoTemplateParams.data(), 149*f4a2713aSLionel Sambuc LSI->AutoTemplateParams.size(), RAngleLoc); 150*f4a2713aSLionel Sambuc } 151*f4a2713aSLionel Sambuc return LSI->GLTemplateParameterList; 152*f4a2713aSLionel Sambuc } 153*f4a2713aSLionel Sambuc 154*f4a2713aSLionel Sambuc 155*f4a2713aSLionel Sambuc 156*f4a2713aSLionel Sambuc CXXRecordDecl *Sema::createLambdaClosureType(SourceRange IntroducerRange, 157*f4a2713aSLionel Sambuc TypeSourceInfo *Info, 158*f4a2713aSLionel Sambuc bool KnownDependent, 159*f4a2713aSLionel Sambuc LambdaCaptureDefault CaptureDefault) { 160*f4a2713aSLionel Sambuc DeclContext *DC = CurContext; 161*f4a2713aSLionel Sambuc while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) 162*f4a2713aSLionel Sambuc DC = DC->getParent(); 163*f4a2713aSLionel Sambuc bool IsGenericLambda = getGenericLambdaTemplateParameterList(getCurLambda(), 164*f4a2713aSLionel Sambuc *this); 165*f4a2713aSLionel Sambuc // Start constructing the lambda class. 166*f4a2713aSLionel Sambuc CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(Context, DC, Info, 167*f4a2713aSLionel Sambuc IntroducerRange.getBegin(), 168*f4a2713aSLionel Sambuc KnownDependent, 169*f4a2713aSLionel Sambuc IsGenericLambda, 170*f4a2713aSLionel Sambuc CaptureDefault); 171*f4a2713aSLionel Sambuc DC->addDecl(Class); 172*f4a2713aSLionel Sambuc 173*f4a2713aSLionel Sambuc return Class; 174*f4a2713aSLionel Sambuc } 175*f4a2713aSLionel Sambuc 176*f4a2713aSLionel Sambuc /// \brief Determine whether the given context is or is enclosed in an inline 177*f4a2713aSLionel Sambuc /// function. 178*f4a2713aSLionel Sambuc static bool isInInlineFunction(const DeclContext *DC) { 179*f4a2713aSLionel Sambuc while (!DC->isFileContext()) { 180*f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) 181*f4a2713aSLionel Sambuc if (FD->isInlined()) 182*f4a2713aSLionel Sambuc return true; 183*f4a2713aSLionel Sambuc 184*f4a2713aSLionel Sambuc DC = DC->getLexicalParent(); 185*f4a2713aSLionel Sambuc } 186*f4a2713aSLionel Sambuc 187*f4a2713aSLionel Sambuc return false; 188*f4a2713aSLionel Sambuc } 189*f4a2713aSLionel Sambuc 190*f4a2713aSLionel Sambuc MangleNumberingContext * 191*f4a2713aSLionel Sambuc Sema::getCurrentMangleNumberContext(const DeclContext *DC, 192*f4a2713aSLionel Sambuc Decl *&ManglingContextDecl) { 193*f4a2713aSLionel Sambuc // Compute the context for allocating mangling numbers in the current 194*f4a2713aSLionel Sambuc // expression, if the ABI requires them. 195*f4a2713aSLionel Sambuc ManglingContextDecl = ExprEvalContexts.back().ManglingContextDecl; 196*f4a2713aSLionel Sambuc 197*f4a2713aSLionel Sambuc enum ContextKind { 198*f4a2713aSLionel Sambuc Normal, 199*f4a2713aSLionel Sambuc DefaultArgument, 200*f4a2713aSLionel Sambuc DataMember, 201*f4a2713aSLionel Sambuc StaticDataMember 202*f4a2713aSLionel Sambuc } Kind = Normal; 203*f4a2713aSLionel Sambuc 204*f4a2713aSLionel Sambuc // Default arguments of member function parameters that appear in a class 205*f4a2713aSLionel Sambuc // definition, as well as the initializers of data members, receive special 206*f4a2713aSLionel Sambuc // treatment. Identify them. 207*f4a2713aSLionel Sambuc if (ManglingContextDecl) { 208*f4a2713aSLionel Sambuc if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) { 209*f4a2713aSLionel Sambuc if (const DeclContext *LexicalDC 210*f4a2713aSLionel Sambuc = Param->getDeclContext()->getLexicalParent()) 211*f4a2713aSLionel Sambuc if (LexicalDC->isRecord()) 212*f4a2713aSLionel Sambuc Kind = DefaultArgument; 213*f4a2713aSLionel Sambuc } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) { 214*f4a2713aSLionel Sambuc if (Var->getDeclContext()->isRecord()) 215*f4a2713aSLionel Sambuc Kind = StaticDataMember; 216*f4a2713aSLionel Sambuc } else if (isa<FieldDecl>(ManglingContextDecl)) { 217*f4a2713aSLionel Sambuc Kind = DataMember; 218*f4a2713aSLionel Sambuc } 219*f4a2713aSLionel Sambuc } 220*f4a2713aSLionel Sambuc 221*f4a2713aSLionel Sambuc // Itanium ABI [5.1.7]: 222*f4a2713aSLionel Sambuc // In the following contexts [...] the one-definition rule requires closure 223*f4a2713aSLionel Sambuc // types in different translation units to "correspond": 224*f4a2713aSLionel Sambuc bool IsInNonspecializedTemplate = 225*f4a2713aSLionel Sambuc !ActiveTemplateInstantiations.empty() || CurContext->isDependentContext(); 226*f4a2713aSLionel Sambuc switch (Kind) { 227*f4a2713aSLionel Sambuc case Normal: 228*f4a2713aSLionel Sambuc // -- the bodies of non-exported nonspecialized template functions 229*f4a2713aSLionel Sambuc // -- the bodies of inline functions 230*f4a2713aSLionel Sambuc if ((IsInNonspecializedTemplate && 231*f4a2713aSLionel Sambuc !(ManglingContextDecl && isa<ParmVarDecl>(ManglingContextDecl))) || 232*f4a2713aSLionel Sambuc isInInlineFunction(CurContext)) { 233*f4a2713aSLionel Sambuc ManglingContextDecl = 0; 234*f4a2713aSLionel Sambuc return &Context.getManglingNumberContext(DC); 235*f4a2713aSLionel Sambuc } 236*f4a2713aSLionel Sambuc 237*f4a2713aSLionel Sambuc ManglingContextDecl = 0; 238*f4a2713aSLionel Sambuc return 0; 239*f4a2713aSLionel Sambuc 240*f4a2713aSLionel Sambuc case StaticDataMember: 241*f4a2713aSLionel Sambuc // -- the initializers of nonspecialized static members of template classes 242*f4a2713aSLionel Sambuc if (!IsInNonspecializedTemplate) { 243*f4a2713aSLionel Sambuc ManglingContextDecl = 0; 244*f4a2713aSLionel Sambuc return 0; 245*f4a2713aSLionel Sambuc } 246*f4a2713aSLionel Sambuc // Fall through to get the current context. 247*f4a2713aSLionel Sambuc 248*f4a2713aSLionel Sambuc case DataMember: 249*f4a2713aSLionel Sambuc // -- the in-class initializers of class members 250*f4a2713aSLionel Sambuc case DefaultArgument: 251*f4a2713aSLionel Sambuc // -- default arguments appearing in class definitions 252*f4a2713aSLionel Sambuc return &ExprEvalContexts.back().getMangleNumberingContext(Context); 253*f4a2713aSLionel Sambuc } 254*f4a2713aSLionel Sambuc 255*f4a2713aSLionel Sambuc llvm_unreachable("unexpected context"); 256*f4a2713aSLionel Sambuc } 257*f4a2713aSLionel Sambuc 258*f4a2713aSLionel Sambuc MangleNumberingContext & 259*f4a2713aSLionel Sambuc Sema::ExpressionEvaluationContextRecord::getMangleNumberingContext( 260*f4a2713aSLionel Sambuc ASTContext &Ctx) { 261*f4a2713aSLionel Sambuc assert(ManglingContextDecl && "Need to have a context declaration"); 262*f4a2713aSLionel Sambuc if (!MangleNumbering) 263*f4a2713aSLionel Sambuc MangleNumbering = Ctx.createMangleNumberingContext(); 264*f4a2713aSLionel Sambuc return *MangleNumbering; 265*f4a2713aSLionel Sambuc } 266*f4a2713aSLionel Sambuc 267*f4a2713aSLionel Sambuc CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, 268*f4a2713aSLionel Sambuc SourceRange IntroducerRange, 269*f4a2713aSLionel Sambuc TypeSourceInfo *MethodTypeInfo, 270*f4a2713aSLionel Sambuc SourceLocation EndLoc, 271*f4a2713aSLionel Sambuc ArrayRef<ParmVarDecl *> Params) { 272*f4a2713aSLionel Sambuc QualType MethodType = MethodTypeInfo->getType(); 273*f4a2713aSLionel Sambuc TemplateParameterList *TemplateParams = 274*f4a2713aSLionel Sambuc getGenericLambdaTemplateParameterList(getCurLambda(), *this); 275*f4a2713aSLionel Sambuc // If a lambda appears in a dependent context or is a generic lambda (has 276*f4a2713aSLionel Sambuc // template parameters) and has an 'auto' return type, deduce it to a 277*f4a2713aSLionel Sambuc // dependent type. 278*f4a2713aSLionel Sambuc if (Class->isDependentContext() || TemplateParams) { 279*f4a2713aSLionel Sambuc const FunctionProtoType *FPT = MethodType->castAs<FunctionProtoType>(); 280*f4a2713aSLionel Sambuc QualType Result = FPT->getResultType(); 281*f4a2713aSLionel Sambuc if (Result->isUndeducedType()) { 282*f4a2713aSLionel Sambuc Result = SubstAutoType(Result, Context.DependentTy); 283*f4a2713aSLionel Sambuc MethodType = Context.getFunctionType(Result, FPT->getArgTypes(), 284*f4a2713aSLionel Sambuc FPT->getExtProtoInfo()); 285*f4a2713aSLionel Sambuc } 286*f4a2713aSLionel Sambuc } 287*f4a2713aSLionel Sambuc 288*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p5: 289*f4a2713aSLionel Sambuc // The closure type for a lambda-expression has a public inline function 290*f4a2713aSLionel Sambuc // call operator (13.5.4) whose parameters and return type are described by 291*f4a2713aSLionel Sambuc // the lambda-expression's parameter-declaration-clause and 292*f4a2713aSLionel Sambuc // trailing-return-type respectively. 293*f4a2713aSLionel Sambuc DeclarationName MethodName 294*f4a2713aSLionel Sambuc = Context.DeclarationNames.getCXXOperatorName(OO_Call); 295*f4a2713aSLionel Sambuc DeclarationNameLoc MethodNameLoc; 296*f4a2713aSLionel Sambuc MethodNameLoc.CXXOperatorName.BeginOpNameLoc 297*f4a2713aSLionel Sambuc = IntroducerRange.getBegin().getRawEncoding(); 298*f4a2713aSLionel Sambuc MethodNameLoc.CXXOperatorName.EndOpNameLoc 299*f4a2713aSLionel Sambuc = IntroducerRange.getEnd().getRawEncoding(); 300*f4a2713aSLionel Sambuc CXXMethodDecl *Method 301*f4a2713aSLionel Sambuc = CXXMethodDecl::Create(Context, Class, EndLoc, 302*f4a2713aSLionel Sambuc DeclarationNameInfo(MethodName, 303*f4a2713aSLionel Sambuc IntroducerRange.getBegin(), 304*f4a2713aSLionel Sambuc MethodNameLoc), 305*f4a2713aSLionel Sambuc MethodType, MethodTypeInfo, 306*f4a2713aSLionel Sambuc SC_None, 307*f4a2713aSLionel Sambuc /*isInline=*/true, 308*f4a2713aSLionel Sambuc /*isConstExpr=*/false, 309*f4a2713aSLionel Sambuc EndLoc); 310*f4a2713aSLionel Sambuc Method->setAccess(AS_public); 311*f4a2713aSLionel Sambuc 312*f4a2713aSLionel Sambuc // Temporarily set the lexical declaration context to the current 313*f4a2713aSLionel Sambuc // context, so that the Scope stack matches the lexical nesting. 314*f4a2713aSLionel Sambuc Method->setLexicalDeclContext(CurContext); 315*f4a2713aSLionel Sambuc // Create a function template if we have a template parameter list 316*f4a2713aSLionel Sambuc FunctionTemplateDecl *const TemplateMethod = TemplateParams ? 317*f4a2713aSLionel Sambuc FunctionTemplateDecl::Create(Context, Class, 318*f4a2713aSLionel Sambuc Method->getLocation(), MethodName, 319*f4a2713aSLionel Sambuc TemplateParams, 320*f4a2713aSLionel Sambuc Method) : 0; 321*f4a2713aSLionel Sambuc if (TemplateMethod) { 322*f4a2713aSLionel Sambuc TemplateMethod->setLexicalDeclContext(CurContext); 323*f4a2713aSLionel Sambuc TemplateMethod->setAccess(AS_public); 324*f4a2713aSLionel Sambuc Method->setDescribedFunctionTemplate(TemplateMethod); 325*f4a2713aSLionel Sambuc } 326*f4a2713aSLionel Sambuc 327*f4a2713aSLionel Sambuc // Add parameters. 328*f4a2713aSLionel Sambuc if (!Params.empty()) { 329*f4a2713aSLionel Sambuc Method->setParams(Params); 330*f4a2713aSLionel Sambuc CheckParmsForFunctionDef(const_cast<ParmVarDecl **>(Params.begin()), 331*f4a2713aSLionel Sambuc const_cast<ParmVarDecl **>(Params.end()), 332*f4a2713aSLionel Sambuc /*CheckParameterNames=*/false); 333*f4a2713aSLionel Sambuc 334*f4a2713aSLionel Sambuc for (CXXMethodDecl::param_iterator P = Method->param_begin(), 335*f4a2713aSLionel Sambuc PEnd = Method->param_end(); 336*f4a2713aSLionel Sambuc P != PEnd; ++P) 337*f4a2713aSLionel Sambuc (*P)->setOwningFunction(Method); 338*f4a2713aSLionel Sambuc } 339*f4a2713aSLionel Sambuc 340*f4a2713aSLionel Sambuc Decl *ManglingContextDecl; 341*f4a2713aSLionel Sambuc if (MangleNumberingContext *MCtx = 342*f4a2713aSLionel Sambuc getCurrentMangleNumberContext(Class->getDeclContext(), 343*f4a2713aSLionel Sambuc ManglingContextDecl)) { 344*f4a2713aSLionel Sambuc unsigned ManglingNumber = MCtx->getManglingNumber(Method); 345*f4a2713aSLionel Sambuc Class->setLambdaMangling(ManglingNumber, ManglingContextDecl); 346*f4a2713aSLionel Sambuc } 347*f4a2713aSLionel Sambuc 348*f4a2713aSLionel Sambuc return Method; 349*f4a2713aSLionel Sambuc } 350*f4a2713aSLionel Sambuc 351*f4a2713aSLionel Sambuc void Sema::buildLambdaScope(LambdaScopeInfo *LSI, 352*f4a2713aSLionel Sambuc CXXMethodDecl *CallOperator, 353*f4a2713aSLionel Sambuc SourceRange IntroducerRange, 354*f4a2713aSLionel Sambuc LambdaCaptureDefault CaptureDefault, 355*f4a2713aSLionel Sambuc SourceLocation CaptureDefaultLoc, 356*f4a2713aSLionel Sambuc bool ExplicitParams, 357*f4a2713aSLionel Sambuc bool ExplicitResultType, 358*f4a2713aSLionel Sambuc bool Mutable) { 359*f4a2713aSLionel Sambuc LSI->CallOperator = CallOperator; 360*f4a2713aSLionel Sambuc CXXRecordDecl *LambdaClass = CallOperator->getParent(); 361*f4a2713aSLionel Sambuc LSI->Lambda = LambdaClass; 362*f4a2713aSLionel Sambuc if (CaptureDefault == LCD_ByCopy) 363*f4a2713aSLionel Sambuc LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval; 364*f4a2713aSLionel Sambuc else if (CaptureDefault == LCD_ByRef) 365*f4a2713aSLionel Sambuc LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref; 366*f4a2713aSLionel Sambuc LSI->CaptureDefaultLoc = CaptureDefaultLoc; 367*f4a2713aSLionel Sambuc LSI->IntroducerRange = IntroducerRange; 368*f4a2713aSLionel Sambuc LSI->ExplicitParams = ExplicitParams; 369*f4a2713aSLionel Sambuc LSI->Mutable = Mutable; 370*f4a2713aSLionel Sambuc 371*f4a2713aSLionel Sambuc if (ExplicitResultType) { 372*f4a2713aSLionel Sambuc LSI->ReturnType = CallOperator->getResultType(); 373*f4a2713aSLionel Sambuc 374*f4a2713aSLionel Sambuc if (!LSI->ReturnType->isDependentType() && 375*f4a2713aSLionel Sambuc !LSI->ReturnType->isVoidType()) { 376*f4a2713aSLionel Sambuc if (RequireCompleteType(CallOperator->getLocStart(), LSI->ReturnType, 377*f4a2713aSLionel Sambuc diag::err_lambda_incomplete_result)) { 378*f4a2713aSLionel Sambuc // Do nothing. 379*f4a2713aSLionel Sambuc } 380*f4a2713aSLionel Sambuc } 381*f4a2713aSLionel Sambuc } else { 382*f4a2713aSLionel Sambuc LSI->HasImplicitReturnType = true; 383*f4a2713aSLionel Sambuc } 384*f4a2713aSLionel Sambuc } 385*f4a2713aSLionel Sambuc 386*f4a2713aSLionel Sambuc void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) { 387*f4a2713aSLionel Sambuc LSI->finishedExplicitCaptures(); 388*f4a2713aSLionel Sambuc } 389*f4a2713aSLionel Sambuc 390*f4a2713aSLionel Sambuc void Sema::addLambdaParameters(CXXMethodDecl *CallOperator, Scope *CurScope) { 391*f4a2713aSLionel Sambuc // Introduce our parameters into the function scope 392*f4a2713aSLionel Sambuc for (unsigned p = 0, NumParams = CallOperator->getNumParams(); 393*f4a2713aSLionel Sambuc p < NumParams; ++p) { 394*f4a2713aSLionel Sambuc ParmVarDecl *Param = CallOperator->getParamDecl(p); 395*f4a2713aSLionel Sambuc 396*f4a2713aSLionel Sambuc // If this has an identifier, add it to the scope stack. 397*f4a2713aSLionel Sambuc if (CurScope && Param->getIdentifier()) { 398*f4a2713aSLionel Sambuc CheckShadow(CurScope, Param); 399*f4a2713aSLionel Sambuc 400*f4a2713aSLionel Sambuc PushOnScopeChains(Param, CurScope); 401*f4a2713aSLionel Sambuc } 402*f4a2713aSLionel Sambuc } 403*f4a2713aSLionel Sambuc } 404*f4a2713aSLionel Sambuc 405*f4a2713aSLionel Sambuc /// If this expression is an enumerator-like expression of some type 406*f4a2713aSLionel Sambuc /// T, return the type T; otherwise, return null. 407*f4a2713aSLionel Sambuc /// 408*f4a2713aSLionel Sambuc /// Pointer comparisons on the result here should always work because 409*f4a2713aSLionel Sambuc /// it's derived from either the parent of an EnumConstantDecl 410*f4a2713aSLionel Sambuc /// (i.e. the definition) or the declaration returned by 411*f4a2713aSLionel Sambuc /// EnumType::getDecl() (i.e. the definition). 412*f4a2713aSLionel Sambuc static EnumDecl *findEnumForBlockReturn(Expr *E) { 413*f4a2713aSLionel Sambuc // An expression is an enumerator-like expression of type T if, 414*f4a2713aSLionel Sambuc // ignoring parens and parens-like expressions: 415*f4a2713aSLionel Sambuc E = E->IgnoreParens(); 416*f4a2713aSLionel Sambuc 417*f4a2713aSLionel Sambuc // - it is an enumerator whose enum type is T or 418*f4a2713aSLionel Sambuc if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 419*f4a2713aSLionel Sambuc if (EnumConstantDecl *D 420*f4a2713aSLionel Sambuc = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { 421*f4a2713aSLionel Sambuc return cast<EnumDecl>(D->getDeclContext()); 422*f4a2713aSLionel Sambuc } 423*f4a2713aSLionel Sambuc return 0; 424*f4a2713aSLionel Sambuc } 425*f4a2713aSLionel Sambuc 426*f4a2713aSLionel Sambuc // - it is a comma expression whose RHS is an enumerator-like 427*f4a2713aSLionel Sambuc // expression of type T or 428*f4a2713aSLionel Sambuc if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 429*f4a2713aSLionel Sambuc if (BO->getOpcode() == BO_Comma) 430*f4a2713aSLionel Sambuc return findEnumForBlockReturn(BO->getRHS()); 431*f4a2713aSLionel Sambuc return 0; 432*f4a2713aSLionel Sambuc } 433*f4a2713aSLionel Sambuc 434*f4a2713aSLionel Sambuc // - it is a statement-expression whose value expression is an 435*f4a2713aSLionel Sambuc // enumerator-like expression of type T or 436*f4a2713aSLionel Sambuc if (StmtExpr *SE = dyn_cast<StmtExpr>(E)) { 437*f4a2713aSLionel Sambuc if (Expr *last = dyn_cast_or_null<Expr>(SE->getSubStmt()->body_back())) 438*f4a2713aSLionel Sambuc return findEnumForBlockReturn(last); 439*f4a2713aSLionel Sambuc return 0; 440*f4a2713aSLionel Sambuc } 441*f4a2713aSLionel Sambuc 442*f4a2713aSLionel Sambuc // - it is a ternary conditional operator (not the GNU ?: 443*f4a2713aSLionel Sambuc // extension) whose second and third operands are 444*f4a2713aSLionel Sambuc // enumerator-like expressions of type T or 445*f4a2713aSLionel Sambuc if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 446*f4a2713aSLionel Sambuc if (EnumDecl *ED = findEnumForBlockReturn(CO->getTrueExpr())) 447*f4a2713aSLionel Sambuc if (ED == findEnumForBlockReturn(CO->getFalseExpr())) 448*f4a2713aSLionel Sambuc return ED; 449*f4a2713aSLionel Sambuc return 0; 450*f4a2713aSLionel Sambuc } 451*f4a2713aSLionel Sambuc 452*f4a2713aSLionel Sambuc // (implicitly:) 453*f4a2713aSLionel Sambuc // - it is an implicit integral conversion applied to an 454*f4a2713aSLionel Sambuc // enumerator-like expression of type T or 455*f4a2713aSLionel Sambuc if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 456*f4a2713aSLionel Sambuc // We can sometimes see integral conversions in valid 457*f4a2713aSLionel Sambuc // enumerator-like expressions. 458*f4a2713aSLionel Sambuc if (ICE->getCastKind() == CK_IntegralCast) 459*f4a2713aSLionel Sambuc return findEnumForBlockReturn(ICE->getSubExpr()); 460*f4a2713aSLionel Sambuc 461*f4a2713aSLionel Sambuc // Otherwise, just rely on the type. 462*f4a2713aSLionel Sambuc } 463*f4a2713aSLionel Sambuc 464*f4a2713aSLionel Sambuc // - it is an expression of that formal enum type. 465*f4a2713aSLionel Sambuc if (const EnumType *ET = E->getType()->getAs<EnumType>()) { 466*f4a2713aSLionel Sambuc return ET->getDecl(); 467*f4a2713aSLionel Sambuc } 468*f4a2713aSLionel Sambuc 469*f4a2713aSLionel Sambuc // Otherwise, nope. 470*f4a2713aSLionel Sambuc return 0; 471*f4a2713aSLionel Sambuc } 472*f4a2713aSLionel Sambuc 473*f4a2713aSLionel Sambuc /// Attempt to find a type T for which the returned expression of the 474*f4a2713aSLionel Sambuc /// given statement is an enumerator-like expression of that type. 475*f4a2713aSLionel Sambuc static EnumDecl *findEnumForBlockReturn(ReturnStmt *ret) { 476*f4a2713aSLionel Sambuc if (Expr *retValue = ret->getRetValue()) 477*f4a2713aSLionel Sambuc return findEnumForBlockReturn(retValue); 478*f4a2713aSLionel Sambuc return 0; 479*f4a2713aSLionel Sambuc } 480*f4a2713aSLionel Sambuc 481*f4a2713aSLionel Sambuc /// Attempt to find a common type T for which all of the returned 482*f4a2713aSLionel Sambuc /// expressions in a block are enumerator-like expressions of that 483*f4a2713aSLionel Sambuc /// type. 484*f4a2713aSLionel Sambuc static EnumDecl *findCommonEnumForBlockReturns(ArrayRef<ReturnStmt*> returns) { 485*f4a2713aSLionel Sambuc ArrayRef<ReturnStmt*>::iterator i = returns.begin(), e = returns.end(); 486*f4a2713aSLionel Sambuc 487*f4a2713aSLionel Sambuc // Try to find one for the first return. 488*f4a2713aSLionel Sambuc EnumDecl *ED = findEnumForBlockReturn(*i); 489*f4a2713aSLionel Sambuc if (!ED) return 0; 490*f4a2713aSLionel Sambuc 491*f4a2713aSLionel Sambuc // Check that the rest of the returns have the same enum. 492*f4a2713aSLionel Sambuc for (++i; i != e; ++i) { 493*f4a2713aSLionel Sambuc if (findEnumForBlockReturn(*i) != ED) 494*f4a2713aSLionel Sambuc return 0; 495*f4a2713aSLionel Sambuc } 496*f4a2713aSLionel Sambuc 497*f4a2713aSLionel Sambuc // Never infer an anonymous enum type. 498*f4a2713aSLionel Sambuc if (!ED->hasNameForLinkage()) return 0; 499*f4a2713aSLionel Sambuc 500*f4a2713aSLionel Sambuc return ED; 501*f4a2713aSLionel Sambuc } 502*f4a2713aSLionel Sambuc 503*f4a2713aSLionel Sambuc /// Adjust the given return statements so that they formally return 504*f4a2713aSLionel Sambuc /// the given type. It should require, at most, an IntegralCast. 505*f4a2713aSLionel Sambuc static void adjustBlockReturnsToEnum(Sema &S, ArrayRef<ReturnStmt*> returns, 506*f4a2713aSLionel Sambuc QualType returnType) { 507*f4a2713aSLionel Sambuc for (ArrayRef<ReturnStmt*>::iterator 508*f4a2713aSLionel Sambuc i = returns.begin(), e = returns.end(); i != e; ++i) { 509*f4a2713aSLionel Sambuc ReturnStmt *ret = *i; 510*f4a2713aSLionel Sambuc Expr *retValue = ret->getRetValue(); 511*f4a2713aSLionel Sambuc if (S.Context.hasSameType(retValue->getType(), returnType)) 512*f4a2713aSLionel Sambuc continue; 513*f4a2713aSLionel Sambuc 514*f4a2713aSLionel Sambuc // Right now we only support integral fixup casts. 515*f4a2713aSLionel Sambuc assert(returnType->isIntegralOrUnscopedEnumerationType()); 516*f4a2713aSLionel Sambuc assert(retValue->getType()->isIntegralOrUnscopedEnumerationType()); 517*f4a2713aSLionel Sambuc 518*f4a2713aSLionel Sambuc ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(retValue); 519*f4a2713aSLionel Sambuc 520*f4a2713aSLionel Sambuc Expr *E = (cleanups ? cleanups->getSubExpr() : retValue); 521*f4a2713aSLionel Sambuc E = ImplicitCastExpr::Create(S.Context, returnType, CK_IntegralCast, 522*f4a2713aSLionel Sambuc E, /*base path*/ 0, VK_RValue); 523*f4a2713aSLionel Sambuc if (cleanups) { 524*f4a2713aSLionel Sambuc cleanups->setSubExpr(E); 525*f4a2713aSLionel Sambuc } else { 526*f4a2713aSLionel Sambuc ret->setRetValue(E); 527*f4a2713aSLionel Sambuc } 528*f4a2713aSLionel Sambuc } 529*f4a2713aSLionel Sambuc } 530*f4a2713aSLionel Sambuc 531*f4a2713aSLionel Sambuc void Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) { 532*f4a2713aSLionel Sambuc assert(CSI.HasImplicitReturnType); 533*f4a2713aSLionel Sambuc // If it was ever a placeholder, it had to been deduced to DependentTy. 534*f4a2713aSLionel Sambuc assert(CSI.ReturnType.isNull() || !CSI.ReturnType->isUndeducedType()); 535*f4a2713aSLionel Sambuc 536*f4a2713aSLionel Sambuc // C++ Core Issue #975, proposed resolution: 537*f4a2713aSLionel Sambuc // If a lambda-expression does not include a trailing-return-type, 538*f4a2713aSLionel Sambuc // it is as if the trailing-return-type denotes the following type: 539*f4a2713aSLionel Sambuc // - if there are no return statements in the compound-statement, 540*f4a2713aSLionel Sambuc // or all return statements return either an expression of type 541*f4a2713aSLionel Sambuc // void or no expression or braced-init-list, the type void; 542*f4a2713aSLionel Sambuc // - otherwise, if all return statements return an expression 543*f4a2713aSLionel Sambuc // and the types of the returned expressions after 544*f4a2713aSLionel Sambuc // lvalue-to-rvalue conversion (4.1 [conv.lval]), 545*f4a2713aSLionel Sambuc // array-to-pointer conversion (4.2 [conv.array]), and 546*f4a2713aSLionel Sambuc // function-to-pointer conversion (4.3 [conv.func]) are the 547*f4a2713aSLionel Sambuc // same, that common type; 548*f4a2713aSLionel Sambuc // - otherwise, the program is ill-formed. 549*f4a2713aSLionel Sambuc // 550*f4a2713aSLionel Sambuc // In addition, in blocks in non-C++ modes, if all of the return 551*f4a2713aSLionel Sambuc // statements are enumerator-like expressions of some type T, where 552*f4a2713aSLionel Sambuc // T has a name for linkage, then we infer the return type of the 553*f4a2713aSLionel Sambuc // block to be that type. 554*f4a2713aSLionel Sambuc 555*f4a2713aSLionel Sambuc // First case: no return statements, implicit void return type. 556*f4a2713aSLionel Sambuc ASTContext &Ctx = getASTContext(); 557*f4a2713aSLionel Sambuc if (CSI.Returns.empty()) { 558*f4a2713aSLionel Sambuc // It's possible there were simply no /valid/ return statements. 559*f4a2713aSLionel Sambuc // In this case, the first one we found may have at least given us a type. 560*f4a2713aSLionel Sambuc if (CSI.ReturnType.isNull()) 561*f4a2713aSLionel Sambuc CSI.ReturnType = Ctx.VoidTy; 562*f4a2713aSLionel Sambuc return; 563*f4a2713aSLionel Sambuc } 564*f4a2713aSLionel Sambuc 565*f4a2713aSLionel Sambuc // Second case: at least one return statement has dependent type. 566*f4a2713aSLionel Sambuc // Delay type checking until instantiation. 567*f4a2713aSLionel Sambuc assert(!CSI.ReturnType.isNull() && "We should have a tentative return type."); 568*f4a2713aSLionel Sambuc if (CSI.ReturnType->isDependentType()) 569*f4a2713aSLionel Sambuc return; 570*f4a2713aSLionel Sambuc 571*f4a2713aSLionel Sambuc // Try to apply the enum-fuzz rule. 572*f4a2713aSLionel Sambuc if (!getLangOpts().CPlusPlus) { 573*f4a2713aSLionel Sambuc assert(isa<BlockScopeInfo>(CSI)); 574*f4a2713aSLionel Sambuc const EnumDecl *ED = findCommonEnumForBlockReturns(CSI.Returns); 575*f4a2713aSLionel Sambuc if (ED) { 576*f4a2713aSLionel Sambuc CSI.ReturnType = Context.getTypeDeclType(ED); 577*f4a2713aSLionel Sambuc adjustBlockReturnsToEnum(*this, CSI.Returns, CSI.ReturnType); 578*f4a2713aSLionel Sambuc return; 579*f4a2713aSLionel Sambuc } 580*f4a2713aSLionel Sambuc } 581*f4a2713aSLionel Sambuc 582*f4a2713aSLionel Sambuc // Third case: only one return statement. Don't bother doing extra work! 583*f4a2713aSLionel Sambuc SmallVectorImpl<ReturnStmt*>::iterator I = CSI.Returns.begin(), 584*f4a2713aSLionel Sambuc E = CSI.Returns.end(); 585*f4a2713aSLionel Sambuc if (I+1 == E) 586*f4a2713aSLionel Sambuc return; 587*f4a2713aSLionel Sambuc 588*f4a2713aSLionel Sambuc // General case: many return statements. 589*f4a2713aSLionel Sambuc // Check that they all have compatible return types. 590*f4a2713aSLionel Sambuc 591*f4a2713aSLionel Sambuc // We require the return types to strictly match here. 592*f4a2713aSLionel Sambuc // Note that we've already done the required promotions as part of 593*f4a2713aSLionel Sambuc // processing the return statement. 594*f4a2713aSLionel Sambuc for (; I != E; ++I) { 595*f4a2713aSLionel Sambuc const ReturnStmt *RS = *I; 596*f4a2713aSLionel Sambuc const Expr *RetE = RS->getRetValue(); 597*f4a2713aSLionel Sambuc 598*f4a2713aSLionel Sambuc QualType ReturnType = (RetE ? RetE->getType() : Context.VoidTy); 599*f4a2713aSLionel Sambuc if (Context.hasSameType(ReturnType, CSI.ReturnType)) 600*f4a2713aSLionel Sambuc continue; 601*f4a2713aSLionel Sambuc 602*f4a2713aSLionel Sambuc // FIXME: This is a poor diagnostic for ReturnStmts without expressions. 603*f4a2713aSLionel Sambuc // TODO: It's possible that the *first* return is the divergent one. 604*f4a2713aSLionel Sambuc Diag(RS->getLocStart(), 605*f4a2713aSLionel Sambuc diag::err_typecheck_missing_return_type_incompatible) 606*f4a2713aSLionel Sambuc << ReturnType << CSI.ReturnType 607*f4a2713aSLionel Sambuc << isa<LambdaScopeInfo>(CSI); 608*f4a2713aSLionel Sambuc // Continue iterating so that we keep emitting diagnostics. 609*f4a2713aSLionel Sambuc } 610*f4a2713aSLionel Sambuc } 611*f4a2713aSLionel Sambuc 612*f4a2713aSLionel Sambuc VarDecl *Sema::checkInitCapture(SourceLocation Loc, bool ByRef, 613*f4a2713aSLionel Sambuc IdentifierInfo *Id, Expr *Init) { 614*f4a2713aSLionel Sambuc // C++1y [expr.prim.lambda]p11: 615*f4a2713aSLionel Sambuc // An init-capture behaves as if it declares and explicitly captures 616*f4a2713aSLionel Sambuc // a variable of the form 617*f4a2713aSLionel Sambuc // "auto init-capture;" 618*f4a2713aSLionel Sambuc QualType DeductType = Context.getAutoDeductType(); 619*f4a2713aSLionel Sambuc TypeLocBuilder TLB; 620*f4a2713aSLionel Sambuc TLB.pushTypeSpec(DeductType).setNameLoc(Loc); 621*f4a2713aSLionel Sambuc if (ByRef) { 622*f4a2713aSLionel Sambuc DeductType = BuildReferenceType(DeductType, true, Loc, Id); 623*f4a2713aSLionel Sambuc assert(!DeductType.isNull() && "can't build reference to auto"); 624*f4a2713aSLionel Sambuc TLB.push<ReferenceTypeLoc>(DeductType).setSigilLoc(Loc); 625*f4a2713aSLionel Sambuc } 626*f4a2713aSLionel Sambuc TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType); 627*f4a2713aSLionel Sambuc 628*f4a2713aSLionel Sambuc // Create a dummy variable representing the init-capture. This is not actually 629*f4a2713aSLionel Sambuc // used as a variable, and only exists as a way to name and refer to the 630*f4a2713aSLionel Sambuc // init-capture. 631*f4a2713aSLionel Sambuc // FIXME: Pass in separate source locations for '&' and identifier. 632*f4a2713aSLionel Sambuc VarDecl *NewVD = VarDecl::Create(Context, CurContext, Loc, 633*f4a2713aSLionel Sambuc Loc, Id, TSI->getType(), TSI, SC_Auto); 634*f4a2713aSLionel Sambuc NewVD->setInitCapture(true); 635*f4a2713aSLionel Sambuc NewVD->setReferenced(true); 636*f4a2713aSLionel Sambuc NewVD->markUsed(Context); 637*f4a2713aSLionel Sambuc 638*f4a2713aSLionel Sambuc // We do not need to distinguish between direct-list-initialization 639*f4a2713aSLionel Sambuc // and copy-list-initialization here, because we will always deduce 640*f4a2713aSLionel Sambuc // std::initializer_list<T>, and direct- and copy-list-initialization 641*f4a2713aSLionel Sambuc // always behave the same for such a type. 642*f4a2713aSLionel Sambuc // FIXME: We should model whether an '=' was present. 643*f4a2713aSLionel Sambuc bool DirectInit = isa<ParenListExpr>(Init) || isa<InitListExpr>(Init); 644*f4a2713aSLionel Sambuc AddInitializerToDecl(NewVD, Init, DirectInit, /*ContainsAuto*/true); 645*f4a2713aSLionel Sambuc return NewVD; 646*f4a2713aSLionel Sambuc } 647*f4a2713aSLionel Sambuc 648*f4a2713aSLionel Sambuc FieldDecl *Sema::buildInitCaptureField(LambdaScopeInfo *LSI, VarDecl *Var) { 649*f4a2713aSLionel Sambuc FieldDecl *Field = FieldDecl::Create( 650*f4a2713aSLionel Sambuc Context, LSI->Lambda, Var->getLocation(), Var->getLocation(), 651*f4a2713aSLionel Sambuc 0, Var->getType(), Var->getTypeSourceInfo(), 0, false, ICIS_NoInit); 652*f4a2713aSLionel Sambuc Field->setImplicit(true); 653*f4a2713aSLionel Sambuc Field->setAccess(AS_private); 654*f4a2713aSLionel Sambuc LSI->Lambda->addDecl(Field); 655*f4a2713aSLionel Sambuc 656*f4a2713aSLionel Sambuc LSI->addCapture(Var, /*isBlock*/false, Var->getType()->isReferenceType(), 657*f4a2713aSLionel Sambuc /*isNested*/false, Var->getLocation(), SourceLocation(), 658*f4a2713aSLionel Sambuc Var->getType(), Var->getInit()); 659*f4a2713aSLionel Sambuc return Field; 660*f4a2713aSLionel Sambuc } 661*f4a2713aSLionel Sambuc 662*f4a2713aSLionel Sambuc void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, 663*f4a2713aSLionel Sambuc Declarator &ParamInfo, Scope *CurScope) { 664*f4a2713aSLionel Sambuc // Determine if we're within a context where we know that the lambda will 665*f4a2713aSLionel Sambuc // be dependent, because there are template parameters in scope. 666*f4a2713aSLionel Sambuc bool KnownDependent = false; 667*f4a2713aSLionel Sambuc LambdaScopeInfo *const LSI = getCurLambda(); 668*f4a2713aSLionel Sambuc assert(LSI && "LambdaScopeInfo should be on stack!"); 669*f4a2713aSLionel Sambuc TemplateParameterList *TemplateParams = 670*f4a2713aSLionel Sambuc getGenericLambdaTemplateParameterList(LSI, *this); 671*f4a2713aSLionel Sambuc 672*f4a2713aSLionel Sambuc if (Scope *TmplScope = CurScope->getTemplateParamParent()) { 673*f4a2713aSLionel Sambuc // Since we have our own TemplateParams, so check if an outer scope 674*f4a2713aSLionel Sambuc // has template params, only then are we in a dependent scope. 675*f4a2713aSLionel Sambuc if (TemplateParams) { 676*f4a2713aSLionel Sambuc TmplScope = TmplScope->getParent(); 677*f4a2713aSLionel Sambuc TmplScope = TmplScope ? TmplScope->getTemplateParamParent() : 0; 678*f4a2713aSLionel Sambuc } 679*f4a2713aSLionel Sambuc if (TmplScope && !TmplScope->decl_empty()) 680*f4a2713aSLionel Sambuc KnownDependent = true; 681*f4a2713aSLionel Sambuc } 682*f4a2713aSLionel Sambuc // Determine the signature of the call operator. 683*f4a2713aSLionel Sambuc TypeSourceInfo *MethodTyInfo; 684*f4a2713aSLionel Sambuc bool ExplicitParams = true; 685*f4a2713aSLionel Sambuc bool ExplicitResultType = true; 686*f4a2713aSLionel Sambuc bool ContainsUnexpandedParameterPack = false; 687*f4a2713aSLionel Sambuc SourceLocation EndLoc; 688*f4a2713aSLionel Sambuc SmallVector<ParmVarDecl *, 8> Params; 689*f4a2713aSLionel Sambuc if (ParamInfo.getNumTypeObjects() == 0) { 690*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p4: 691*f4a2713aSLionel Sambuc // If a lambda-expression does not include a lambda-declarator, it is as 692*f4a2713aSLionel Sambuc // if the lambda-declarator were (). 693*f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo EPI(Context.getDefaultCallingConvention( 694*f4a2713aSLionel Sambuc /*IsVariadic=*/false, /*IsCXXMethod=*/true)); 695*f4a2713aSLionel Sambuc EPI.HasTrailingReturn = true; 696*f4a2713aSLionel Sambuc EPI.TypeQuals |= DeclSpec::TQ_const; 697*f4a2713aSLionel Sambuc // C++1y [expr.prim.lambda]: 698*f4a2713aSLionel Sambuc // The lambda return type is 'auto', which is replaced by the 699*f4a2713aSLionel Sambuc // trailing-return type if provided and/or deduced from 'return' 700*f4a2713aSLionel Sambuc // statements 701*f4a2713aSLionel Sambuc // We don't do this before C++1y, because we don't support deduced return 702*f4a2713aSLionel Sambuc // types there. 703*f4a2713aSLionel Sambuc QualType DefaultTypeForNoTrailingReturn = 704*f4a2713aSLionel Sambuc getLangOpts().CPlusPlus1y ? Context.getAutoDeductType() 705*f4a2713aSLionel Sambuc : Context.DependentTy; 706*f4a2713aSLionel Sambuc QualType MethodTy = 707*f4a2713aSLionel Sambuc Context.getFunctionType(DefaultTypeForNoTrailingReturn, None, EPI); 708*f4a2713aSLionel Sambuc MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy); 709*f4a2713aSLionel Sambuc ExplicitParams = false; 710*f4a2713aSLionel Sambuc ExplicitResultType = false; 711*f4a2713aSLionel Sambuc EndLoc = Intro.Range.getEnd(); 712*f4a2713aSLionel Sambuc } else { 713*f4a2713aSLionel Sambuc assert(ParamInfo.isFunctionDeclarator() && 714*f4a2713aSLionel Sambuc "lambda-declarator is a function"); 715*f4a2713aSLionel Sambuc DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo(); 716*f4a2713aSLionel Sambuc 717*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p5: 718*f4a2713aSLionel Sambuc // This function call operator is declared const (9.3.1) if and only if 719*f4a2713aSLionel Sambuc // the lambda-expression's parameter-declaration-clause is not followed 720*f4a2713aSLionel Sambuc // by mutable. It is neither virtual nor declared volatile. [...] 721*f4a2713aSLionel Sambuc if (!FTI.hasMutableQualifier()) 722*f4a2713aSLionel Sambuc FTI.TypeQuals |= DeclSpec::TQ_const; 723*f4a2713aSLionel Sambuc 724*f4a2713aSLionel Sambuc MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope); 725*f4a2713aSLionel Sambuc assert(MethodTyInfo && "no type from lambda-declarator"); 726*f4a2713aSLionel Sambuc EndLoc = ParamInfo.getSourceRange().getEnd(); 727*f4a2713aSLionel Sambuc 728*f4a2713aSLionel Sambuc ExplicitResultType = FTI.hasTrailingReturnType(); 729*f4a2713aSLionel Sambuc 730*f4a2713aSLionel Sambuc if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && 731*f4a2713aSLionel Sambuc cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()) { 732*f4a2713aSLionel Sambuc // Empty arg list, don't push any params. 733*f4a2713aSLionel Sambuc checkVoidParamDecl(cast<ParmVarDecl>(FTI.ArgInfo[0].Param)); 734*f4a2713aSLionel Sambuc } else { 735*f4a2713aSLionel Sambuc Params.reserve(FTI.NumArgs); 736*f4a2713aSLionel Sambuc for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) 737*f4a2713aSLionel Sambuc Params.push_back(cast<ParmVarDecl>(FTI.ArgInfo[i].Param)); 738*f4a2713aSLionel Sambuc } 739*f4a2713aSLionel Sambuc 740*f4a2713aSLionel Sambuc // Check for unexpanded parameter packs in the method type. 741*f4a2713aSLionel Sambuc if (MethodTyInfo->getType()->containsUnexpandedParameterPack()) 742*f4a2713aSLionel Sambuc ContainsUnexpandedParameterPack = true; 743*f4a2713aSLionel Sambuc } 744*f4a2713aSLionel Sambuc 745*f4a2713aSLionel Sambuc CXXRecordDecl *Class = createLambdaClosureType(Intro.Range, MethodTyInfo, 746*f4a2713aSLionel Sambuc KnownDependent, Intro.Default); 747*f4a2713aSLionel Sambuc 748*f4a2713aSLionel Sambuc CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range, 749*f4a2713aSLionel Sambuc MethodTyInfo, EndLoc, Params); 750*f4a2713aSLionel Sambuc if (ExplicitParams) 751*f4a2713aSLionel Sambuc CheckCXXDefaultArguments(Method); 752*f4a2713aSLionel Sambuc 753*f4a2713aSLionel Sambuc // Attributes on the lambda apply to the method. 754*f4a2713aSLionel Sambuc ProcessDeclAttributes(CurScope, Method, ParamInfo); 755*f4a2713aSLionel Sambuc 756*f4a2713aSLionel Sambuc // Introduce the function call operator as the current declaration context. 757*f4a2713aSLionel Sambuc PushDeclContext(CurScope, Method); 758*f4a2713aSLionel Sambuc 759*f4a2713aSLionel Sambuc // Build the lambda scope. 760*f4a2713aSLionel Sambuc buildLambdaScope(LSI, Method, 761*f4a2713aSLionel Sambuc Intro.Range, 762*f4a2713aSLionel Sambuc Intro.Default, Intro.DefaultLoc, 763*f4a2713aSLionel Sambuc ExplicitParams, 764*f4a2713aSLionel Sambuc ExplicitResultType, 765*f4a2713aSLionel Sambuc !Method->isConst()); 766*f4a2713aSLionel Sambuc 767*f4a2713aSLionel Sambuc // Distinct capture names, for diagnostics. 768*f4a2713aSLionel Sambuc llvm::SmallSet<IdentifierInfo*, 8> CaptureNames; 769*f4a2713aSLionel Sambuc 770*f4a2713aSLionel Sambuc // Handle explicit captures. 771*f4a2713aSLionel Sambuc SourceLocation PrevCaptureLoc 772*f4a2713aSLionel Sambuc = Intro.Default == LCD_None? Intro.Range.getBegin() : Intro.DefaultLoc; 773*f4a2713aSLionel Sambuc for (SmallVectorImpl<LambdaCapture>::const_iterator 774*f4a2713aSLionel Sambuc C = Intro.Captures.begin(), 775*f4a2713aSLionel Sambuc E = Intro.Captures.end(); 776*f4a2713aSLionel Sambuc C != E; 777*f4a2713aSLionel Sambuc PrevCaptureLoc = C->Loc, ++C) { 778*f4a2713aSLionel Sambuc if (C->Kind == LCK_This) { 779*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p8: 780*f4a2713aSLionel Sambuc // An identifier or this shall not appear more than once in a 781*f4a2713aSLionel Sambuc // lambda-capture. 782*f4a2713aSLionel Sambuc if (LSI->isCXXThisCaptured()) { 783*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_capture_more_than_once) 784*f4a2713aSLionel Sambuc << "'this'" 785*f4a2713aSLionel Sambuc << SourceRange(LSI->getCXXThisCapture().getLocation()) 786*f4a2713aSLionel Sambuc << FixItHint::CreateRemoval( 787*f4a2713aSLionel Sambuc SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 788*f4a2713aSLionel Sambuc continue; 789*f4a2713aSLionel Sambuc } 790*f4a2713aSLionel Sambuc 791*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p8: 792*f4a2713aSLionel Sambuc // If a lambda-capture includes a capture-default that is =, the 793*f4a2713aSLionel Sambuc // lambda-capture shall not contain this [...]. 794*f4a2713aSLionel Sambuc if (Intro.Default == LCD_ByCopy) { 795*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_this_capture_with_copy_default) 796*f4a2713aSLionel Sambuc << FixItHint::CreateRemoval( 797*f4a2713aSLionel Sambuc SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 798*f4a2713aSLionel Sambuc continue; 799*f4a2713aSLionel Sambuc } 800*f4a2713aSLionel Sambuc 801*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p12: 802*f4a2713aSLionel Sambuc // If this is captured by a local lambda expression, its nearest 803*f4a2713aSLionel Sambuc // enclosing function shall be a non-static member function. 804*f4a2713aSLionel Sambuc QualType ThisCaptureType = getCurrentThisType(); 805*f4a2713aSLionel Sambuc if (ThisCaptureType.isNull()) { 806*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_this_capture) << true; 807*f4a2713aSLionel Sambuc continue; 808*f4a2713aSLionel Sambuc } 809*f4a2713aSLionel Sambuc 810*f4a2713aSLionel Sambuc CheckCXXThisCapture(C->Loc, /*Explicit=*/true); 811*f4a2713aSLionel Sambuc continue; 812*f4a2713aSLionel Sambuc } 813*f4a2713aSLionel Sambuc 814*f4a2713aSLionel Sambuc assert(C->Id && "missing identifier for capture"); 815*f4a2713aSLionel Sambuc 816*f4a2713aSLionel Sambuc if (C->Init.isInvalid()) 817*f4a2713aSLionel Sambuc continue; 818*f4a2713aSLionel Sambuc 819*f4a2713aSLionel Sambuc VarDecl *Var; 820*f4a2713aSLionel Sambuc if (C->Init.isUsable()) { 821*f4a2713aSLionel Sambuc Diag(C->Loc, getLangOpts().CPlusPlus1y 822*f4a2713aSLionel Sambuc ? diag::warn_cxx11_compat_init_capture 823*f4a2713aSLionel Sambuc : diag::ext_init_capture); 824*f4a2713aSLionel Sambuc 825*f4a2713aSLionel Sambuc if (C->Init.get()->containsUnexpandedParameterPack()) 826*f4a2713aSLionel Sambuc ContainsUnexpandedParameterPack = true; 827*f4a2713aSLionel Sambuc 828*f4a2713aSLionel Sambuc Var = checkInitCapture(C->Loc, C->Kind == LCK_ByRef, 829*f4a2713aSLionel Sambuc C->Id, C->Init.take()); 830*f4a2713aSLionel Sambuc // C++1y [expr.prim.lambda]p11: 831*f4a2713aSLionel Sambuc // An init-capture behaves as if it declares and explicitly 832*f4a2713aSLionel Sambuc // captures a variable [...] whose declarative region is the 833*f4a2713aSLionel Sambuc // lambda-expression's compound-statement 834*f4a2713aSLionel Sambuc if (Var) 835*f4a2713aSLionel Sambuc PushOnScopeChains(Var, CurScope, false); 836*f4a2713aSLionel Sambuc } else { 837*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p8: 838*f4a2713aSLionel Sambuc // If a lambda-capture includes a capture-default that is &, the 839*f4a2713aSLionel Sambuc // identifiers in the lambda-capture shall not be preceded by &. 840*f4a2713aSLionel Sambuc // If a lambda-capture includes a capture-default that is =, [...] 841*f4a2713aSLionel Sambuc // each identifier it contains shall be preceded by &. 842*f4a2713aSLionel Sambuc if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) { 843*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_reference_capture_with_reference_default) 844*f4a2713aSLionel Sambuc << FixItHint::CreateRemoval( 845*f4a2713aSLionel Sambuc SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 846*f4a2713aSLionel Sambuc continue; 847*f4a2713aSLionel Sambuc } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) { 848*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_copy_capture_with_copy_default) 849*f4a2713aSLionel Sambuc << FixItHint::CreateRemoval( 850*f4a2713aSLionel Sambuc SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 851*f4a2713aSLionel Sambuc continue; 852*f4a2713aSLionel Sambuc } 853*f4a2713aSLionel Sambuc 854*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p10: 855*f4a2713aSLionel Sambuc // The identifiers in a capture-list are looked up using the usual 856*f4a2713aSLionel Sambuc // rules for unqualified name lookup (3.4.1) 857*f4a2713aSLionel Sambuc DeclarationNameInfo Name(C->Id, C->Loc); 858*f4a2713aSLionel Sambuc LookupResult R(*this, Name, LookupOrdinaryName); 859*f4a2713aSLionel Sambuc LookupName(R, CurScope); 860*f4a2713aSLionel Sambuc if (R.isAmbiguous()) 861*f4a2713aSLionel Sambuc continue; 862*f4a2713aSLionel Sambuc if (R.empty()) { 863*f4a2713aSLionel Sambuc // FIXME: Disable corrections that would add qualification? 864*f4a2713aSLionel Sambuc CXXScopeSpec ScopeSpec; 865*f4a2713aSLionel Sambuc DeclFilterCCC<VarDecl> Validator; 866*f4a2713aSLionel Sambuc if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator)) 867*f4a2713aSLionel Sambuc continue; 868*f4a2713aSLionel Sambuc } 869*f4a2713aSLionel Sambuc 870*f4a2713aSLionel Sambuc Var = R.getAsSingle<VarDecl>(); 871*f4a2713aSLionel Sambuc } 872*f4a2713aSLionel Sambuc 873*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p8: 874*f4a2713aSLionel Sambuc // An identifier or this shall not appear more than once in a 875*f4a2713aSLionel Sambuc // lambda-capture. 876*f4a2713aSLionel Sambuc if (!CaptureNames.insert(C->Id)) { 877*f4a2713aSLionel Sambuc if (Var && LSI->isCaptured(Var)) { 878*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_capture_more_than_once) 879*f4a2713aSLionel Sambuc << C->Id << SourceRange(LSI->getCapture(Var).getLocation()) 880*f4a2713aSLionel Sambuc << FixItHint::CreateRemoval( 881*f4a2713aSLionel Sambuc SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 882*f4a2713aSLionel Sambuc } else 883*f4a2713aSLionel Sambuc // Previous capture captured something different (one or both was 884*f4a2713aSLionel Sambuc // an init-cpature): no fixit. 885*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_capture_more_than_once) << C->Id; 886*f4a2713aSLionel Sambuc continue; 887*f4a2713aSLionel Sambuc } 888*f4a2713aSLionel Sambuc 889*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p10: 890*f4a2713aSLionel Sambuc // [...] each such lookup shall find a variable with automatic storage 891*f4a2713aSLionel Sambuc // duration declared in the reaching scope of the local lambda expression. 892*f4a2713aSLionel Sambuc // Note that the 'reaching scope' check happens in tryCaptureVariable(). 893*f4a2713aSLionel Sambuc if (!Var) { 894*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id; 895*f4a2713aSLionel Sambuc continue; 896*f4a2713aSLionel Sambuc } 897*f4a2713aSLionel Sambuc 898*f4a2713aSLionel Sambuc // Ignore invalid decls; they'll just confuse the code later. 899*f4a2713aSLionel Sambuc if (Var->isInvalidDecl()) 900*f4a2713aSLionel Sambuc continue; 901*f4a2713aSLionel Sambuc 902*f4a2713aSLionel Sambuc if (!Var->hasLocalStorage()) { 903*f4a2713aSLionel Sambuc Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id; 904*f4a2713aSLionel Sambuc Diag(Var->getLocation(), diag::note_previous_decl) << C->Id; 905*f4a2713aSLionel Sambuc continue; 906*f4a2713aSLionel Sambuc } 907*f4a2713aSLionel Sambuc 908*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p23: 909*f4a2713aSLionel Sambuc // A capture followed by an ellipsis is a pack expansion (14.5.3). 910*f4a2713aSLionel Sambuc SourceLocation EllipsisLoc; 911*f4a2713aSLionel Sambuc if (C->EllipsisLoc.isValid()) { 912*f4a2713aSLionel Sambuc if (Var->isParameterPack()) { 913*f4a2713aSLionel Sambuc EllipsisLoc = C->EllipsisLoc; 914*f4a2713aSLionel Sambuc } else { 915*f4a2713aSLionel Sambuc Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) 916*f4a2713aSLionel Sambuc << SourceRange(C->Loc); 917*f4a2713aSLionel Sambuc 918*f4a2713aSLionel Sambuc // Just ignore the ellipsis. 919*f4a2713aSLionel Sambuc } 920*f4a2713aSLionel Sambuc } else if (Var->isParameterPack()) { 921*f4a2713aSLionel Sambuc ContainsUnexpandedParameterPack = true; 922*f4a2713aSLionel Sambuc } 923*f4a2713aSLionel Sambuc 924*f4a2713aSLionel Sambuc if (C->Init.isUsable()) { 925*f4a2713aSLionel Sambuc buildInitCaptureField(LSI, Var); 926*f4a2713aSLionel Sambuc } else { 927*f4a2713aSLionel Sambuc TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef : 928*f4a2713aSLionel Sambuc TryCapture_ExplicitByVal; 929*f4a2713aSLionel Sambuc tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc); 930*f4a2713aSLionel Sambuc } 931*f4a2713aSLionel Sambuc } 932*f4a2713aSLionel Sambuc finishLambdaExplicitCaptures(LSI); 933*f4a2713aSLionel Sambuc 934*f4a2713aSLionel Sambuc LSI->ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 935*f4a2713aSLionel Sambuc 936*f4a2713aSLionel Sambuc // Add lambda parameters into scope. 937*f4a2713aSLionel Sambuc addLambdaParameters(Method, CurScope); 938*f4a2713aSLionel Sambuc 939*f4a2713aSLionel Sambuc // Enter a new evaluation context to insulate the lambda from any 940*f4a2713aSLionel Sambuc // cleanups from the enclosing full-expression. 941*f4a2713aSLionel Sambuc PushExpressionEvaluationContext(PotentiallyEvaluated); 942*f4a2713aSLionel Sambuc } 943*f4a2713aSLionel Sambuc 944*f4a2713aSLionel Sambuc void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope, 945*f4a2713aSLionel Sambuc bool IsInstantiation) { 946*f4a2713aSLionel Sambuc // Leave the expression-evaluation context. 947*f4a2713aSLionel Sambuc DiscardCleanupsInEvaluationContext(); 948*f4a2713aSLionel Sambuc PopExpressionEvaluationContext(); 949*f4a2713aSLionel Sambuc 950*f4a2713aSLionel Sambuc // Leave the context of the lambda. 951*f4a2713aSLionel Sambuc if (!IsInstantiation) 952*f4a2713aSLionel Sambuc PopDeclContext(); 953*f4a2713aSLionel Sambuc 954*f4a2713aSLionel Sambuc // Finalize the lambda. 955*f4a2713aSLionel Sambuc LambdaScopeInfo *LSI = getCurLambda(); 956*f4a2713aSLionel Sambuc CXXRecordDecl *Class = LSI->Lambda; 957*f4a2713aSLionel Sambuc Class->setInvalidDecl(); 958*f4a2713aSLionel Sambuc SmallVector<Decl*, 4> Fields; 959*f4a2713aSLionel Sambuc for (RecordDecl::field_iterator i = Class->field_begin(), 960*f4a2713aSLionel Sambuc e = Class->field_end(); i != e; ++i) 961*f4a2713aSLionel Sambuc Fields.push_back(*i); 962*f4a2713aSLionel Sambuc ActOnFields(0, Class->getLocation(), Class, Fields, 963*f4a2713aSLionel Sambuc SourceLocation(), SourceLocation(), 0); 964*f4a2713aSLionel Sambuc CheckCompletedCXXClass(Class); 965*f4a2713aSLionel Sambuc 966*f4a2713aSLionel Sambuc PopFunctionScopeInfo(); 967*f4a2713aSLionel Sambuc } 968*f4a2713aSLionel Sambuc 969*f4a2713aSLionel Sambuc /// \brief Add a lambda's conversion to function pointer, as described in 970*f4a2713aSLionel Sambuc /// C++11 [expr.prim.lambda]p6. 971*f4a2713aSLionel Sambuc static void addFunctionPointerConversion(Sema &S, 972*f4a2713aSLionel Sambuc SourceRange IntroducerRange, 973*f4a2713aSLionel Sambuc CXXRecordDecl *Class, 974*f4a2713aSLionel Sambuc CXXMethodDecl *CallOperator) { 975*f4a2713aSLionel Sambuc // Add the conversion to function pointer. 976*f4a2713aSLionel Sambuc const FunctionProtoType *CallOpProto = 977*f4a2713aSLionel Sambuc CallOperator->getType()->getAs<FunctionProtoType>(); 978*f4a2713aSLionel Sambuc const FunctionProtoType::ExtProtoInfo CallOpExtInfo = 979*f4a2713aSLionel Sambuc CallOpProto->getExtProtoInfo(); 980*f4a2713aSLionel Sambuc QualType PtrToFunctionTy; 981*f4a2713aSLionel Sambuc QualType InvokerFunctionTy; 982*f4a2713aSLionel Sambuc { 983*f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo InvokerExtInfo = CallOpExtInfo; 984*f4a2713aSLionel Sambuc CallingConv CC = S.Context.getDefaultCallingConvention( 985*f4a2713aSLionel Sambuc CallOpProto->isVariadic(), /*IsCXXMethod=*/false); 986*f4a2713aSLionel Sambuc InvokerExtInfo.ExtInfo = InvokerExtInfo.ExtInfo.withCallingConv(CC); 987*f4a2713aSLionel Sambuc InvokerExtInfo.TypeQuals = 0; 988*f4a2713aSLionel Sambuc assert(InvokerExtInfo.RefQualifier == RQ_None && 989*f4a2713aSLionel Sambuc "Lambda's call operator should not have a reference qualifier"); 990*f4a2713aSLionel Sambuc InvokerFunctionTy = S.Context.getFunctionType(CallOpProto->getResultType(), 991*f4a2713aSLionel Sambuc CallOpProto->getArgTypes(), InvokerExtInfo); 992*f4a2713aSLionel Sambuc PtrToFunctionTy = S.Context.getPointerType(InvokerFunctionTy); 993*f4a2713aSLionel Sambuc } 994*f4a2713aSLionel Sambuc 995*f4a2713aSLionel Sambuc // Create the type of the conversion function. 996*f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo ConvExtInfo( 997*f4a2713aSLionel Sambuc S.Context.getDefaultCallingConvention( 998*f4a2713aSLionel Sambuc /*IsVariadic=*/false, /*IsCXXMethod=*/true)); 999*f4a2713aSLionel Sambuc // The conversion function is always const. 1000*f4a2713aSLionel Sambuc ConvExtInfo.TypeQuals = Qualifiers::Const; 1001*f4a2713aSLionel Sambuc QualType ConvTy = 1002*f4a2713aSLionel Sambuc S.Context.getFunctionType(PtrToFunctionTy, None, ConvExtInfo); 1003*f4a2713aSLionel Sambuc 1004*f4a2713aSLionel Sambuc SourceLocation Loc = IntroducerRange.getBegin(); 1005*f4a2713aSLionel Sambuc DeclarationName ConversionName 1006*f4a2713aSLionel Sambuc = S.Context.DeclarationNames.getCXXConversionFunctionName( 1007*f4a2713aSLionel Sambuc S.Context.getCanonicalType(PtrToFunctionTy)); 1008*f4a2713aSLionel Sambuc DeclarationNameLoc ConvNameLoc; 1009*f4a2713aSLionel Sambuc // Construct a TypeSourceInfo for the conversion function, and wire 1010*f4a2713aSLionel Sambuc // all the parameters appropriately for the FunctionProtoTypeLoc 1011*f4a2713aSLionel Sambuc // so that everything works during transformation/instantiation of 1012*f4a2713aSLionel Sambuc // generic lambdas. 1013*f4a2713aSLionel Sambuc // The main reason for wiring up the parameters of the conversion 1014*f4a2713aSLionel Sambuc // function with that of the call operator is so that constructs 1015*f4a2713aSLionel Sambuc // like the following work: 1016*f4a2713aSLionel Sambuc // auto L = [](auto b) { <-- 1 1017*f4a2713aSLionel Sambuc // return [](auto a) -> decltype(a) { <-- 2 1018*f4a2713aSLionel Sambuc // return a; 1019*f4a2713aSLionel Sambuc // }; 1020*f4a2713aSLionel Sambuc // }; 1021*f4a2713aSLionel Sambuc // int (*fp)(int) = L(5); 1022*f4a2713aSLionel Sambuc // Because the trailing return type can contain DeclRefExprs that refer 1023*f4a2713aSLionel Sambuc // to the original call operator's variables, we hijack the call 1024*f4a2713aSLionel Sambuc // operators ParmVarDecls below. 1025*f4a2713aSLionel Sambuc TypeSourceInfo *ConvNamePtrToFunctionTSI = 1026*f4a2713aSLionel Sambuc S.Context.getTrivialTypeSourceInfo(PtrToFunctionTy, Loc); 1027*f4a2713aSLionel Sambuc ConvNameLoc.NamedType.TInfo = ConvNamePtrToFunctionTSI; 1028*f4a2713aSLionel Sambuc 1029*f4a2713aSLionel Sambuc // The conversion function is a conversion to a pointer-to-function. 1030*f4a2713aSLionel Sambuc TypeSourceInfo *ConvTSI = S.Context.getTrivialTypeSourceInfo(ConvTy, Loc); 1031*f4a2713aSLionel Sambuc FunctionProtoTypeLoc ConvTL = 1032*f4a2713aSLionel Sambuc ConvTSI->getTypeLoc().getAs<FunctionProtoTypeLoc>(); 1033*f4a2713aSLionel Sambuc // Get the result of the conversion function which is a pointer-to-function. 1034*f4a2713aSLionel Sambuc PointerTypeLoc PtrToFunctionTL = 1035*f4a2713aSLionel Sambuc ConvTL.getResultLoc().getAs<PointerTypeLoc>(); 1036*f4a2713aSLionel Sambuc // Do the same for the TypeSourceInfo that is used to name the conversion 1037*f4a2713aSLionel Sambuc // operator. 1038*f4a2713aSLionel Sambuc PointerTypeLoc ConvNamePtrToFunctionTL = 1039*f4a2713aSLionel Sambuc ConvNamePtrToFunctionTSI->getTypeLoc().getAs<PointerTypeLoc>(); 1040*f4a2713aSLionel Sambuc 1041*f4a2713aSLionel Sambuc // Get the underlying function types that the conversion function will 1042*f4a2713aSLionel Sambuc // be converting to (should match the type of the call operator). 1043*f4a2713aSLionel Sambuc FunctionProtoTypeLoc CallOpConvTL = 1044*f4a2713aSLionel Sambuc PtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>(); 1045*f4a2713aSLionel Sambuc FunctionProtoTypeLoc CallOpConvNameTL = 1046*f4a2713aSLionel Sambuc ConvNamePtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>(); 1047*f4a2713aSLionel Sambuc 1048*f4a2713aSLionel Sambuc // Wire up the FunctionProtoTypeLocs with the call operator's parameters. 1049*f4a2713aSLionel Sambuc // These parameter's are essentially used to transform the name and 1050*f4a2713aSLionel Sambuc // the type of the conversion operator. By using the same parameters 1051*f4a2713aSLionel Sambuc // as the call operator's we don't have to fix any back references that 1052*f4a2713aSLionel Sambuc // the trailing return type of the call operator's uses (such as 1053*f4a2713aSLionel Sambuc // decltype(some_type<decltype(a)>::type{} + decltype(a){}) etc.) 1054*f4a2713aSLionel Sambuc // - we can simply use the return type of the call operator, and 1055*f4a2713aSLionel Sambuc // everything should work. 1056*f4a2713aSLionel Sambuc SmallVector<ParmVarDecl *, 4> InvokerParams; 1057*f4a2713aSLionel Sambuc for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { 1058*f4a2713aSLionel Sambuc ParmVarDecl *From = CallOperator->getParamDecl(I); 1059*f4a2713aSLionel Sambuc 1060*f4a2713aSLionel Sambuc InvokerParams.push_back(ParmVarDecl::Create(S.Context, 1061*f4a2713aSLionel Sambuc // Temporarily add to the TU. This is set to the invoker below. 1062*f4a2713aSLionel Sambuc S.Context.getTranslationUnitDecl(), 1063*f4a2713aSLionel Sambuc From->getLocStart(), 1064*f4a2713aSLionel Sambuc From->getLocation(), 1065*f4a2713aSLionel Sambuc From->getIdentifier(), 1066*f4a2713aSLionel Sambuc From->getType(), 1067*f4a2713aSLionel Sambuc From->getTypeSourceInfo(), 1068*f4a2713aSLionel Sambuc From->getStorageClass(), 1069*f4a2713aSLionel Sambuc /*DefaultArg=*/0)); 1070*f4a2713aSLionel Sambuc CallOpConvTL.setArg(I, From); 1071*f4a2713aSLionel Sambuc CallOpConvNameTL.setArg(I, From); 1072*f4a2713aSLionel Sambuc } 1073*f4a2713aSLionel Sambuc 1074*f4a2713aSLionel Sambuc CXXConversionDecl *Conversion 1075*f4a2713aSLionel Sambuc = CXXConversionDecl::Create(S.Context, Class, Loc, 1076*f4a2713aSLionel Sambuc DeclarationNameInfo(ConversionName, 1077*f4a2713aSLionel Sambuc Loc, ConvNameLoc), 1078*f4a2713aSLionel Sambuc ConvTy, 1079*f4a2713aSLionel Sambuc ConvTSI, 1080*f4a2713aSLionel Sambuc /*isInline=*/true, /*isExplicit=*/false, 1081*f4a2713aSLionel Sambuc /*isConstexpr=*/false, 1082*f4a2713aSLionel Sambuc CallOperator->getBody()->getLocEnd()); 1083*f4a2713aSLionel Sambuc Conversion->setAccess(AS_public); 1084*f4a2713aSLionel Sambuc Conversion->setImplicit(true); 1085*f4a2713aSLionel Sambuc 1086*f4a2713aSLionel Sambuc if (Class->isGenericLambda()) { 1087*f4a2713aSLionel Sambuc // Create a template version of the conversion operator, using the template 1088*f4a2713aSLionel Sambuc // parameter list of the function call operator. 1089*f4a2713aSLionel Sambuc FunctionTemplateDecl *TemplateCallOperator = 1090*f4a2713aSLionel Sambuc CallOperator->getDescribedFunctionTemplate(); 1091*f4a2713aSLionel Sambuc FunctionTemplateDecl *ConversionTemplate = 1092*f4a2713aSLionel Sambuc FunctionTemplateDecl::Create(S.Context, Class, 1093*f4a2713aSLionel Sambuc Loc, ConversionName, 1094*f4a2713aSLionel Sambuc TemplateCallOperator->getTemplateParameters(), 1095*f4a2713aSLionel Sambuc Conversion); 1096*f4a2713aSLionel Sambuc ConversionTemplate->setAccess(AS_public); 1097*f4a2713aSLionel Sambuc ConversionTemplate->setImplicit(true); 1098*f4a2713aSLionel Sambuc Conversion->setDescribedFunctionTemplate(ConversionTemplate); 1099*f4a2713aSLionel Sambuc Class->addDecl(ConversionTemplate); 1100*f4a2713aSLionel Sambuc } else 1101*f4a2713aSLionel Sambuc Class->addDecl(Conversion); 1102*f4a2713aSLionel Sambuc // Add a non-static member function that will be the result of 1103*f4a2713aSLionel Sambuc // the conversion with a certain unique ID. 1104*f4a2713aSLionel Sambuc DeclarationName InvokerName = &S.Context.Idents.get( 1105*f4a2713aSLionel Sambuc getLambdaStaticInvokerName()); 1106*f4a2713aSLionel Sambuc // FIXME: Instead of passing in the CallOperator->getTypeSourceInfo() 1107*f4a2713aSLionel Sambuc // we should get a prebuilt TrivialTypeSourceInfo from Context 1108*f4a2713aSLionel Sambuc // using FunctionTy & Loc and get its TypeLoc as a FunctionProtoTypeLoc 1109*f4a2713aSLionel Sambuc // then rewire the parameters accordingly, by hoisting up the InvokeParams 1110*f4a2713aSLionel Sambuc // loop below and then use its Params to set Invoke->setParams(...) below. 1111*f4a2713aSLionel Sambuc // This would avoid the 'const' qualifier of the calloperator from 1112*f4a2713aSLionel Sambuc // contaminating the type of the invoker, which is currently adjusted 1113*f4a2713aSLionel Sambuc // in SemaTemplateDeduction.cpp:DeduceTemplateArguments. Fixing the 1114*f4a2713aSLionel Sambuc // trailing return type of the invoker would require a visitor to rebuild 1115*f4a2713aSLionel Sambuc // the trailing return type and adjusting all back DeclRefExpr's to refer 1116*f4a2713aSLionel Sambuc // to the new static invoker parameters - not the call operator's. 1117*f4a2713aSLionel Sambuc CXXMethodDecl *Invoke 1118*f4a2713aSLionel Sambuc = CXXMethodDecl::Create(S.Context, Class, Loc, 1119*f4a2713aSLionel Sambuc DeclarationNameInfo(InvokerName, Loc), 1120*f4a2713aSLionel Sambuc InvokerFunctionTy, 1121*f4a2713aSLionel Sambuc CallOperator->getTypeSourceInfo(), 1122*f4a2713aSLionel Sambuc SC_Static, /*IsInline=*/true, 1123*f4a2713aSLionel Sambuc /*IsConstexpr=*/false, 1124*f4a2713aSLionel Sambuc CallOperator->getBody()->getLocEnd()); 1125*f4a2713aSLionel Sambuc for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) 1126*f4a2713aSLionel Sambuc InvokerParams[I]->setOwningFunction(Invoke); 1127*f4a2713aSLionel Sambuc Invoke->setParams(InvokerParams); 1128*f4a2713aSLionel Sambuc Invoke->setAccess(AS_private); 1129*f4a2713aSLionel Sambuc Invoke->setImplicit(true); 1130*f4a2713aSLionel Sambuc if (Class->isGenericLambda()) { 1131*f4a2713aSLionel Sambuc FunctionTemplateDecl *TemplateCallOperator = 1132*f4a2713aSLionel Sambuc CallOperator->getDescribedFunctionTemplate(); 1133*f4a2713aSLionel Sambuc FunctionTemplateDecl *StaticInvokerTemplate = FunctionTemplateDecl::Create( 1134*f4a2713aSLionel Sambuc S.Context, Class, Loc, InvokerName, 1135*f4a2713aSLionel Sambuc TemplateCallOperator->getTemplateParameters(), 1136*f4a2713aSLionel Sambuc Invoke); 1137*f4a2713aSLionel Sambuc StaticInvokerTemplate->setAccess(AS_private); 1138*f4a2713aSLionel Sambuc StaticInvokerTemplate->setImplicit(true); 1139*f4a2713aSLionel Sambuc Invoke->setDescribedFunctionTemplate(StaticInvokerTemplate); 1140*f4a2713aSLionel Sambuc Class->addDecl(StaticInvokerTemplate); 1141*f4a2713aSLionel Sambuc } else 1142*f4a2713aSLionel Sambuc Class->addDecl(Invoke); 1143*f4a2713aSLionel Sambuc } 1144*f4a2713aSLionel Sambuc 1145*f4a2713aSLionel Sambuc /// \brief Add a lambda's conversion to block pointer. 1146*f4a2713aSLionel Sambuc static void addBlockPointerConversion(Sema &S, 1147*f4a2713aSLionel Sambuc SourceRange IntroducerRange, 1148*f4a2713aSLionel Sambuc CXXRecordDecl *Class, 1149*f4a2713aSLionel Sambuc CXXMethodDecl *CallOperator) { 1150*f4a2713aSLionel Sambuc const FunctionProtoType *Proto 1151*f4a2713aSLionel Sambuc = CallOperator->getType()->getAs<FunctionProtoType>(); 1152*f4a2713aSLionel Sambuc QualType BlockPtrTy; 1153*f4a2713aSLionel Sambuc { 1154*f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); 1155*f4a2713aSLionel Sambuc ExtInfo.TypeQuals = 0; 1156*f4a2713aSLionel Sambuc QualType FunctionTy = S.Context.getFunctionType( 1157*f4a2713aSLionel Sambuc Proto->getResultType(), Proto->getArgTypes(), ExtInfo); 1158*f4a2713aSLionel Sambuc BlockPtrTy = S.Context.getBlockPointerType(FunctionTy); 1159*f4a2713aSLionel Sambuc } 1160*f4a2713aSLionel Sambuc 1161*f4a2713aSLionel Sambuc FunctionProtoType::ExtProtoInfo ExtInfo(S.Context.getDefaultCallingConvention( 1162*f4a2713aSLionel Sambuc /*IsVariadic=*/false, /*IsCXXMethod=*/true)); 1163*f4a2713aSLionel Sambuc ExtInfo.TypeQuals = Qualifiers::Const; 1164*f4a2713aSLionel Sambuc QualType ConvTy = S.Context.getFunctionType(BlockPtrTy, None, ExtInfo); 1165*f4a2713aSLionel Sambuc 1166*f4a2713aSLionel Sambuc SourceLocation Loc = IntroducerRange.getBegin(); 1167*f4a2713aSLionel Sambuc DeclarationName Name 1168*f4a2713aSLionel Sambuc = S.Context.DeclarationNames.getCXXConversionFunctionName( 1169*f4a2713aSLionel Sambuc S.Context.getCanonicalType(BlockPtrTy)); 1170*f4a2713aSLionel Sambuc DeclarationNameLoc NameLoc; 1171*f4a2713aSLionel Sambuc NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc); 1172*f4a2713aSLionel Sambuc CXXConversionDecl *Conversion 1173*f4a2713aSLionel Sambuc = CXXConversionDecl::Create(S.Context, Class, Loc, 1174*f4a2713aSLionel Sambuc DeclarationNameInfo(Name, Loc, NameLoc), 1175*f4a2713aSLionel Sambuc ConvTy, 1176*f4a2713aSLionel Sambuc S.Context.getTrivialTypeSourceInfo(ConvTy, Loc), 1177*f4a2713aSLionel Sambuc /*isInline=*/true, /*isExplicit=*/false, 1178*f4a2713aSLionel Sambuc /*isConstexpr=*/false, 1179*f4a2713aSLionel Sambuc CallOperator->getBody()->getLocEnd()); 1180*f4a2713aSLionel Sambuc Conversion->setAccess(AS_public); 1181*f4a2713aSLionel Sambuc Conversion->setImplicit(true); 1182*f4a2713aSLionel Sambuc Class->addDecl(Conversion); 1183*f4a2713aSLionel Sambuc } 1184*f4a2713aSLionel Sambuc 1185*f4a2713aSLionel Sambuc ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, 1186*f4a2713aSLionel Sambuc Scope *CurScope, 1187*f4a2713aSLionel Sambuc bool IsInstantiation) { 1188*f4a2713aSLionel Sambuc // Collect information from the lambda scope. 1189*f4a2713aSLionel Sambuc SmallVector<LambdaExpr::Capture, 4> Captures; 1190*f4a2713aSLionel Sambuc SmallVector<Expr *, 4> CaptureInits; 1191*f4a2713aSLionel Sambuc LambdaCaptureDefault CaptureDefault; 1192*f4a2713aSLionel Sambuc SourceLocation CaptureDefaultLoc; 1193*f4a2713aSLionel Sambuc CXXRecordDecl *Class; 1194*f4a2713aSLionel Sambuc CXXMethodDecl *CallOperator; 1195*f4a2713aSLionel Sambuc SourceRange IntroducerRange; 1196*f4a2713aSLionel Sambuc bool ExplicitParams; 1197*f4a2713aSLionel Sambuc bool ExplicitResultType; 1198*f4a2713aSLionel Sambuc bool LambdaExprNeedsCleanups; 1199*f4a2713aSLionel Sambuc bool ContainsUnexpandedParameterPack; 1200*f4a2713aSLionel Sambuc SmallVector<VarDecl *, 4> ArrayIndexVars; 1201*f4a2713aSLionel Sambuc SmallVector<unsigned, 4> ArrayIndexStarts; 1202*f4a2713aSLionel Sambuc { 1203*f4a2713aSLionel Sambuc LambdaScopeInfo *LSI = getCurLambda(); 1204*f4a2713aSLionel Sambuc CallOperator = LSI->CallOperator; 1205*f4a2713aSLionel Sambuc Class = LSI->Lambda; 1206*f4a2713aSLionel Sambuc IntroducerRange = LSI->IntroducerRange; 1207*f4a2713aSLionel Sambuc ExplicitParams = LSI->ExplicitParams; 1208*f4a2713aSLionel Sambuc ExplicitResultType = !LSI->HasImplicitReturnType; 1209*f4a2713aSLionel Sambuc LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups; 1210*f4a2713aSLionel Sambuc ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack; 1211*f4a2713aSLionel Sambuc ArrayIndexVars.swap(LSI->ArrayIndexVars); 1212*f4a2713aSLionel Sambuc ArrayIndexStarts.swap(LSI->ArrayIndexStarts); 1213*f4a2713aSLionel Sambuc 1214*f4a2713aSLionel Sambuc // Translate captures. 1215*f4a2713aSLionel Sambuc for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) { 1216*f4a2713aSLionel Sambuc LambdaScopeInfo::Capture From = LSI->Captures[I]; 1217*f4a2713aSLionel Sambuc assert(!From.isBlockCapture() && "Cannot capture __block variables"); 1218*f4a2713aSLionel Sambuc bool IsImplicit = I >= LSI->NumExplicitCaptures; 1219*f4a2713aSLionel Sambuc 1220*f4a2713aSLionel Sambuc // Handle 'this' capture. 1221*f4a2713aSLionel Sambuc if (From.isThisCapture()) { 1222*f4a2713aSLionel Sambuc Captures.push_back(LambdaExpr::Capture(From.getLocation(), 1223*f4a2713aSLionel Sambuc IsImplicit, 1224*f4a2713aSLionel Sambuc LCK_This)); 1225*f4a2713aSLionel Sambuc CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(), 1226*f4a2713aSLionel Sambuc getCurrentThisType(), 1227*f4a2713aSLionel Sambuc /*isImplicit=*/true)); 1228*f4a2713aSLionel Sambuc continue; 1229*f4a2713aSLionel Sambuc } 1230*f4a2713aSLionel Sambuc 1231*f4a2713aSLionel Sambuc VarDecl *Var = From.getVariable(); 1232*f4a2713aSLionel Sambuc LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef; 1233*f4a2713aSLionel Sambuc Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit, 1234*f4a2713aSLionel Sambuc Kind, Var, From.getEllipsisLoc())); 1235*f4a2713aSLionel Sambuc CaptureInits.push_back(From.getInitExpr()); 1236*f4a2713aSLionel Sambuc } 1237*f4a2713aSLionel Sambuc 1238*f4a2713aSLionel Sambuc switch (LSI->ImpCaptureStyle) { 1239*f4a2713aSLionel Sambuc case CapturingScopeInfo::ImpCap_None: 1240*f4a2713aSLionel Sambuc CaptureDefault = LCD_None; 1241*f4a2713aSLionel Sambuc break; 1242*f4a2713aSLionel Sambuc 1243*f4a2713aSLionel Sambuc case CapturingScopeInfo::ImpCap_LambdaByval: 1244*f4a2713aSLionel Sambuc CaptureDefault = LCD_ByCopy; 1245*f4a2713aSLionel Sambuc break; 1246*f4a2713aSLionel Sambuc 1247*f4a2713aSLionel Sambuc case CapturingScopeInfo::ImpCap_CapturedRegion: 1248*f4a2713aSLionel Sambuc case CapturingScopeInfo::ImpCap_LambdaByref: 1249*f4a2713aSLionel Sambuc CaptureDefault = LCD_ByRef; 1250*f4a2713aSLionel Sambuc break; 1251*f4a2713aSLionel Sambuc 1252*f4a2713aSLionel Sambuc case CapturingScopeInfo::ImpCap_Block: 1253*f4a2713aSLionel Sambuc llvm_unreachable("block capture in lambda"); 1254*f4a2713aSLionel Sambuc break; 1255*f4a2713aSLionel Sambuc } 1256*f4a2713aSLionel Sambuc CaptureDefaultLoc = LSI->CaptureDefaultLoc; 1257*f4a2713aSLionel Sambuc 1258*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p4: 1259*f4a2713aSLionel Sambuc // If a lambda-expression does not include a 1260*f4a2713aSLionel Sambuc // trailing-return-type, it is as if the trailing-return-type 1261*f4a2713aSLionel Sambuc // denotes the following type: 1262*f4a2713aSLionel Sambuc // 1263*f4a2713aSLionel Sambuc // Skip for C++1y return type deduction semantics which uses 1264*f4a2713aSLionel Sambuc // different machinery. 1265*f4a2713aSLionel Sambuc // FIXME: Refactor and Merge the return type deduction machinery. 1266*f4a2713aSLionel Sambuc // FIXME: Assumes current resolution to core issue 975. 1267*f4a2713aSLionel Sambuc if (LSI->HasImplicitReturnType && !getLangOpts().CPlusPlus1y) { 1268*f4a2713aSLionel Sambuc deduceClosureReturnType(*LSI); 1269*f4a2713aSLionel Sambuc 1270*f4a2713aSLionel Sambuc // - if there are no return statements in the 1271*f4a2713aSLionel Sambuc // compound-statement, or all return statements return 1272*f4a2713aSLionel Sambuc // either an expression of type void or no expression or 1273*f4a2713aSLionel Sambuc // braced-init-list, the type void; 1274*f4a2713aSLionel Sambuc if (LSI->ReturnType.isNull()) { 1275*f4a2713aSLionel Sambuc LSI->ReturnType = Context.VoidTy; 1276*f4a2713aSLionel Sambuc } 1277*f4a2713aSLionel Sambuc 1278*f4a2713aSLionel Sambuc // Create a function type with the inferred return type. 1279*f4a2713aSLionel Sambuc const FunctionProtoType *Proto 1280*f4a2713aSLionel Sambuc = CallOperator->getType()->getAs<FunctionProtoType>(); 1281*f4a2713aSLionel Sambuc QualType FunctionTy = Context.getFunctionType( 1282*f4a2713aSLionel Sambuc LSI->ReturnType, Proto->getArgTypes(), Proto->getExtProtoInfo()); 1283*f4a2713aSLionel Sambuc CallOperator->setType(FunctionTy); 1284*f4a2713aSLionel Sambuc } 1285*f4a2713aSLionel Sambuc // C++ [expr.prim.lambda]p7: 1286*f4a2713aSLionel Sambuc // The lambda-expression's compound-statement yields the 1287*f4a2713aSLionel Sambuc // function-body (8.4) of the function call operator [...]. 1288*f4a2713aSLionel Sambuc ActOnFinishFunctionBody(CallOperator, Body, IsInstantiation); 1289*f4a2713aSLionel Sambuc CallOperator->setLexicalDeclContext(Class); 1290*f4a2713aSLionel Sambuc Decl *TemplateOrNonTemplateCallOperatorDecl = 1291*f4a2713aSLionel Sambuc CallOperator->getDescribedFunctionTemplate() 1292*f4a2713aSLionel Sambuc ? CallOperator->getDescribedFunctionTemplate() 1293*f4a2713aSLionel Sambuc : cast<Decl>(CallOperator); 1294*f4a2713aSLionel Sambuc 1295*f4a2713aSLionel Sambuc TemplateOrNonTemplateCallOperatorDecl->setLexicalDeclContext(Class); 1296*f4a2713aSLionel Sambuc Class->addDecl(TemplateOrNonTemplateCallOperatorDecl); 1297*f4a2713aSLionel Sambuc 1298*f4a2713aSLionel Sambuc PopExpressionEvaluationContext(); 1299*f4a2713aSLionel Sambuc 1300*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p6: 1301*f4a2713aSLionel Sambuc // The closure type for a lambda-expression with no lambda-capture 1302*f4a2713aSLionel Sambuc // has a public non-virtual non-explicit const conversion function 1303*f4a2713aSLionel Sambuc // to pointer to function having the same parameter and return 1304*f4a2713aSLionel Sambuc // types as the closure type's function call operator. 1305*f4a2713aSLionel Sambuc if (Captures.empty() && CaptureDefault == LCD_None) 1306*f4a2713aSLionel Sambuc addFunctionPointerConversion(*this, IntroducerRange, Class, 1307*f4a2713aSLionel Sambuc CallOperator); 1308*f4a2713aSLionel Sambuc 1309*f4a2713aSLionel Sambuc // Objective-C++: 1310*f4a2713aSLionel Sambuc // The closure type for a lambda-expression has a public non-virtual 1311*f4a2713aSLionel Sambuc // non-explicit const conversion function to a block pointer having the 1312*f4a2713aSLionel Sambuc // same parameter and return types as the closure type's function call 1313*f4a2713aSLionel Sambuc // operator. 1314*f4a2713aSLionel Sambuc // FIXME: Fix generic lambda to block conversions. 1315*f4a2713aSLionel Sambuc if (getLangOpts().Blocks && getLangOpts().ObjC1 && 1316*f4a2713aSLionel Sambuc !Class->isGenericLambda()) 1317*f4a2713aSLionel Sambuc addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator); 1318*f4a2713aSLionel Sambuc 1319*f4a2713aSLionel Sambuc // Finalize the lambda class. 1320*f4a2713aSLionel Sambuc SmallVector<Decl*, 4> Fields; 1321*f4a2713aSLionel Sambuc for (RecordDecl::field_iterator i = Class->field_begin(), 1322*f4a2713aSLionel Sambuc e = Class->field_end(); i != e; ++i) 1323*f4a2713aSLionel Sambuc Fields.push_back(*i); 1324*f4a2713aSLionel Sambuc ActOnFields(0, Class->getLocation(), Class, Fields, 1325*f4a2713aSLionel Sambuc SourceLocation(), SourceLocation(), 0); 1326*f4a2713aSLionel Sambuc CheckCompletedCXXClass(Class); 1327*f4a2713aSLionel Sambuc } 1328*f4a2713aSLionel Sambuc 1329*f4a2713aSLionel Sambuc if (LambdaExprNeedsCleanups) 1330*f4a2713aSLionel Sambuc ExprNeedsCleanups = true; 1331*f4a2713aSLionel Sambuc 1332*f4a2713aSLionel Sambuc LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, 1333*f4a2713aSLionel Sambuc CaptureDefault, CaptureDefaultLoc, 1334*f4a2713aSLionel Sambuc Captures, 1335*f4a2713aSLionel Sambuc ExplicitParams, ExplicitResultType, 1336*f4a2713aSLionel Sambuc CaptureInits, ArrayIndexVars, 1337*f4a2713aSLionel Sambuc ArrayIndexStarts, Body->getLocEnd(), 1338*f4a2713aSLionel Sambuc ContainsUnexpandedParameterPack); 1339*f4a2713aSLionel Sambuc 1340*f4a2713aSLionel Sambuc if (!CurContext->isDependentContext()) { 1341*f4a2713aSLionel Sambuc switch (ExprEvalContexts.back().Context) { 1342*f4a2713aSLionel Sambuc // C++11 [expr.prim.lambda]p2: 1343*f4a2713aSLionel Sambuc // A lambda-expression shall not appear in an unevaluated operand 1344*f4a2713aSLionel Sambuc // (Clause 5). 1345*f4a2713aSLionel Sambuc case Unevaluated: 1346*f4a2713aSLionel Sambuc case UnevaluatedAbstract: 1347*f4a2713aSLionel Sambuc // C++1y [expr.const]p2: 1348*f4a2713aSLionel Sambuc // A conditional-expression e is a core constant expression unless the 1349*f4a2713aSLionel Sambuc // evaluation of e, following the rules of the abstract machine, would 1350*f4a2713aSLionel Sambuc // evaluate [...] a lambda-expression. 1351*f4a2713aSLionel Sambuc // 1352*f4a2713aSLionel Sambuc // This is technically incorrect, there are some constant evaluated contexts 1353*f4a2713aSLionel Sambuc // where this should be allowed. We should probably fix this when DR1607 is 1354*f4a2713aSLionel Sambuc // ratified, it lays out the exact set of conditions where we shouldn't 1355*f4a2713aSLionel Sambuc // allow a lambda-expression. 1356*f4a2713aSLionel Sambuc case ConstantEvaluated: 1357*f4a2713aSLionel Sambuc // We don't actually diagnose this case immediately, because we 1358*f4a2713aSLionel Sambuc // could be within a context where we might find out later that 1359*f4a2713aSLionel Sambuc // the expression is potentially evaluated (e.g., for typeid). 1360*f4a2713aSLionel Sambuc ExprEvalContexts.back().Lambdas.push_back(Lambda); 1361*f4a2713aSLionel Sambuc break; 1362*f4a2713aSLionel Sambuc 1363*f4a2713aSLionel Sambuc case PotentiallyEvaluated: 1364*f4a2713aSLionel Sambuc case PotentiallyEvaluatedIfUsed: 1365*f4a2713aSLionel Sambuc break; 1366*f4a2713aSLionel Sambuc } 1367*f4a2713aSLionel Sambuc } 1368*f4a2713aSLionel Sambuc 1369*f4a2713aSLionel Sambuc return MaybeBindToTemporary(Lambda); 1370*f4a2713aSLionel Sambuc } 1371*f4a2713aSLionel Sambuc 1372*f4a2713aSLionel Sambuc ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation, 1373*f4a2713aSLionel Sambuc SourceLocation ConvLocation, 1374*f4a2713aSLionel Sambuc CXXConversionDecl *Conv, 1375*f4a2713aSLionel Sambuc Expr *Src) { 1376*f4a2713aSLionel Sambuc // Make sure that the lambda call operator is marked used. 1377*f4a2713aSLionel Sambuc CXXRecordDecl *Lambda = Conv->getParent(); 1378*f4a2713aSLionel Sambuc CXXMethodDecl *CallOperator 1379*f4a2713aSLionel Sambuc = cast<CXXMethodDecl>( 1380*f4a2713aSLionel Sambuc Lambda->lookup( 1381*f4a2713aSLionel Sambuc Context.DeclarationNames.getCXXOperatorName(OO_Call)).front()); 1382*f4a2713aSLionel Sambuc CallOperator->setReferenced(); 1383*f4a2713aSLionel Sambuc CallOperator->markUsed(Context); 1384*f4a2713aSLionel Sambuc 1385*f4a2713aSLionel Sambuc ExprResult Init = PerformCopyInitialization( 1386*f4a2713aSLionel Sambuc InitializedEntity::InitializeBlock(ConvLocation, 1387*f4a2713aSLionel Sambuc Src->getType(), 1388*f4a2713aSLionel Sambuc /*NRVO=*/false), 1389*f4a2713aSLionel Sambuc CurrentLocation, Src); 1390*f4a2713aSLionel Sambuc if (!Init.isInvalid()) 1391*f4a2713aSLionel Sambuc Init = ActOnFinishFullExpr(Init.take()); 1392*f4a2713aSLionel Sambuc 1393*f4a2713aSLionel Sambuc if (Init.isInvalid()) 1394*f4a2713aSLionel Sambuc return ExprError(); 1395*f4a2713aSLionel Sambuc 1396*f4a2713aSLionel Sambuc // Create the new block to be returned. 1397*f4a2713aSLionel Sambuc BlockDecl *Block = BlockDecl::Create(Context, CurContext, ConvLocation); 1398*f4a2713aSLionel Sambuc 1399*f4a2713aSLionel Sambuc // Set the type information. 1400*f4a2713aSLionel Sambuc Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo()); 1401*f4a2713aSLionel Sambuc Block->setIsVariadic(CallOperator->isVariadic()); 1402*f4a2713aSLionel Sambuc Block->setBlockMissingReturnType(false); 1403*f4a2713aSLionel Sambuc 1404*f4a2713aSLionel Sambuc // Add parameters. 1405*f4a2713aSLionel Sambuc SmallVector<ParmVarDecl *, 4> BlockParams; 1406*f4a2713aSLionel Sambuc for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { 1407*f4a2713aSLionel Sambuc ParmVarDecl *From = CallOperator->getParamDecl(I); 1408*f4a2713aSLionel Sambuc BlockParams.push_back(ParmVarDecl::Create(Context, Block, 1409*f4a2713aSLionel Sambuc From->getLocStart(), 1410*f4a2713aSLionel Sambuc From->getLocation(), 1411*f4a2713aSLionel Sambuc From->getIdentifier(), 1412*f4a2713aSLionel Sambuc From->getType(), 1413*f4a2713aSLionel Sambuc From->getTypeSourceInfo(), 1414*f4a2713aSLionel Sambuc From->getStorageClass(), 1415*f4a2713aSLionel Sambuc /*DefaultArg=*/0)); 1416*f4a2713aSLionel Sambuc } 1417*f4a2713aSLionel Sambuc Block->setParams(BlockParams); 1418*f4a2713aSLionel Sambuc 1419*f4a2713aSLionel Sambuc Block->setIsConversionFromLambda(true); 1420*f4a2713aSLionel Sambuc 1421*f4a2713aSLionel Sambuc // Add capture. The capture uses a fake variable, which doesn't correspond 1422*f4a2713aSLionel Sambuc // to any actual memory location. However, the initializer copy-initializes 1423*f4a2713aSLionel Sambuc // the lambda object. 1424*f4a2713aSLionel Sambuc TypeSourceInfo *CapVarTSI = 1425*f4a2713aSLionel Sambuc Context.getTrivialTypeSourceInfo(Src->getType()); 1426*f4a2713aSLionel Sambuc VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation, 1427*f4a2713aSLionel Sambuc ConvLocation, 0, 1428*f4a2713aSLionel Sambuc Src->getType(), CapVarTSI, 1429*f4a2713aSLionel Sambuc SC_None); 1430*f4a2713aSLionel Sambuc BlockDecl::Capture Capture(/*Variable=*/CapVar, /*ByRef=*/false, 1431*f4a2713aSLionel Sambuc /*Nested=*/false, /*Copy=*/Init.take()); 1432*f4a2713aSLionel Sambuc Block->setCaptures(Context, &Capture, &Capture + 1, 1433*f4a2713aSLionel Sambuc /*CapturesCXXThis=*/false); 1434*f4a2713aSLionel Sambuc 1435*f4a2713aSLionel Sambuc // Add a fake function body to the block. IR generation is responsible 1436*f4a2713aSLionel Sambuc // for filling in the actual body, which cannot be expressed as an AST. 1437*f4a2713aSLionel Sambuc Block->setBody(new (Context) CompoundStmt(ConvLocation)); 1438*f4a2713aSLionel Sambuc 1439*f4a2713aSLionel Sambuc // Create the block literal expression. 1440*f4a2713aSLionel Sambuc Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType()); 1441*f4a2713aSLionel Sambuc ExprCleanupObjects.push_back(Block); 1442*f4a2713aSLionel Sambuc ExprNeedsCleanups = true; 1443*f4a2713aSLionel Sambuc 1444*f4a2713aSLionel Sambuc return BuildBlock; 1445*f4a2713aSLionel Sambuc } 1446