1e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor//===--- SemaLambda.cpp - Semantic Analysis for C++11 Lambdas -------------===// 2e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// 3e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// The LLVM Compiler Infrastructure 4e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// 5e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// This file is distributed under the University of Illinois Open Source 6e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// License. See LICENSE.TXT for details. 7e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// 8e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor//===----------------------------------------------------------------------===// 9e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// 10e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// This file implements semantic analysis for C++ lambda expressions. 11e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor// 12e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor//===----------------------------------------------------------------------===// 13e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/DeclSpec.h" 1455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/AST/ExprCXX.h" 1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/Preprocessor.h" 16e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/Initialization.h" 17e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/Lookup.h" 185878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor#include "clang/Sema/Scope.h" 19e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/ScopeInfo.h" 20e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/SemaInternal.h" 210d8e9646bc000bab521ce52ed294209a92298cefRichard Smith#include "TypeLocBuilder.h" 22e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregorusing namespace clang; 23e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregorusing namespace sema; 24e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 25f4b7de1cef3007cc0479775638198287384d9af1Douglas GregorCXXRecordDecl *Sema::createLambdaClosureType(SourceRange IntroducerRange, 268da8a660128180a31479216111ff9b19b11c95b4Eli Friedman TypeSourceInfo *Info, 27f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor bool KnownDependent) { 28e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor DeclContext *DC = CurContext; 29e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) 30e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor DC = DC->getParent(); 31dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 32e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Start constructing the lambda class. 338da8a660128180a31479216111ff9b19b11c95b4Eli Friedman CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(Context, DC, Info, 34f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor IntroducerRange.getBegin(), 35f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor KnownDependent); 36fa07ab57eb565b4a00712adcefb29d96b445bfcdDouglas Gregor DC->addDecl(Class); 37dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 38dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor return Class; 39dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor} 40e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 41f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor/// \brief Determine whether the given context is or is enclosed in an inline 42f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor/// function. 43f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregorstatic bool isInInlineFunction(const DeclContext *DC) { 44f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor while (!DC->isFileContext()) { 45f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) 46f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor if (FD->isInlined()) 47f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor return true; 48f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor 49f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor DC = DC->getLexicalParent(); 50f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor } 51f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor 52f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor return false; 53f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor} 54f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor 5507369dde9d72213bf8a48288cd8b29999af9a40cEli FriedmanMangleNumberingContext * 565e867c8a07d82da0d3b0a43402ee4f1c6ba416e9Eli FriedmanSema::getCurrentMangleNumberContext(const DeclContext *DC, 5707369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman Decl *&ManglingContextDecl) { 5807369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // Compute the context for allocating mangling numbers in the current 5907369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // expression, if the ABI requires them. 6007369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman ManglingContextDecl = ExprEvalContexts.back().ManglingContextDecl; 6107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman 6207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman enum ContextKind { 6307369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman Normal, 6407369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman DefaultArgument, 6507369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman DataMember, 6607369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman StaticDataMember 6707369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } Kind = Normal; 6807369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman 6907369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // Default arguments of member function parameters that appear in a class 7007369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // definition, as well as the initializers of data members, receive special 7107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // treatment. Identify them. 7207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if (ManglingContextDecl) { 7307369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) { 7407369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if (const DeclContext *LexicalDC 7507369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman = Param->getDeclContext()->getLexicalParent()) 7607369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if (LexicalDC->isRecord()) 7707369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman Kind = DefaultArgument; 7807369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) { 7907369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if (Var->getDeclContext()->isRecord()) 8007369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman Kind = StaticDataMember; 8107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } else if (isa<FieldDecl>(ManglingContextDecl)) { 8207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman Kind = DataMember; 8307369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } 8407369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } 8507369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman 8607369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // Itanium ABI [5.1.7]: 8707369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // In the following contexts [...] the one-definition rule requires closure 8807369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // types in different translation units to "correspond": 8907369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman bool IsInNonspecializedTemplate = 9007369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman !ActiveTemplateInstantiations.empty() || CurContext->isDependentContext(); 9107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman switch (Kind) { 9207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman case Normal: 9307369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // -- the bodies of non-exported nonspecialized template functions 9407369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // -- the bodies of inline functions 9507369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if ((IsInNonspecializedTemplate && 9607369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman !(ManglingContextDecl && isa<ParmVarDecl>(ManglingContextDecl))) || 9707369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman isInInlineFunction(CurContext)) { 9807369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman ManglingContextDecl = 0; 9907369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman return &Context.getManglingNumberContext(DC); 10007369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } 10107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman 10207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman ManglingContextDecl = 0; 10307369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman return 0; 10407369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman 10507369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman case StaticDataMember: 10607369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // -- the initializers of nonspecialized static members of template classes 10707369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if (!IsInNonspecializedTemplate) { 10807369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman ManglingContextDecl = 0; 10907369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman return 0; 11007369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } 11107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // Fall through to get the current context. 11207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman 11307369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman case DataMember: 11407369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // -- the in-class initializers of class members 11507369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman case DefaultArgument: 11607369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman // -- default arguments appearing in class definitions 11707369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman return &ExprEvalContexts.back().getMangleNumberingContext(); 11807369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman } 119ce9cd911c5b88d375ea1ff9f4719e4d63ce0fa7aAndy Gibbs 120ce9cd911c5b88d375ea1ff9f4719e4d63ce0fa7aAndy Gibbs llvm_unreachable("unexpected context"); 12107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman} 12207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman 123dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas GregorCXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, 124f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor SourceRange IntroducerRange, 125f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor TypeSourceInfo *MethodType, 126f54486acc1cadf2791c3916ece66fded1e57ba0bDouglas Gregor SourceLocation EndLoc, 127cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko ArrayRef<ParmVarDecl *> Params) { 128dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // C++11 [expr.prim.lambda]p5: 129dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // The closure type for a lambda-expression has a public inline function 130dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // call operator (13.5.4) whose parameters and return type are described by 131dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // the lambda-expression's parameter-declaration-clause and 132dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // trailing-return-type respectively. 133dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor DeclarationName MethodName 134dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor = Context.DeclarationNames.getCXXOperatorName(OO_Call); 135dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor DeclarationNameLoc MethodNameLoc; 136dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor MethodNameLoc.CXXOperatorName.BeginOpNameLoc 137dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor = IntroducerRange.getBegin().getRawEncoding(); 138dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor MethodNameLoc.CXXOperatorName.EndOpNameLoc 139dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor = IntroducerRange.getEnd().getRawEncoding(); 140dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor CXXMethodDecl *Method 141dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor = CXXMethodDecl::Create(Context, Class, EndLoc, 142dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor DeclarationNameInfo(MethodName, 143dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor IntroducerRange.getBegin(), 144dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor MethodNameLoc), 145dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor MethodType->getType(), MethodType, 146dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor SC_None, 147dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor /*isInline=*/true, 148dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor /*isConstExpr=*/false, 149dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor EndLoc); 150dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor Method->setAccess(AS_public); 151dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 152dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // Temporarily set the lexical declaration context to the current 153dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // context, so that the Scope stack matches the lexical nesting. 154fa07ab57eb565b4a00712adcefb29d96b445bfcdDouglas Gregor Method->setLexicalDeclContext(CurContext); 155dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 156c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor // Add parameters. 157c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor if (!Params.empty()) { 158c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor Method->setParams(Params); 159c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor CheckParmsForFunctionDef(const_cast<ParmVarDecl **>(Params.begin()), 160c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor const_cast<ParmVarDecl **>(Params.end()), 161c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor /*CheckParameterNames=*/false); 162c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor 163c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor for (CXXMethodDecl::param_iterator P = Method->param_begin(), 164c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor PEnd = Method->param_end(); 165c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor P != PEnd; ++P) 166c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor (*P)->setOwningFunction(Method); 167c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor } 168adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith 16907369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman Decl *ManglingContextDecl; 17007369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman if (MangleNumberingContext *MCtx = 17107369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman getCurrentMangleNumberContext(Class->getDeclContext(), 17207369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman ManglingContextDecl)) { 17307369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman unsigned ManglingNumber = MCtx->getManglingNumber(Method); 17407369dde9d72213bf8a48288cd8b29999af9a40cEli Friedman Class->setLambdaMangling(ManglingNumber, ManglingContextDecl); 175adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith } 176adb1d4c18ee83249d4cffc99ef902f98e846092aRichard Smith 177dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor return Method; 178dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor} 179dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 180dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas GregorLambdaScopeInfo *Sema::enterLambdaScope(CXXMethodDecl *CallOperator, 181dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor SourceRange IntroducerRange, 182dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LambdaCaptureDefault CaptureDefault, 183dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor bool ExplicitParams, 184dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor bool ExplicitResultType, 185dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor bool Mutable) { 186dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor PushLambdaScope(CallOperator->getParent(), CallOperator); 187dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LambdaScopeInfo *LSI = getCurLambda(); 188dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor if (CaptureDefault == LCD_ByCopy) 189dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval; 190dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor else if (CaptureDefault == LCD_ByRef) 191dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref; 192dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->IntroducerRange = IntroducerRange; 193dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->ExplicitParams = ExplicitParams; 194dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->Mutable = Mutable; 195dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 196dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor if (ExplicitResultType) { 197dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->ReturnType = CallOperator->getResultType(); 19853393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor 19953393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor if (!LSI->ReturnType->isDependentType() && 20053393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor !LSI->ReturnType->isVoidType()) { 20153393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor if (RequireCompleteType(CallOperator->getLocStart(), LSI->ReturnType, 20253393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor diag::err_lambda_incomplete_result)) { 20353393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor // Do nothing. 20453393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor } 20553393f23d8b767f976427a6d45b310bf37dd91c4Douglas Gregor } 206dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor } else { 207dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->HasImplicitReturnType = true; 208dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor } 209dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 210dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor return LSI; 211dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor} 212dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 213dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregorvoid Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) { 214dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LSI->finishedExplicitCaptures(); 215dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor} 216dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 217c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregorvoid Sema::addLambdaParameters(CXXMethodDecl *CallOperator, Scope *CurScope) { 218dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // Introduce our parameters into the function scope 219dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor for (unsigned p = 0, NumParams = CallOperator->getNumParams(); 220dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor p < NumParams; ++p) { 221dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor ParmVarDecl *Param = CallOperator->getParamDecl(p); 222dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 223dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // If this has an identifier, add it to the scope stack. 224dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor if (CurScope && Param->getIdentifier()) { 225dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor CheckShadow(CurScope, Param); 226dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 227dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor PushOnScopeChains(Param, CurScope); 228dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor } 229dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor } 230dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor} 231dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 23241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// If this expression is an enumerator-like expression of some type 23341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// T, return the type T; otherwise, return null. 23441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// 23541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// Pointer comparisons on the result here should always work because 23641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// it's derived from either the parent of an EnumConstantDecl 23741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// (i.e. the definition) or the declaration returned by 23841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// EnumType::getDecl() (i.e. the definition). 23941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCallstatic EnumDecl *findEnumForBlockReturn(Expr *E) { 24041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // An expression is an enumerator-like expression of type T if, 24141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // ignoring parens and parens-like expressions: 24241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall E = E->IgnoreParens(); 24341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 24441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - it is an enumerator whose enum type is T or 24541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { 24641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (EnumConstantDecl *D 24741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { 24841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return cast<EnumDecl>(D->getDeclContext()); 24941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } 25041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return 0; 2517dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose } 2527dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 25341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - it is a comma expression whose RHS is an enumerator-like 25441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // expression of type T or 25541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { 25641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (BO->getOpcode() == BO_Comma) 25741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return findEnumForBlockReturn(BO->getRHS()); 25841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return 0; 2597dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose } 2607dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 26141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - it is a statement-expression whose value expression is an 26241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // enumerator-like expression of type T or 26341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (StmtExpr *SE = dyn_cast<StmtExpr>(E)) { 26441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (Expr *last = dyn_cast_or_null<Expr>(SE->getSubStmt()->body_back())) 26541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return findEnumForBlockReturn(last); 26641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return 0; 2677dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose } 2687dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 26941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - it is a ternary conditional operator (not the GNU ?: 27041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // extension) whose second and third operands are 27141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // enumerator-like expressions of type T or 27241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { 27341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (EnumDecl *ED = findEnumForBlockReturn(CO->getTrueExpr())) 27441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (ED == findEnumForBlockReturn(CO->getFalseExpr())) 27541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return ED; 27641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return 0; 2777dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose } 2787dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 27941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // (implicitly:) 28041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - it is an implicit integral conversion applied to an 28141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // enumerator-like expression of type T or 28241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { 28370133b5b4ad863f7d73fabfaf799b2f4e30d98ecJohn McCall // We can sometimes see integral conversions in valid 28470133b5b4ad863f7d73fabfaf799b2f4e30d98ecJohn McCall // enumerator-like expressions. 28541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (ICE->getCastKind() == CK_IntegralCast) 28641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return findEnumForBlockReturn(ICE->getSubExpr()); 28770133b5b4ad863f7d73fabfaf799b2f4e30d98ecJohn McCall 28870133b5b4ad863f7d73fabfaf799b2f4e30d98ecJohn McCall // Otherwise, just rely on the type. 2897dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose } 2907dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 29141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - it is an expression of that formal enum type. 29241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (const EnumType *ET = E->getType()->getAs<EnumType>()) { 29341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return ET->getDecl(); 29441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } 29541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 29641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Otherwise, nope. 29741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return 0; 29841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall} 29941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 30041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// Attempt to find a type T for which the returned expression of the 30141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// given statement is an enumerator-like expression of that type. 30241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCallstatic EnumDecl *findEnumForBlockReturn(ReturnStmt *ret) { 30341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (Expr *retValue = ret->getRetValue()) 30441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return findEnumForBlockReturn(retValue); 30541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return 0; 30641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall} 30741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 30841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// Attempt to find a common type T for which all of the returned 30941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// expressions in a block are enumerator-like expressions of that 31041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// type. 31141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCallstatic EnumDecl *findCommonEnumForBlockReturns(ArrayRef<ReturnStmt*> returns) { 31241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall ArrayRef<ReturnStmt*>::iterator i = returns.begin(), e = returns.end(); 31341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 31441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Try to find one for the first return. 31541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall EnumDecl *ED = findEnumForBlockReturn(*i); 31641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (!ED) return 0; 31741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 31841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Check that the rest of the returns have the same enum. 31941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall for (++i; i != e; ++i) { 32041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (findEnumForBlockReturn(*i) != ED) 32141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return 0; 32241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } 32341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 32441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Never infer an anonymous enum type. 32541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (!ED->hasNameForLinkage()) return 0; 32641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 32741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return ED; 32841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall} 32941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 33041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// Adjust the given return statements so that they formally return 33141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall/// the given type. It should require, at most, an IntegralCast. 33241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCallstatic void adjustBlockReturnsToEnum(Sema &S, ArrayRef<ReturnStmt*> returns, 33341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall QualType returnType) { 33441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall for (ArrayRef<ReturnStmt*>::iterator 33541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall i = returns.begin(), e = returns.end(); i != e; ++i) { 33641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall ReturnStmt *ret = *i; 33741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall Expr *retValue = ret->getRetValue(); 33841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (S.Context.hasSameType(retValue->getType(), returnType)) 33941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall continue; 34041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 34141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Right now we only support integral fixup casts. 34241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall assert(returnType->isIntegralOrUnscopedEnumerationType()); 34341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall assert(retValue->getType()->isIntegralOrUnscopedEnumerationType()); 34441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 34541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(retValue); 34641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 34741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall Expr *E = (cleanups ? cleanups->getSubExpr() : retValue); 34841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall E = ImplicitCastExpr::Create(S.Context, returnType, CK_IntegralCast, 34941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall E, /*base path*/ 0, VK_RValue); 35041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (cleanups) { 35141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall cleanups->setSubExpr(E); 35241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } else { 35341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall ret->setRetValue(E); 35441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } 35541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } 3567dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose} 3577dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 3587dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rosevoid Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) { 3597dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose assert(CSI.HasImplicitReturnType); 3607dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 36141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // C++ Core Issue #975, proposed resolution: 36241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // If a lambda-expression does not include a trailing-return-type, 36341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // it is as if the trailing-return-type denotes the following type: 36441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - if there are no return statements in the compound-statement, 36541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // or all return statements return either an expression of type 36641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // void or no expression or braced-init-list, the type void; 36741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - otherwise, if all return statements return an expression 36841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // and the types of the returned expressions after 36941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // lvalue-to-rvalue conversion (4.1 [conv.lval]), 37041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // array-to-pointer conversion (4.2 [conv.array]), and 37141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // function-to-pointer conversion (4.3 [conv.func]) are the 37241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // same, that common type; 37341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // - otherwise, the program is ill-formed. 37441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // 37541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // In addition, in blocks in non-C++ modes, if all of the return 37641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // statements are enumerator-like expressions of some type T, where 37741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // T has a name for linkage, then we infer the return type of the 37841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // block to be that type. 37941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 3807dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // First case: no return statements, implicit void return type. 3817dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose ASTContext &Ctx = getASTContext(); 3827dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose if (CSI.Returns.empty()) { 3837dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // It's possible there were simply no /valid/ return statements. 3847dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // In this case, the first one we found may have at least given us a type. 3857dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose if (CSI.ReturnType.isNull()) 3867dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose CSI.ReturnType = Ctx.VoidTy; 3877dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose return; 3887dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose } 3897dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 3907dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // Second case: at least one return statement has dependent type. 3917dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // Delay type checking until instantiation. 3927dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose assert(!CSI.ReturnType.isNull() && "We should have a tentative return type."); 3937dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose if (CSI.ReturnType->isDependentType()) 3947dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose return; 3957dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 39641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Try to apply the enum-fuzz rule. 39741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (!getLangOpts().CPlusPlus) { 39841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall assert(isa<BlockScopeInfo>(CSI)); 39941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall const EnumDecl *ED = findCommonEnumForBlockReturns(CSI.Returns); 40041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (ED) { 40141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall CSI.ReturnType = Context.getTypeDeclType(ED); 40241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall adjustBlockReturnsToEnum(*this, CSI.Returns, CSI.ReturnType); 40341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall return; 40441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } 40541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall } 40641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 4077dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // Third case: only one return statement. Don't bother doing extra work! 4087dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose SmallVectorImpl<ReturnStmt*>::iterator I = CSI.Returns.begin(), 4097dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose E = CSI.Returns.end(); 4107dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose if (I+1 == E) 4117dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose return; 4127dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 4137dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // General case: many return statements. 4147dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // Check that they all have compatible return types. 4157dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 4167dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose // We require the return types to strictly match here. 41741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Note that we've already done the required promotions as part of 41841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // processing the return statement. 4197dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose for (; I != E; ++I) { 4207dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose const ReturnStmt *RS = *I; 4217dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose const Expr *RetE = RS->getRetValue(); 4227dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 42341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall QualType ReturnType = (RetE ? RetE->getType() : Context.VoidTy); 42441d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall if (Context.hasSameType(ReturnType, CSI.ReturnType)) 42541d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall continue; 42641d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall 42741d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // FIXME: This is a poor diagnostic for ReturnStmts without expressions. 42841d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // TODO: It's possible that the *first* return is the divergent one. 42941d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall Diag(RS->getLocStart(), 43041d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall diag::err_typecheck_missing_return_type_incompatible) 43141d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall << ReturnType << CSI.ReturnType 43241d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall << isa<LambdaScopeInfo>(CSI); 43341d016454b48f25c93d4b3fb84cfc7e426a9bd73John McCall // Continue iterating so that we keep emitting diagnostics. 4347dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose } 4357dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose} 4367dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 4370d8e9646bc000bab521ce52ed294209a92298cefRichard SmithFieldDecl *Sema::checkInitCapture(SourceLocation Loc, bool ByRef, 4380d8e9646bc000bab521ce52ed294209a92298cefRichard Smith IdentifierInfo *Id, Expr *InitExpr) { 4390d8e9646bc000bab521ce52ed294209a92298cefRichard Smith LambdaScopeInfo *LSI = getCurLambda(); 4400d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 4410d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // C++1y [expr.prim.lambda]p11: 4420d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // The type of [the] member corresponds to the type of a hypothetical 4430d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // variable declaration of the form "auto init-capture;" 4440d8e9646bc000bab521ce52ed294209a92298cefRichard Smith QualType DeductType = Context.getAutoDeductType(); 4450d8e9646bc000bab521ce52ed294209a92298cefRichard Smith TypeLocBuilder TLB; 4460d8e9646bc000bab521ce52ed294209a92298cefRichard Smith TLB.pushTypeSpec(DeductType).setNameLoc(Loc); 4470d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (ByRef) { 4480d8e9646bc000bab521ce52ed294209a92298cefRichard Smith DeductType = BuildReferenceType(DeductType, true, Loc, Id); 4490d8e9646bc000bab521ce52ed294209a92298cefRichard Smith assert(!DeductType.isNull() && "can't build reference to auto"); 4500d8e9646bc000bab521ce52ed294209a92298cefRichard Smith TLB.push<ReferenceTypeLoc>(DeductType).setSigilLoc(Loc); 4510d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } 45244ee0a710c59d8e6793189f903bae21c16814324Eli Friedman TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType); 4530d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 4540d8e9646bc000bab521ce52ed294209a92298cefRichard Smith InitializationKind InitKind = InitializationKind::CreateDefault(Loc); 4550d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Expr *Init = InitExpr; 4560d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (ParenListExpr *Parens = dyn_cast<ParenListExpr>(Init)) { 4570d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (Parens->getNumExprs() == 1) { 4580d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Init = Parens->getExpr(0); 4590d8e9646bc000bab521ce52ed294209a92298cefRichard Smith InitKind = InitializationKind::CreateDirect( 4600d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Loc, Parens->getLParenLoc(), Parens->getRParenLoc()); 4610d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } else { 4620d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // C++1y [dcl.spec.auto]p3: 4630d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // In an initializer of the form ( expression-list ), the 4640d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // expression-list shall be a single assignment-expression. 4650d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (Parens->getNumExprs() == 0) 4660d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Diag(Parens->getLocStart(), diag::err_init_capture_no_expression) 4670d8e9646bc000bab521ce52ed294209a92298cefRichard Smith << Id; 4680d8e9646bc000bab521ce52ed294209a92298cefRichard Smith else if (Parens->getNumExprs() > 1) 4690d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Diag(Parens->getExpr(1)->getLocStart(), 4700d8e9646bc000bab521ce52ed294209a92298cefRichard Smith diag::err_init_capture_multiple_expressions) 4710d8e9646bc000bab521ce52ed294209a92298cefRichard Smith << Id; 4720d8e9646bc000bab521ce52ed294209a92298cefRichard Smith return 0; 4730d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } 4740d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } else if (isa<InitListExpr>(Init)) 4750d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // We do not need to distinguish between direct-list-initialization 4760d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // and copy-list-initialization here, because we will always deduce 4770d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // std::initializer_list<T>, and direct- and copy-list-initialization 4780d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // always behave the same for such a type. 4790d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // FIXME: We should model whether an '=' was present. 4800d8e9646bc000bab521ce52ed294209a92298cefRichard Smith InitKind = InitializationKind::CreateDirectList(Loc); 4810d8e9646bc000bab521ce52ed294209a92298cefRichard Smith else 4820d8e9646bc000bab521ce52ed294209a92298cefRichard Smith InitKind = InitializationKind::CreateCopy(Loc, Loc); 4830d8e9646bc000bab521ce52ed294209a92298cefRichard Smith QualType DeducedType; 48444ee0a710c59d8e6793189f903bae21c16814324Eli Friedman if (DeduceAutoType(TSI, Init, DeducedType) == DAR_Failed) { 4850d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (isa<InitListExpr>(Init)) 4860d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Diag(Loc, diag::err_init_capture_deduction_failure_from_init_list) 4870d8e9646bc000bab521ce52ed294209a92298cefRichard Smith << Id << Init->getSourceRange(); 4880d8e9646bc000bab521ce52ed294209a92298cefRichard Smith else 4890d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Diag(Loc, diag::err_init_capture_deduction_failure) 4900d8e9646bc000bab521ce52ed294209a92298cefRichard Smith << Id << Init->getType() << Init->getSourceRange(); 4910d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } 4920d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (DeducedType.isNull()) 4930d8e9646bc000bab521ce52ed294209a92298cefRichard Smith return 0; 4940d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 4950d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // [...] a non-static data member named by the identifier is declared in 4960d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // the closure type. This member is not a bit-field and not mutable. 4970d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // Core issue: the member is (probably...) public. 4980d8e9646bc000bab521ce52ed294209a92298cefRichard Smith FieldDecl *NewFD = CheckFieldDecl( 49944ee0a710c59d8e6793189f903bae21c16814324Eli Friedman Id, DeducedType, TSI, LSI->Lambda, 5000d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Loc, /*Mutable*/ false, /*BitWidth*/ 0, ICIS_NoInit, 5010d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Loc, AS_public, /*PrevDecl*/ 0, /*Declarator*/ 0); 5020d8e9646bc000bab521ce52ed294209a92298cefRichard Smith LSI->Lambda->addDecl(NewFD); 5030d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 5040d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (CurContext->isDependentContext()) { 5050d8e9646bc000bab521ce52ed294209a92298cefRichard Smith LSI->addInitCapture(NewFD, InitExpr); 5060d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } else { 5070d8e9646bc000bab521ce52ed294209a92298cefRichard Smith InitializedEntity Entity = InitializedEntity::InitializeMember(NewFD); 5080d8e9646bc000bab521ce52ed294209a92298cefRichard Smith InitializationSequence InitSeq(*this, Entity, InitKind, Init); 5090d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (!InitSeq.Diagnose(*this, Entity, InitKind, Init)) { 5100d8e9646bc000bab521ce52ed294209a92298cefRichard Smith ExprResult InitResult = InitSeq.Perform(*this, Entity, InitKind, Init); 5110d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (!InitResult.isInvalid()) 5120d8e9646bc000bab521ce52ed294209a92298cefRichard Smith LSI->addInitCapture(NewFD, InitResult.take()); 5130d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } 5140d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } 5150d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 5160d8e9646bc000bab521ce52ed294209a92298cefRichard Smith return NewFD; 5170d8e9646bc000bab521ce52ed294209a92298cefRichard Smith} 5180d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 519dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregorvoid Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, 520dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor Declarator &ParamInfo, 521dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor Scope *CurScope) { 522f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor // Determine if we're within a context where we know that the lambda will 523f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor // be dependent, because there are template parameters in scope. 524f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor bool KnownDependent = false; 525f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor if (Scope *TmplScope = CurScope->getTemplateParamParent()) 526f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor if (!TmplScope->decl_empty()) 527f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor KnownDependent = true; 528f4b7de1cef3007cc0479775638198287384d9af1Douglas Gregor 529dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // Determine the signature of the call operator. 530e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor TypeSourceInfo *MethodTyInfo; 531e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor bool ExplicitParams = true; 532dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor bool ExplicitResultType = true; 533612409ece080e814f79e06772c690d603f45fbd6Richard Smith bool ContainsUnexpandedParameterPack = false; 534e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor SourceLocation EndLoc; 535cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<ParmVarDecl *, 8> Params; 536e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (ParamInfo.getNumTypeObjects() == 0) { 537e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // C++11 [expr.prim.lambda]p4: 538e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // If a lambda-expression does not include a lambda-declarator, it is as 539e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // if the lambda-declarator were (). 540e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor FunctionProtoType::ExtProtoInfo EPI; 541eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith EPI.HasTrailingReturn = true; 542e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor EPI.TypeQuals |= DeclSpec::TQ_const; 5435543169296beeb183b9c9392debc774fcf493eebDmitri Gribenko QualType MethodTy = Context.getFunctionType(Context.DependentTy, None, 544bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose EPI); 545e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy); 546e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor ExplicitParams = false; 547dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor ExplicitResultType = false; 548e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor EndLoc = Intro.Range.getEnd(); 549e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } else { 550e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor assert(ParamInfo.isFunctionDeclarator() && 551e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor "lambda-declarator is a function"); 552e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo(); 553e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 554e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // C++11 [expr.prim.lambda]p5: 555e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // This function call operator is declared const (9.3.1) if and only if 556e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // the lambda-expression's parameter-declaration-clause is not followed 557e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // by mutable. It is neither virtual nor declared volatile. [...] 558e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (!FTI.hasMutableQualifier()) 559e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor FTI.TypeQuals |= DeclSpec::TQ_const; 560e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 561e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope); 562e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor assert(MethodTyInfo && "no type from lambda-declarator"); 563e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor EndLoc = ParamInfo.getSourceRange().getEnd(); 564dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 565dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor ExplicitResultType 566dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor = MethodTyInfo->getType()->getAs<FunctionType>()->getResultType() 567dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor != Context.DependentTy; 5683bc22262af7b09a459b400976cfce3d9318b8ea9Richard Smith 5697c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && 5707c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()) { 5717c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman // Empty arg list, don't push any params. 5727c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman checkVoidParamDecl(cast<ParmVarDecl>(FTI.ArgInfo[0].Param)); 5737c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman } else { 5747c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman Params.reserve(FTI.NumArgs); 5757c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) 5767c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman Params.push_back(cast<ParmVarDecl>(FTI.ArgInfo[i].Param)); 5777c3c6bca2662704fbe038137d8ef2e4112359586Eli Friedman } 57803f1eb031b84e93415b792c4b45d8da71c88e92dDouglas Gregor 57903f1eb031b84e93415b792c4b45d8da71c88e92dDouglas Gregor // Check for unexpanded parameter packs in the method type. 580612409ece080e814f79e06772c690d603f45fbd6Richard Smith if (MethodTyInfo->getType()->containsUnexpandedParameterPack()) 581612409ece080e814f79e06772c690d603f45fbd6Richard Smith ContainsUnexpandedParameterPack = true; 582e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 5838da8a660128180a31479216111ff9b19b11c95b4Eli Friedman 5848da8a660128180a31479216111ff9b19b11c95b4Eli Friedman CXXRecordDecl *Class = createLambdaClosureType(Intro.Range, MethodTyInfo, 5858da8a660128180a31479216111ff9b19b11c95b4Eli Friedman KnownDependent); 5868da8a660128180a31479216111ff9b19b11c95b4Eli Friedman 58703f1eb031b84e93415b792c4b45d8da71c88e92dDouglas Gregor CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range, 588c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor MethodTyInfo, EndLoc, Params); 589c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor 590c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor if (ExplicitParams) 591c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor CheckCXXDefaultArguments(Method); 592dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor 593ad017fa7a4df7389d245d02a49b3c79ed70bedb9Bill Wendling // Attributes on the lambda apply to the method. 594e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor ProcessDeclAttributes(CurScope, Method, ParamInfo); 595e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 596503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor // Introduce the function call operator as the current declaration context. 597e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor PushDeclContext(CurScope, Method); 598e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 599e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Introduce the lambda scope. 600dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor LambdaScopeInfo *LSI 601dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor = enterLambdaScope(Method, Intro.Range, Intro.Default, ExplicitParams, 602dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor ExplicitResultType, 6034ef832ffc1147ce2f9777f9fad650cb3139a1d00David Blaikie !Method->isConst()); 6040d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 6050d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // Distinct capture names, for diagnostics. 6060d8e9646bc000bab521ce52ed294209a92298cefRichard Smith llvm::SmallSet<IdentifierInfo*, 8> CaptureNames; 6070d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 608e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Handle explicit captures. 6093ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor SourceLocation PrevCaptureLoc 6103ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor = Intro.Default == LCD_None? Intro.Range.getBegin() : Intro.DefaultLoc; 61109d19efaa147762f84aed55efa7930bb3616a4e5Craig Topper for (SmallVectorImpl<LambdaCapture>::const_iterator 61209d19efaa147762f84aed55efa7930bb3616a4e5Craig Topper C = Intro.Captures.begin(), 61309d19efaa147762f84aed55efa7930bb3616a4e5Craig Topper E = Intro.Captures.end(); 61409d19efaa147762f84aed55efa7930bb3616a4e5Craig Topper C != E; 6153ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor PrevCaptureLoc = C->Loc, ++C) { 616e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (C->Kind == LCK_This) { 617e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // C++11 [expr.prim.lambda]p8: 618e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // An identifier or this shall not appear more than once in a 619e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // lambda-capture. 620e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (LSI->isCXXThisCaptured()) { 621e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Diag(C->Loc, diag::err_capture_more_than_once) 622e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor << "'this'" 6233ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor << SourceRange(LSI->getCXXThisCapture().getLocation()) 6243ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor << FixItHint::CreateRemoval( 6253ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 626e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 627e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 628e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 629e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // C++11 [expr.prim.lambda]p8: 630e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // If a lambda-capture includes a capture-default that is =, the 631e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // lambda-capture shall not contain this [...]. 632e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (Intro.Default == LCD_ByCopy) { 6333ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor Diag(C->Loc, diag::err_this_capture_with_copy_default) 6343ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor << FixItHint::CreateRemoval( 6353ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 636e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 637e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 638e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 639e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // C++11 [expr.prim.lambda]p12: 640e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // If this is captured by a local lambda expression, its nearest 641e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // enclosing function shall be a non-static member function. 642e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor QualType ThisCaptureType = getCurrentThisType(); 643e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (ThisCaptureType.isNull()) { 644e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Diag(C->Loc, diag::err_this_capture) << true; 645e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 646e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 647e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 648e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor CheckCXXThisCapture(C->Loc, /*Explicit=*/true); 649e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 650e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 651e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 6520d8e9646bc000bab521ce52ed294209a92298cefRichard Smith assert(C->Id && "missing identifier for capture"); 6530d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 6540a664b863255065d960342dd074a77d63c753d35Richard Smith if (C->Init.isInvalid()) 6550a664b863255065d960342dd074a77d63c753d35Richard Smith continue; 6560a664b863255065d960342dd074a77d63c753d35Richard Smith if (C->Init.isUsable()) { 6570d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // C++11 [expr.prim.lambda]p8: 6580d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // An identifier or this shall not appear more than once in a 6590d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // lambda-capture. 6600d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (!CaptureNames.insert(C->Id)) 6610d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Diag(C->Loc, diag::err_capture_more_than_once) << C->Id; 6620d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 6630d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (C->Init.get()->containsUnexpandedParameterPack()) 6640d8e9646bc000bab521ce52ed294209a92298cefRichard Smith ContainsUnexpandedParameterPack = true; 6650d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 6660d8e9646bc000bab521ce52ed294209a92298cefRichard Smith FieldDecl *NewFD = checkInitCapture(C->Loc, C->Kind == LCK_ByRef, 6670d8e9646bc000bab521ce52ed294209a92298cefRichard Smith C->Id, C->Init.take()); 6680d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // C++1y [expr.prim.lambda]p11: 6690d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // Within the lambda-expression's lambda-declarator and 6700d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // compound-statement, the identifier in the init-capture 6710d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // hides any declaration of the same name in scopes enclosing 6720d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // the lambda-expression. 6730d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (NewFD) 6740d8e9646bc000bab521ce52ed294209a92298cefRichard Smith PushOnScopeChains(NewFD, CurScope, false); 6750a664b863255065d960342dd074a77d63c753d35Richard Smith continue; 6760a664b863255065d960342dd074a77d63c753d35Richard Smith } 6770a664b863255065d960342dd074a77d63c753d35Richard Smith 678e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // C++11 [expr.prim.lambda]p8: 679e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // If a lambda-capture includes a capture-default that is &, the 680e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // identifiers in the lambda-capture shall not be preceded by &. 681e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // If a lambda-capture includes a capture-default that is =, [...] 682e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // each identifier it contains shall be preceded by &. 683e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) { 6843ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor Diag(C->Loc, diag::err_reference_capture_with_reference_default) 6853ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor << FixItHint::CreateRemoval( 6863ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 687e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 688e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) { 6893ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor Diag(C->Loc, diag::err_copy_capture_with_copy_default) 6903ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor << FixItHint::CreateRemoval( 6913ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 692e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 693e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 694e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 6950d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // C++11 [expr.prim.lambda]p10: 6960d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // The identifiers in a capture-list are looked up using the usual 6970d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // rules for unqualified name lookup (3.4.1) 698e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor DeclarationNameInfo Name(C->Id, C->Loc); 699e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor LookupResult R(*this, Name, LookupOrdinaryName); 700e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor LookupName(R, CurScope); 701e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (R.isAmbiguous()) 702e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 703e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (R.empty()) { 704e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // FIXME: Disable corrections that would add qualification? 705e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor CXXScopeSpec ScopeSpec; 706e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor DeclFilterCCC<VarDecl> Validator; 707e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator)) 708e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 709e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 710e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 7110d8e9646bc000bab521ce52ed294209a92298cefRichard Smith VarDecl *Var = R.getAsSingle<VarDecl>(); 7120d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 7130d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // C++11 [expr.prim.lambda]p8: 7140d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // An identifier or this shall not appear more than once in a 7150d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // lambda-capture. 7160d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (!CaptureNames.insert(C->Id)) { 7170d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (Var && LSI->isCaptured(Var)) { 7180d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Diag(C->Loc, diag::err_capture_more_than_once) 7190d8e9646bc000bab521ce52ed294209a92298cefRichard Smith << C->Id << SourceRange(LSI->getCapture(Var).getLocation()) 7200d8e9646bc000bab521ce52ed294209a92298cefRichard Smith << FixItHint::CreateRemoval( 7210d8e9646bc000bab521ce52ed294209a92298cefRichard Smith SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); 7220d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } else 7230d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // Previous capture was an init-capture: no fixit. 7240d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Diag(C->Loc, diag::err_capture_more_than_once) << C->Id; 7250d8e9646bc000bab521ce52ed294209a92298cefRichard Smith continue; 7260d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } 7270d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 728e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // C++11 [expr.prim.lambda]p10: 7290d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // [...] each such lookup shall find a variable with automatic storage 7300d8e9646bc000bab521ce52ed294209a92298cefRichard Smith // duration declared in the reaching scope of the local lambda expression. 731999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor // Note that the 'reaching scope' check happens in tryCaptureVariable(). 732e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (!Var) { 733e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id; 734e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 735e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 736e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 7379cd5b24315aea4bc58bac03cfb4874e076b013b8Eli Friedman // Ignore invalid decls; they'll just confuse the code later. 7389cd5b24315aea4bc58bac03cfb4874e076b013b8Eli Friedman if (Var->isInvalidDecl()) 7399cd5b24315aea4bc58bac03cfb4874e076b013b8Eli Friedman continue; 7409cd5b24315aea4bc58bac03cfb4874e076b013b8Eli Friedman 741e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (!Var->hasLocalStorage()) { 742e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id; 743e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Diag(Var->getLocation(), diag::note_previous_decl) << C->Id; 744e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 745e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 746e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 747a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor // C++11 [expr.prim.lambda]p23: 748a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor // A capture followed by an ellipsis is a pack expansion (14.5.3). 749a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor SourceLocation EllipsisLoc; 750a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor if (C->EllipsisLoc.isValid()) { 751a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor if (Var->isParameterPack()) { 752a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor EllipsisLoc = C->EllipsisLoc; 753a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor } else { 754a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) 755a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor << SourceRange(C->Loc); 756a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor 757a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor // Just ignore the ellipsis. 758a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor } 759a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor } else if (Var->isParameterPack()) { 760612409ece080e814f79e06772c690d603f45fbd6Richard Smith ContainsUnexpandedParameterPack = true; 761a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor } 762a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor 763e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef : 764e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor TryCapture_ExplicitByVal; 765999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc); 766e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 767dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor finishLambdaExplicitCaptures(LSI); 768e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 769612409ece080e814f79e06772c690d603f45fbd6Richard Smith LSI->ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; 770612409ece080e814f79e06772c690d603f45fbd6Richard Smith 771c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor // Add lambda parameters into scope. 772c6889e7ed16604c51994e1f11becf213fdc64eb3Douglas Gregor addLambdaParameters(Method, CurScope); 773e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 774dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor // Enter a new evaluation context to insulate the lambda from any 775503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor // cleanups from the enclosing full-expression. 776503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor PushExpressionEvaluationContext(PotentiallyEvaluated); 777e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor} 778e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 779dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregorvoid Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope, 780dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor bool IsInstantiation) { 781e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Leave the expression-evaluation context. 782e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor DiscardCleanupsInEvaluationContext(); 783e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor PopExpressionEvaluationContext(); 784e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 785e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Leave the context of the lambda. 786dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor if (!IsInstantiation) 787dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor PopDeclContext(); 788630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor 789630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor // Finalize the lambda. 790630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor LambdaScopeInfo *LSI = getCurLambda(); 791630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor CXXRecordDecl *Class = LSI->Lambda; 792630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor Class->setInvalidDecl(); 793262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie SmallVector<Decl*, 4> Fields; 794262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie for (RecordDecl::field_iterator i = Class->field_begin(), 795262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie e = Class->field_end(); i != e; ++i) 796581deb3da481053c4993c7600f97acf7768caac5David Blaikie Fields.push_back(*i); 797630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor ActOnFields(0, Class->getLocation(), Class, Fields, 798630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor SourceLocation(), SourceLocation(), 0); 799630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor CheckCompletedCXXClass(Class); 800630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor 801e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor PopFunctionScopeInfo(); 802e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor} 803e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 804c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor/// \brief Add a lambda's conversion to function pointer, as described in 805c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor/// C++11 [expr.prim.lambda]p6. 806c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregorstatic void addFunctionPointerConversion(Sema &S, 807c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor SourceRange IntroducerRange, 808c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor CXXRecordDecl *Class, 809c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor CXXMethodDecl *CallOperator) { 81027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor // Add the conversion to function pointer. 811c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor const FunctionProtoType *Proto 812c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor = CallOperator->getType()->getAs<FunctionProtoType>(); 813c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor QualType FunctionPtrTy; 81427dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor QualType FunctionTy; 815c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor { 816c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); 817c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor ExtInfo.TypeQuals = 0; 8180567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner FunctionTy = S.Context.getFunctionType(Proto->getResultType(), 8190567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner Proto->getArgTypes(), ExtInfo); 820c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor FunctionPtrTy = S.Context.getPointerType(FunctionTy); 821c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor } 822c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor 823c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor FunctionProtoType::ExtProtoInfo ExtInfo; 824c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor ExtInfo.TypeQuals = Qualifiers::Const; 825bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose QualType ConvTy = 8265543169296beeb183b9c9392debc774fcf493eebDmitri Gribenko S.Context.getFunctionType(FunctionPtrTy, None, ExtInfo); 827c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor 828c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor SourceLocation Loc = IntroducerRange.getBegin(); 829c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor DeclarationName Name 830c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor = S.Context.DeclarationNames.getCXXConversionFunctionName( 831c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor S.Context.getCanonicalType(FunctionPtrTy)); 832c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor DeclarationNameLoc NameLoc; 833c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(FunctionPtrTy, 834c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor Loc); 835c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor CXXConversionDecl *Conversion 836c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor = CXXConversionDecl::Create(S.Context, Class, Loc, 837c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor DeclarationNameInfo(Name, Loc, NameLoc), 838c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor ConvTy, 839c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor S.Context.getTrivialTypeSourceInfo(ConvTy, 840c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor Loc), 84138fa961573efb0fb9ef53f3e8a9e730701bc9375Eli Friedman /*isInline=*/true, /*isExplicit=*/false, 842c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor /*isConstexpr=*/false, 843c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor CallOperator->getBody()->getLocEnd()); 844c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor Conversion->setAccess(AS_public); 845c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor Conversion->setImplicit(true); 846c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor Class->addDecl(Conversion); 84727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor 84827dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor // Add a non-static member function "__invoke" that will be the result of 84927dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor // the conversion. 85027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor Name = &S.Context.Idents.get("__invoke"); 85127dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor CXXMethodDecl *Invoke 85227dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor = CXXMethodDecl::Create(S.Context, Class, Loc, 85327dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor DeclarationNameInfo(Name, Loc), FunctionTy, 85427dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor CallOperator->getTypeSourceInfo(), 855d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola SC_Static, /*IsInline=*/true, 85627dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor /*IsConstexpr=*/false, 85727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor CallOperator->getBody()->getLocEnd()); 85827dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor SmallVector<ParmVarDecl *, 4> InvokeParams; 85927dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { 86027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor ParmVarDecl *From = CallOperator->getParamDecl(I); 86127dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor InvokeParams.push_back(ParmVarDecl::Create(S.Context, Invoke, 86227dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor From->getLocStart(), 86327dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor From->getLocation(), 86427dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor From->getIdentifier(), 86527dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor From->getType(), 86627dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor From->getTypeSourceInfo(), 86727dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor From->getStorageClass(), 86827dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor /*DefaultArg=*/0)); 86927dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor } 87027dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor Invoke->setParams(InvokeParams); 87127dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor Invoke->setAccess(AS_private); 87227dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor Invoke->setImplicit(true); 87327dd7d962bbf774988bc5e59d04a7743ed503514Douglas Gregor Class->addDecl(Invoke); 874c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor} 875c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor 876c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor/// \brief Add a lambda's conversion to block pointer. 877c2956e5681113bbcec5ff98833345166942a211bDouglas Gregorstatic void addBlockPointerConversion(Sema &S, 878c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor SourceRange IntroducerRange, 879c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor CXXRecordDecl *Class, 880c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor CXXMethodDecl *CallOperator) { 881c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor const FunctionProtoType *Proto 882c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor = CallOperator->getType()->getAs<FunctionProtoType>(); 883c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor QualType BlockPtrTy; 884c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor { 885c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); 886c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor ExtInfo.TypeQuals = 0; 8870567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner QualType FunctionTy = S.Context.getFunctionType( 8880567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner Proto->getResultType(), Proto->getArgTypes(), ExtInfo); 889c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor BlockPtrTy = S.Context.getBlockPointerType(FunctionTy); 890c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor } 891c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor 892c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor FunctionProtoType::ExtProtoInfo ExtInfo; 893c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor ExtInfo.TypeQuals = Qualifiers::Const; 8945543169296beeb183b9c9392debc774fcf493eebDmitri Gribenko QualType ConvTy = S.Context.getFunctionType(BlockPtrTy, None, ExtInfo); 895c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor 896c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor SourceLocation Loc = IntroducerRange.getBegin(); 897c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor DeclarationName Name 898c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor = S.Context.DeclarationNames.getCXXConversionFunctionName( 899c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor S.Context.getCanonicalType(BlockPtrTy)); 900c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor DeclarationNameLoc NameLoc; 901c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc); 902c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor CXXConversionDecl *Conversion 903c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor = CXXConversionDecl::Create(S.Context, Class, Loc, 904c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor DeclarationNameInfo(Name, Loc, NameLoc), 905c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor ConvTy, 906c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor S.Context.getTrivialTypeSourceInfo(ConvTy, Loc), 90795099ef43aff8fb3d5d47bed0a583e7977f4b3f4Eli Friedman /*isInline=*/true, /*isExplicit=*/false, 908c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor /*isConstexpr=*/false, 909c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor CallOperator->getBody()->getLocEnd()); 910c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor Conversion->setAccess(AS_public); 911c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor Conversion->setImplicit(true); 912c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor Class->addDecl(Conversion); 913c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor} 9145878cbcfaa90b8515550db86033fd5a0efab971dDouglas Gregor 915dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas GregorExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, 9169e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor Scope *CurScope, 9179e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor bool IsInstantiation) { 918e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Collect information from the lambda scope. 919cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<LambdaExpr::Capture, 4> Captures; 920cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<Expr *, 4> CaptureInits; 921e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor LambdaCaptureDefault CaptureDefault; 922e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor CXXRecordDecl *Class; 923ef7d78bd5be466c369b04af742ed8268244d4fe7Douglas Gregor CXXMethodDecl *CallOperator; 924e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor SourceRange IntroducerRange; 925e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor bool ExplicitParams; 926dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor bool ExplicitResultType; 927503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor bool LambdaExprNeedsCleanups; 928612409ece080e814f79e06772c690d603f45fbd6Richard Smith bool ContainsUnexpandedParameterPack; 929cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<VarDecl *, 4> ArrayIndexVars; 930cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko SmallVector<unsigned, 4> ArrayIndexStarts; 931e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor { 932e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor LambdaScopeInfo *LSI = getCurLambda(); 933ef7d78bd5be466c369b04af742ed8268244d4fe7Douglas Gregor CallOperator = LSI->CallOperator; 934e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Class = LSI->Lambda; 935e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor IntroducerRange = LSI->IntroducerRange; 936e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor ExplicitParams = LSI->ExplicitParams; 937dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor ExplicitResultType = !LSI->HasImplicitReturnType; 938503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups; 939612409ece080e814f79e06772c690d603f45fbd6Richard Smith ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack; 9409daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor ArrayIndexVars.swap(LSI->ArrayIndexVars); 9419daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor ArrayIndexStarts.swap(LSI->ArrayIndexStarts); 9429daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor 943e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Translate captures. 944e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) { 945e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor LambdaScopeInfo::Capture From = LSI->Captures[I]; 946e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor assert(!From.isBlockCapture() && "Cannot capture __block variables"); 947e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor bool IsImplicit = I >= LSI->NumExplicitCaptures; 948e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 949e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor // Handle 'this' capture. 950e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor if (From.isThisCapture()) { 951e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Captures.push_back(LambdaExpr::Capture(From.getLocation(), 952e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor IsImplicit, 953e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor LCK_This)); 954e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(), 955e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor getCurrentThisType(), 956e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor /*isImplicit=*/true)); 957e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor continue; 958e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 959e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 9600d8e9646bc000bab521ce52ed294209a92298cefRichard Smith if (From.isInitCapture()) { 9610d8e9646bc000bab521ce52ed294209a92298cefRichard Smith Captures.push_back(LambdaExpr::Capture(From.getInitCaptureField())); 9620d8e9646bc000bab521ce52ed294209a92298cefRichard Smith CaptureInits.push_back(From.getInitExpr()); 9630d8e9646bc000bab521ce52ed294209a92298cefRichard Smith continue; 9640d8e9646bc000bab521ce52ed294209a92298cefRichard Smith } 9650d8e9646bc000bab521ce52ed294209a92298cefRichard Smith 966e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor VarDecl *Var = From.getVariable(); 967e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef; 968e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit, 969a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor Kind, Var, From.getEllipsisLoc())); 9700d8e9646bc000bab521ce52ed294209a92298cefRichard Smith CaptureInits.push_back(From.getInitExpr()); 971e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 972e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 973e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor switch (LSI->ImpCaptureStyle) { 974e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor case CapturingScopeInfo::ImpCap_None: 975e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor CaptureDefault = LCD_None; 976e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor break; 977e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 978e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor case CapturingScopeInfo::ImpCap_LambdaByval: 979e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor CaptureDefault = LCD_ByCopy; 980e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor break; 981e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 9826afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj case CapturingScopeInfo::ImpCap_CapturedRegion: 983e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor case CapturingScopeInfo::ImpCap_LambdaByref: 984e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor CaptureDefault = LCD_ByRef; 985e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor break; 986e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 987e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor case CapturingScopeInfo::ImpCap_Block: 988e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor llvm_unreachable("block capture in lambda"); 989e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor break; 990e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 991e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 99254042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // C++11 [expr.prim.lambda]p4: 99354042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // If a lambda-expression does not include a 99454042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // trailing-return-type, it is as if the trailing-return-type 99554042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // denotes the following type: 99654042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // FIXME: Assumes current resolution to core issue 975. 99754042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor if (LSI->HasImplicitReturnType) { 9987dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose deduceClosureReturnType(*LSI); 9997dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose 100054042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // - if there are no return statements in the 100154042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // compound-statement, or all return statements return 100254042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // either an expression of type void or no expression or 100354042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // braced-init-list, the type void; 100454042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor if (LSI->ReturnType.isNull()) { 100554042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor LSI->ReturnType = Context.VoidTy; 100654042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor } 100754042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor 100854042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor // Create a function type with the inferred return type. 100954042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor const FunctionProtoType *Proto 101054042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor = CallOperator->getType()->getAs<FunctionProtoType>(); 10110567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner QualType FunctionTy = Context.getFunctionType( 10120567a79130a251bf464ce21ecf3f8b9fb5207900Reid Kleckner LSI->ReturnType, Proto->getArgTypes(), Proto->getExtProtoInfo()); 101354042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor CallOperator->setType(FunctionTy); 101454042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor } 101554042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor 1016215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor // C++ [expr.prim.lambda]p7: 1017215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor // The lambda-expression's compound-statement yields the 1018215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor // function-body (8.4) of the function call operator [...]. 1019dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor ActOnFinishFunctionBody(CallOperator, Body, IsInstantiation); 1020215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor CallOperator->setLexicalDeclContext(Class); 1021215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor Class->addDecl(CallOperator); 1022b09ab8c293833c3dbcbf78f0db5e01fec46966bfDouglas Gregor PopExpressionEvaluationContext(); 1023215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor 1024b555971345750350c21d541afe135054c7402933Douglas Gregor // C++11 [expr.prim.lambda]p6: 1025b555971345750350c21d541afe135054c7402933Douglas Gregor // The closure type for a lambda-expression with no lambda-capture 1026b555971345750350c21d541afe135054c7402933Douglas Gregor // has a public non-virtual non-explicit const conversion function 1027b555971345750350c21d541afe135054c7402933Douglas Gregor // to pointer to function having the same parameter and return 1028b555971345750350c21d541afe135054c7402933Douglas Gregor // types as the closure type's function call operator. 1029c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor if (Captures.empty() && CaptureDefault == LCD_None) 1030c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor addFunctionPointerConversion(*this, IntroducerRange, Class, 1031c25d1c9821e576ae6e3d11f621ff0901aa4e7c69Douglas Gregor CallOperator); 1032503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor 1033c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor // Objective-C++: 1034c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor // The closure type for a lambda-expression has a public non-virtual 1035c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor // non-explicit const conversion function to a block pointer having the 1036c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor // same parameter and return types as the closure type's function call 1037c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor // operator. 10384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie if (getLangOpts().Blocks && getLangOpts().ObjC1) 1039c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator); 1040c2956e5681113bbcec5ff98833345166942a211bDouglas Gregor 1041b555971345750350c21d541afe135054c7402933Douglas Gregor // Finalize the lambda class. 1042262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie SmallVector<Decl*, 4> Fields; 1043262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie for (RecordDecl::field_iterator i = Class->field_begin(), 1044262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie e = Class->field_end(); i != e; ++i) 1045581deb3da481053c4993c7600f97acf7768caac5David Blaikie Fields.push_back(*i); 1046b555971345750350c21d541afe135054c7402933Douglas Gregor ActOnFields(0, Class->getLocation(), Class, Fields, 1047b555971345750350c21d541afe135054c7402933Douglas Gregor SourceLocation(), SourceLocation(), 0); 1048b555971345750350c21d541afe135054c7402933Douglas Gregor CheckCompletedCXXClass(Class); 1049e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor } 1050e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor 1051503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor if (LambdaExprNeedsCleanups) 1052503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor ExprNeedsCleanups = true; 10539e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor 1054e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, 1055e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor CaptureDefault, Captures, 1056dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor ExplicitParams, ExplicitResultType, 1057dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor CaptureInits, ArrayIndexVars, 1058612409ece080e814f79e06772c690d603f45fbd6Richard Smith ArrayIndexStarts, Body->getLocEnd(), 1059612409ece080e814f79e06772c690d603f45fbd6Richard Smith ContainsUnexpandedParameterPack); 1060e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor 1061e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor // C++11 [expr.prim.lambda]p2: 1062e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor // A lambda-expression shall not appear in an unevaluated operand 1063e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor // (Clause 5). 1064d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor if (!CurContext->isDependentContext()) { 1065d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor switch (ExprEvalContexts.back().Context) { 1066d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor case Unevaluated: 1067aeeacf725c9e0ddd64ea9764bd008e5b6873ce51John McCall case UnevaluatedAbstract: 1068d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor // We don't actually diagnose this case immediately, because we 1069d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor // could be within a context where we might find out later that 1070d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor // the expression is potentially evaluated (e.g., for typeid). 1071d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor ExprEvalContexts.back().Lambdas.push_back(Lambda); 1072d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor break; 1073e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor 1074d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor case ConstantEvaluated: 1075d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor case PotentiallyEvaluated: 1076d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor case PotentiallyEvaluatedIfUsed: 1077d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor break; 1078d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor } 1079d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor } 1080d5387e86ce3dfe1ae09e050ee11d86ca0d066d04Douglas Gregor 1081503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor return MaybeBindToTemporary(Lambda); 1082e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor} 108323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 108423f0267e2d56c0407f12e62df3561ecf75d74e6eEli FriedmanExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation, 108523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman SourceLocation ConvLocation, 108623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman CXXConversionDecl *Conv, 108723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Expr *Src) { 108823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // Make sure that the lambda call operator is marked used. 108923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman CXXRecordDecl *Lambda = Conv->getParent(); 109023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman CXXMethodDecl *CallOperator 109123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman = cast<CXXMethodDecl>( 10923bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie Lambda->lookup( 10933bc93e3124ad5e7191c4a12dc981c8ee53578193David Blaikie Context.DeclarationNames.getCXXOperatorName(OO_Call)).front()); 109423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman CallOperator->setReferenced(); 109523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman CallOperator->setUsed(); 109623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 109723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman ExprResult Init = PerformCopyInitialization( 109823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman InitializedEntity::InitializeBlock(ConvLocation, 109923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Src->getType(), 110023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman /*NRVO=*/false), 110123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman CurrentLocation, Src); 110223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman if (!Init.isInvalid()) 110323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Init = ActOnFinishFullExpr(Init.take()); 110423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 110523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman if (Init.isInvalid()) 110623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman return ExprError(); 110723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 110823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // Create the new block to be returned. 110923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman BlockDecl *Block = BlockDecl::Create(Context, CurContext, ConvLocation); 111023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 111123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // Set the type information. 111223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo()); 111323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Block->setIsVariadic(CallOperator->isVariadic()); 111423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Block->setBlockMissingReturnType(false); 111523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 111623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // Add parameters. 111723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman SmallVector<ParmVarDecl *, 4> BlockParams; 111823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { 111923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman ParmVarDecl *From = CallOperator->getParamDecl(I); 112023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman BlockParams.push_back(ParmVarDecl::Create(Context, Block, 112123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman From->getLocStart(), 112223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman From->getLocation(), 112323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman From->getIdentifier(), 112423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman From->getType(), 112523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman From->getTypeSourceInfo(), 112623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman From->getStorageClass(), 112723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman /*DefaultArg=*/0)); 112823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman } 112923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Block->setParams(BlockParams); 113023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 113123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Block->setIsConversionFromLambda(true); 113223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 113323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // Add capture. The capture uses a fake variable, which doesn't correspond 113423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // to any actual memory location. However, the initializer copy-initializes 113523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // the lambda object. 113623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman TypeSourceInfo *CapVarTSI = 113723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Context.getTrivialTypeSourceInfo(Src->getType()); 113823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation, 113923f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman ConvLocation, 0, 114023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Src->getType(), CapVarTSI, 1141d2615cc53b916e8aae45783ca7113b93de515ce3Rafael Espindola SC_None); 114223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman BlockDecl::Capture Capture(/*Variable=*/CapVar, /*ByRef=*/false, 114323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman /*Nested=*/false, /*Copy=*/Init.take()); 114423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Block->setCaptures(Context, &Capture, &Capture + 1, 114523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman /*CapturesCXXThis=*/false); 114623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 114723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // Add a fake function body to the block. IR generation is responsible 114823f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // for filling in the actual body, which cannot be expressed as an AST. 11493a2d0fb726aca3096b5c1ea9be734417060f34d7Benjamin Kramer Block->setBody(new (Context) CompoundStmt(ConvLocation)); 115023f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 115123f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman // Create the block literal expression. 115223f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType()); 115323f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman ExprCleanupObjects.push_back(Block); 115423f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman ExprNeedsCleanups = true; 115523f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman 115623f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman return BuildBlock; 115723f0267e2d56c0407f12e62df3561ecf75d74e6eEli Friedman} 1158