SemaLambda.cpp revision dfca6f53ab97d28d43e3fa2564209df08f3d282c
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"
14e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/Initialization.h"
15e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/Lookup.h"
16e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/ScopeInfo.h"
17e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/Sema/SemaInternal.h"
183ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor#include "clang/Lex/Preprocessor.h"
19e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor#include "clang/AST/ExprCXX.h"
20e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregorusing namespace clang;
21e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregorusing namespace sema;
22e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
23dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas GregorCXXRecordDecl *Sema::createLambdaClosureType(SourceRange IntroducerRange) {
24e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  DeclContext *DC = CurContext;
25e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
26e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    DC = DC->getParent();
27dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
28e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Start constructing the lambda class.
29da8962a6198bc4bf09a38209db99551b2b0a41a0Douglas Gregor  CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(Context, DC,
30dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                                     IntroducerRange.getBegin());
31e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  CurContext->addDecl(Class);
32dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
33dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  return Class;
34dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor}
35e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
36dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas GregorCXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class,
37dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                           SourceRange IntroducerRange,
38dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                           TypeSourceInfo *MethodType,
39dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                           SourceLocation EndLoc) {
40dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  // C++11 [expr.prim.lambda]p5:
41dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  //   The closure type for a lambda-expression has a public inline function
42dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  //   call operator (13.5.4) whose parameters and return type are described by
43dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  //   the lambda-expression's parameter-declaration-clause and
44dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  //   trailing-return-type respectively.
45dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  DeclarationName MethodName
46dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    = Context.DeclarationNames.getCXXOperatorName(OO_Call);
47dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  DeclarationNameLoc MethodNameLoc;
48dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  MethodNameLoc.CXXOperatorName.BeginOpNameLoc
49dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    = IntroducerRange.getBegin().getRawEncoding();
50dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  MethodNameLoc.CXXOperatorName.EndOpNameLoc
51dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    = IntroducerRange.getEnd().getRawEncoding();
52dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  CXXMethodDecl *Method
53dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    = CXXMethodDecl::Create(Context, Class, EndLoc,
54dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            DeclarationNameInfo(MethodName,
55dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                                IntroducerRange.getBegin(),
56dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                                MethodNameLoc),
57dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            MethodType->getType(), MethodType,
58dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            /*isStatic=*/false,
59dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            SC_None,
60dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            /*isInline=*/true,
61dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            /*isConstExpr=*/false,
62dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            EndLoc);
63dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  Method->setAccess(AS_public);
64dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
65dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  // Temporarily set the lexical declaration context to the current
66dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  // context, so that the Scope stack matches the lexical nesting.
67dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  Method->setLexicalDeclContext(Class->getDeclContext());
68dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
69dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  return Method;
70dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor}
71dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
72dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas GregorLambdaScopeInfo *Sema::enterLambdaScope(CXXMethodDecl *CallOperator,
73dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                        SourceRange IntroducerRange,
74dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                        LambdaCaptureDefault CaptureDefault,
75dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                        bool ExplicitParams,
76dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                        bool ExplicitResultType,
77dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                        bool Mutable) {
78dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  PushLambdaScope(CallOperator->getParent(), CallOperator);
79dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  LambdaScopeInfo *LSI = getCurLambda();
80dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  if (CaptureDefault == LCD_ByCopy)
81dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval;
82dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  else if (CaptureDefault == LCD_ByRef)
83dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref;
84dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  LSI->IntroducerRange = IntroducerRange;
85dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  LSI->ExplicitParams = ExplicitParams;
86dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  LSI->Mutable = Mutable;
87dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
88dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  if (ExplicitResultType) {
89dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    LSI->ReturnType = CallOperator->getResultType();
90dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  } else {
91dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    LSI->HasImplicitReturnType = true;
92dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  }
93dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
94dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  return LSI;
95dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor}
96dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
97dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregorvoid Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) {
98dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  LSI->finishedExplicitCaptures();
99dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor}
100dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
101dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregorvoid Sema::addLambdaParameters(CXXMethodDecl *CallOperator, Scope *CurScope,
102dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                               llvm::ArrayRef<ParmVarDecl *> Params) {
103dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  CallOperator->setParams(Params);
104dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  CheckParmsForFunctionDef(const_cast<ParmVarDecl **>(Params.begin()),
105dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                           const_cast<ParmVarDecl **>(Params.end()),
106dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                           /*CheckParameterNames=*/false);
107dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
108dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  // Introduce our parameters into the function scope
109dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  for (unsigned p = 0, NumParams = CallOperator->getNumParams();
110dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor       p < NumParams; ++p) {
111dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    ParmVarDecl *Param = CallOperator->getParamDecl(p);
112dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    Param->setOwningFunction(CallOperator);
113dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
114dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    // If this has an identifier, add it to the scope stack.
115dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    if (CurScope && Param->getIdentifier()) {
116dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor      CheckShadow(CurScope, Param);
117dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
118dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor      PushOnScopeChains(Param, CurScope);
119dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    }
120dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  }
121dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor}
122dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
123dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregorvoid Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
124dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                        Declarator &ParamInfo,
125dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                        Scope *CurScope) {
126dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  CXXRecordDecl *Class = createLambdaClosureType(Intro.Range);
127dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
128dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  // Determine the signature of the call operator.
129e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  TypeSourceInfo *MethodTyInfo;
130e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  bool ExplicitParams = true;
131dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  bool ExplicitResultType = true;
132e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  SourceLocation EndLoc;
133e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  if (ParamInfo.getNumTypeObjects() == 0) {
134e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // C++11 [expr.prim.lambda]p4:
135e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   If a lambda-expression does not include a lambda-declarator, it is as
136e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   if the lambda-declarator were ().
137e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    FunctionProtoType::ExtProtoInfo EPI;
138eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    EPI.HasTrailingReturn = true;
139e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    EPI.TypeQuals |= DeclSpec::TQ_const;
140dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    QualType MethodTy = Context.getFunctionType(Context.DependentTy,
141dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                                /*Args=*/0, /*NumArgs=*/0, EPI);
142e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy);
143e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    ExplicitParams = false;
144dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    ExplicitResultType = false;
145e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    EndLoc = Intro.Range.getEnd();
146e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  } else {
147e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    assert(ParamInfo.isFunctionDeclarator() &&
148e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor           "lambda-declarator is a function");
149e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo();
150e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
151e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // C++11 [expr.prim.lambda]p5:
152e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   This function call operator is declared const (9.3.1) if and only if
153e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   the lambda-expression's parameter-declaration-clause is not followed
154e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   by mutable. It is neither virtual nor declared volatile. [...]
155e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (!FTI.hasMutableQualifier())
156e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      FTI.TypeQuals |= DeclSpec::TQ_const;
157e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
158e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // C++11 [expr.prim.lambda]p5:
159e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   [...] Default arguments (8.3.6) shall not be specified in the
160e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   parameter-declaration-clause of a lambda-declarator.
161e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    CheckExtraCXXDefaultArguments(ParamInfo);
162e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
163e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope);
164e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // FIXME: Can these asserts actually fail?
165e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    assert(MethodTyInfo && "no type from lambda-declarator");
166e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    EndLoc = ParamInfo.getSourceRange().getEnd();
167dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
168dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    ExplicitResultType
169dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor      = MethodTyInfo->getType()->getAs<FunctionType>()->getResultType()
170dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                                        != Context.DependentTy;
171e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  }
172e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
173dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range,
174dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                                MethodTyInfo, EndLoc);
175dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor
176503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor  // Attributes on the lambda apply to the method.
177e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  ProcessDeclAttributes(CurScope, Method, ParamInfo);
178e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
179503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor  // Introduce the function call operator as the current declaration context.
180e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  PushDeclContext(CurScope, Method);
181e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
182e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Introduce the lambda scope.
183dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  LambdaScopeInfo *LSI
184dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    = enterLambdaScope(Method, Intro.Range, Intro.Default, ExplicitParams,
185dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                       ExplicitResultType,
186dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                       (Method->getTypeQualifiers() & Qualifiers::Const) == 0);
187e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
188e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Handle explicit captures.
1893ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor  SourceLocation PrevCaptureLoc
1903ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor    = Intro.Default == LCD_None? Intro.Range.getBegin() : Intro.DefaultLoc;
191e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  for (llvm::SmallVector<LambdaCapture, 4>::const_iterator
192e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor         C = Intro.Captures.begin(),
193e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor         E = Intro.Captures.end();
1943ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor       C != E;
1953ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor       PrevCaptureLoc = C->Loc, ++C) {
196e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (C->Kind == LCK_This) {
197e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      // C++11 [expr.prim.lambda]p8:
198e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      //   An identifier or this shall not appear more than once in a
199e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      //   lambda-capture.
200e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      if (LSI->isCXXThisCaptured()) {
201e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        Diag(C->Loc, diag::err_capture_more_than_once)
202e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor          << "'this'"
2033ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor          << SourceRange(LSI->getCXXThisCapture().getLocation())
2043ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor          << FixItHint::CreateRemoval(
2053ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor               SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
206e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        continue;
207e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      }
208e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
209e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      // C++11 [expr.prim.lambda]p8:
210e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      //   If a lambda-capture includes a capture-default that is =, the
211e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      //   lambda-capture shall not contain this [...].
212e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      if (Intro.Default == LCD_ByCopy) {
2133ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor        Diag(C->Loc, diag::err_this_capture_with_copy_default)
2143ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor          << FixItHint::CreateRemoval(
2153ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor               SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
216e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        continue;
217e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      }
218e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
219e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      // C++11 [expr.prim.lambda]p12:
220e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      //   If this is captured by a local lambda expression, its nearest
221e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      //   enclosing function shall be a non-static member function.
222e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      QualType ThisCaptureType = getCurrentThisType();
223e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      if (ThisCaptureType.isNull()) {
224e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        Diag(C->Loc, diag::err_this_capture) << true;
225e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        continue;
226e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      }
227e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
228e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      CheckCXXThisCapture(C->Loc, /*Explicit=*/true);
229e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      continue;
230e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
231e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
232e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    assert(C->Id && "missing identifier for capture");
233e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
234e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // C++11 [expr.prim.lambda]p8:
235e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   If a lambda-capture includes a capture-default that is &, the
236e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   identifiers in the lambda-capture shall not be preceded by &.
237e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   If a lambda-capture includes a capture-default that is =, [...]
238e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   each identifier it contains shall be preceded by &.
239e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) {
2403ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor      Diag(C->Loc, diag::err_reference_capture_with_reference_default)
2413ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor        << FixItHint::CreateRemoval(
2423ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor             SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
243e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      continue;
244e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) {
2453ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor      Diag(C->Loc, diag::err_copy_capture_with_copy_default)
2463ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor        << FixItHint::CreateRemoval(
2473ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor             SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
248e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      continue;
249e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
250e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
251e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    DeclarationNameInfo Name(C->Id, C->Loc);
252e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    LookupResult R(*this, Name, LookupOrdinaryName);
253e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    LookupName(R, CurScope);
254e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (R.isAmbiguous())
255e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      continue;
256e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (R.empty()) {
257e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      // FIXME: Disable corrections that would add qualification?
258e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      CXXScopeSpec ScopeSpec;
259e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      DeclFilterCCC<VarDecl> Validator;
260e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator))
261e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        continue;
262e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
263e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
264e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // C++11 [expr.prim.lambda]p10:
265e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   The identifiers in a capture-list are looked up using the usual rules
266e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   for unqualified name lookup (3.4.1); each such lookup shall find a
267e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   variable with automatic storage duration declared in the reaching
268e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   scope of the local lambda expression.
269e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // FIXME: Check reaching scope.
270e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    VarDecl *Var = R.getAsSingle<VarDecl>();
271e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (!Var) {
272e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id;
273e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      continue;
274e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
275e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
276e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (!Var->hasLocalStorage()) {
277e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id;
278e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      Diag(Var->getLocation(), diag::note_previous_decl) << C->Id;
279e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      continue;
280e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
281e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
282e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // C++11 [expr.prim.lambda]p8:
283e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   An identifier or this shall not appear more than once in a
284e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    //   lambda-capture.
285e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    if (LSI->isCaptured(Var)) {
286e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      Diag(C->Loc, diag::err_capture_more_than_once)
287e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        << C->Id
2883ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor        << SourceRange(LSI->getCapture(Var).getLocation())
2893ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor        << FixItHint::CreateRemoval(
2903ac109cd17151bb8ad3a40b0cbb0e1923cd6c4a0Douglas Gregor             SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
291e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      continue;
292e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
293e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
294e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef :
295e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor                                                 TryCapture_ExplicitByVal;
296e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    TryCaptureVar(Var, C->Loc, Kind);
297e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  }
298dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  finishLambdaExplicitCaptures(LSI);
299e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
300e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Set the parameters on the decl, if specified.
301e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  if (isa<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc())) {
302dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    FunctionProtoTypeLoc Proto
303dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor      = cast<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc());
304dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    addLambdaParameters(Method, CurScope, Proto.getParams());
305e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  }
306e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
307e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // FIXME: Check return type is complete, !isObjCObjectType
308e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
309dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  // Enter a new evaluation context to insulate the lambda from any
310503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor  // cleanups from the enclosing full-expression.
311503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor  PushExpressionEvaluationContext(PotentiallyEvaluated);
312e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor}
313e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
314dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregorvoid Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope,
315dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                            bool IsInstantiation) {
316e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Leave the expression-evaluation context.
317e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  DiscardCleanupsInEvaluationContext();
318e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  PopExpressionEvaluationContext();
319e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
320e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Leave the context of the lambda.
321dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  if (!IsInstantiation)
322dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    PopDeclContext();
323630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor
324630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor  // Finalize the lambda.
325630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor  LambdaScopeInfo *LSI = getCurLambda();
326630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor  CXXRecordDecl *Class = LSI->Lambda;
327630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor  Class->setInvalidDecl();
328630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor  SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
329630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor  ActOnFields(0, Class->getLocation(), Class, Fields,
330630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor              SourceLocation(), SourceLocation(), 0);
331630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor  CheckCompletedCXXClass(Class);
332630d5fffdf342128bd73f817013ee91e030daac1Douglas Gregor
333e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  PopFunctionScopeInfo();
334e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor}
335e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
336dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas GregorExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body,
337dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                 Scope *CurScope, bool IsInstantiation) {
338e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Leave the expression-evaluation context.
339e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  DiscardCleanupsInEvaluationContext();
340e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  PopExpressionEvaluationContext();
341e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
342e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  // Collect information from the lambda scope.
343e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  llvm::SmallVector<LambdaExpr::Capture, 4> Captures;
344e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  llvm::SmallVector<Expr *, 4> CaptureInits;
345e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  LambdaCaptureDefault CaptureDefault;
346e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  CXXRecordDecl *Class;
347ef7d78bd5be466c369b04af742ed8268244d4fe7Douglas Gregor  CXXMethodDecl *CallOperator;
348e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  SourceRange IntroducerRange;
349e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  bool ExplicitParams;
350dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor  bool ExplicitResultType;
351503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor  bool LambdaExprNeedsCleanups;
3529daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor  llvm::SmallVector<VarDecl *, 4> ArrayIndexVars;
3539daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor  llvm::SmallVector<unsigned, 4> ArrayIndexStarts;
354e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  {
355e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    LambdaScopeInfo *LSI = getCurLambda();
356ef7d78bd5be466c369b04af742ed8268244d4fe7Douglas Gregor    CallOperator = LSI->CallOperator;
357e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    Class = LSI->Lambda;
358e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    IntroducerRange = LSI->IntroducerRange;
359e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    ExplicitParams = LSI->ExplicitParams;
360dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    ExplicitResultType = !LSI->HasImplicitReturnType;
361503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor    LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups;
3629daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor    ArrayIndexVars.swap(LSI->ArrayIndexVars);
3639daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor    ArrayIndexStarts.swap(LSI->ArrayIndexStarts);
3649daa7bfdff7256cef693d7bf10084881bcb9253cDouglas Gregor
365e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    // Translate captures.
366e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
367e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      LambdaScopeInfo::Capture From = LSI->Captures[I];
368e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      assert(!From.isBlockCapture() && "Cannot capture __block variables");
369e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      bool IsImplicit = I >= LSI->NumExplicitCaptures;
370e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
371e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      // Handle 'this' capture.
372e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      if (From.isThisCapture()) {
373e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        Captures.push_back(LambdaExpr::Capture(From.getLocation(),
374e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor                                               IsImplicit,
375e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor                                               LCK_This));
376e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(),
377e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor                                                         getCurrentThisType(),
378e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor                                                         /*isImplicit=*/true));
379e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor        continue;
380e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      }
381e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
382e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      VarDecl *Var = From.getVariable();
383e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      // FIXME: Handle pack expansions.
384e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef;
385e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit,
386e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor                                             Kind, Var));
387e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      CaptureInits.push_back(From.getCopyExpr());
388e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
389e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
390e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    switch (LSI->ImpCaptureStyle) {
391e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    case CapturingScopeInfo::ImpCap_None:
392e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      CaptureDefault = LCD_None;
393e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      break;
394e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
395e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    case CapturingScopeInfo::ImpCap_LambdaByval:
396e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      CaptureDefault = LCD_ByCopy;
397e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      break;
398e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
399e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    case CapturingScopeInfo::ImpCap_LambdaByref:
400e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      CaptureDefault = LCD_ByRef;
401e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      break;
402e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
403e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    case CapturingScopeInfo::ImpCap_Block:
404e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      llvm_unreachable("block capture in lambda");
405e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor      break;
406e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor    }
407e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
40854042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor    // C++11 [expr.prim.lambda]p4:
40954042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor    //   If a lambda-expression does not include a
41054042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor    //   trailing-return-type, it is as if the trailing-return-type
41154042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor    //   denotes the following type:
41254042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor    // FIXME: Assumes current resolution to core issue 975.
41354042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor    if (LSI->HasImplicitReturnType) {
41454042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      //   - if there are no return statements in the
41554042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      //     compound-statement, or all return statements return
41654042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      //     either an expression of type void or no expression or
41754042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      //     braced-init-list, the type void;
41854042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      if (LSI->ReturnType.isNull()) {
41954042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        LSI->ReturnType = Context.VoidTy;
42054042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      } else {
42154042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        // C++11 [expr.prim.lambda]p4:
42254042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //   - if the compound-statement is of the form
42354042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //
42454042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //       { attribute-specifier-seq[opt] return expression ; }
42554042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //
42654042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //     the type of the returned expression after
42754042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //     lvalue-to-rvalue conversion (4.1), array-to-pointer
42854042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //     conver- sion (4.2), and function-to-pointer conversion
42954042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //     (4.3);
43054042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        //
43154042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        // Since we're accepting the resolution to a post-C++11 core
43254042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        // issue with a non-trivial extension, provide a warning (by
43354042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        // default).
43454042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        CompoundStmt *CompoundBody = cast<CompoundStmt>(Body);
43554042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        if (!(CompoundBody->size() == 1 &&
43654042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor              isa<ReturnStmt>(*CompoundBody->body_begin())) &&
43754042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor            !Context.hasSameType(LSI->ReturnType, Context.VoidTy))
43854042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor          Diag(IntroducerRange.getBegin(),
43954042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor               diag::ext_lambda_implies_void_return);
44054042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      }
44154042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor
44254042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      // Create a function type with the inferred return type.
44354042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      const FunctionProtoType *Proto
44454042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        = CallOperator->getType()->getAs<FunctionProtoType>();
44554042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      QualType FunctionTy
44654042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor        = Context.getFunctionType(LSI->ReturnType,
44754042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor                                  Proto->arg_type_begin(),
44854042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor                                  Proto->getNumArgs(),
44954042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor                                  Proto->getExtProtoInfo());
45054042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor      CallOperator->setType(FunctionTy);
45154042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor    }
45254042f1bd78f1f1ea86be7d4af541462e127d2edDouglas Gregor
453215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor    // C++ [expr.prim.lambda]p7:
454215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor    //   The lambda-expression's compound-statement yields the
455215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor    //   function-body (8.4) of the function call operator [...].
456dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor    ActOnFinishFunctionBody(CallOperator, Body, IsInstantiation);
457215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor    CallOperator->setLexicalDeclContext(Class);
458215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor    Class->addDecl(CallOperator);
459215e4e17d00e12c38687a95502506d8f2ca3e646Douglas Gregor
460b555971345750350c21d541afe135054c7402933Douglas Gregor    // C++11 [expr.prim.lambda]p6:
461b555971345750350c21d541afe135054c7402933Douglas Gregor    //   The closure type for a lambda-expression with no lambda-capture
462b555971345750350c21d541afe135054c7402933Douglas Gregor    //   has a public non-virtual non-explicit const conversion function
463b555971345750350c21d541afe135054c7402933Douglas Gregor    //   to pointer to function having the same parameter and return
464b555971345750350c21d541afe135054c7402933Douglas Gregor    //   types as the closure type's function call operator.
465b555971345750350c21d541afe135054c7402933Douglas Gregor    if (Captures.empty() && CaptureDefault == LCD_None) {
466b555971345750350c21d541afe135054c7402933Douglas Gregor      const FunctionProtoType *Proto
467b555971345750350c21d541afe135054c7402933Douglas Gregor        = CallOperator->getType()->getAs<FunctionProtoType>();
468b555971345750350c21d541afe135054c7402933Douglas Gregor      QualType FunctionPtrTy;
469b555971345750350c21d541afe135054c7402933Douglas Gregor      {
470b555971345750350c21d541afe135054c7402933Douglas Gregor        FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo();
471b555971345750350c21d541afe135054c7402933Douglas Gregor        ExtInfo.TypeQuals = 0;
472b555971345750350c21d541afe135054c7402933Douglas Gregor        QualType FunctionTy
473b555971345750350c21d541afe135054c7402933Douglas Gregor          = Context.getFunctionType(Proto->getResultType(),
474b555971345750350c21d541afe135054c7402933Douglas Gregor                                    Proto->arg_type_begin(),
475b555971345750350c21d541afe135054c7402933Douglas Gregor                                    Proto->getNumArgs(),
476b555971345750350c21d541afe135054c7402933Douglas Gregor                                    ExtInfo);
477b555971345750350c21d541afe135054c7402933Douglas Gregor        FunctionPtrTy = Context.getPointerType(FunctionTy);
478b555971345750350c21d541afe135054c7402933Douglas Gregor      }
479b555971345750350c21d541afe135054c7402933Douglas Gregor
480b555971345750350c21d541afe135054c7402933Douglas Gregor      FunctionProtoType::ExtProtoInfo ExtInfo;
481b555971345750350c21d541afe135054c7402933Douglas Gregor      ExtInfo.TypeQuals = Qualifiers::Const;
482b555971345750350c21d541afe135054c7402933Douglas Gregor      QualType ConvTy = Context.getFunctionType(FunctionPtrTy, 0, 0, ExtInfo);
483b555971345750350c21d541afe135054c7402933Douglas Gregor
484b555971345750350c21d541afe135054c7402933Douglas Gregor      SourceLocation Loc = IntroducerRange.getBegin();
485b555971345750350c21d541afe135054c7402933Douglas Gregor      DeclarationName Name
486b555971345750350c21d541afe135054c7402933Douglas Gregor        = Context.DeclarationNames.getCXXConversionFunctionName(
487b555971345750350c21d541afe135054c7402933Douglas Gregor            Context.getCanonicalType(FunctionPtrTy));
488b555971345750350c21d541afe135054c7402933Douglas Gregor      DeclarationNameLoc NameLoc;
489b555971345750350c21d541afe135054c7402933Douglas Gregor      NameLoc.NamedType.TInfo = Context.getTrivialTypeSourceInfo(FunctionPtrTy,
490b555971345750350c21d541afe135054c7402933Douglas Gregor                                                                 Loc);
491b555971345750350c21d541afe135054c7402933Douglas Gregor      CXXConversionDecl *Conversion
492b555971345750350c21d541afe135054c7402933Douglas Gregor        = CXXConversionDecl::Create(Context, Class, Loc,
493b555971345750350c21d541afe135054c7402933Douglas Gregor                                    DeclarationNameInfo(Name, Loc, NameLoc),
494b555971345750350c21d541afe135054c7402933Douglas Gregor                                    ConvTy,
495b555971345750350c21d541afe135054c7402933Douglas Gregor                                    Context.getTrivialTypeSourceInfo(ConvTy,
496b555971345750350c21d541afe135054c7402933Douglas Gregor                                                                     Loc),
497b555971345750350c21d541afe135054c7402933Douglas Gregor                                    /*isInline=*/false, /*isExplicit=*/false,
498b555971345750350c21d541afe135054c7402933Douglas Gregor                                    /*isConstexpr=*/false, Body->getLocEnd());
499b555971345750350c21d541afe135054c7402933Douglas Gregor      Conversion->setAccess(AS_public);
500b555971345750350c21d541afe135054c7402933Douglas Gregor      Conversion->setImplicit(true);
501b555971345750350c21d541afe135054c7402933Douglas Gregor      Class->addDecl(Conversion);
502b555971345750350c21d541afe135054c7402933Douglas Gregor    }
503503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor
504b555971345750350c21d541afe135054c7402933Douglas Gregor    // Finalize the lambda class.
505b555971345750350c21d541afe135054c7402933Douglas Gregor    SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
506b555971345750350c21d541afe135054c7402933Douglas Gregor    ActOnFields(0, Class->getLocation(), Class, Fields,
507b555971345750350c21d541afe135054c7402933Douglas Gregor                SourceLocation(), SourceLocation(), 0);
508b555971345750350c21d541afe135054c7402933Douglas Gregor    CheckCompletedCXXClass(Class);
509b555971345750350c21d541afe135054c7402933Douglas Gregor
510e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor  }
511e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor
512503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor  if (LambdaExprNeedsCleanups)
513503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor    ExprNeedsCleanups = true;
514503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor
515e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange,
516e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor                                          CaptureDefault, Captures,
517dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                          ExplicitParams, ExplicitResultType,
518dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                          CaptureInits, ArrayIndexVars,
519dfca6f53ab97d28d43e3fa2564209df08f3d282cDouglas Gregor                                          ArrayIndexStarts, Body->getLocEnd());
520e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor
521e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  // C++11 [expr.prim.lambda]p2:
522e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  //   A lambda-expression shall not appear in an unevaluated operand
523e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  //   (Clause 5).
524e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  switch (ExprEvalContexts.back().Context) {
525e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  case Unevaluated:
526e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor    // We don't actually diagnose this case immediately, because we
527e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor    // could be within a context where we might find out later that
528e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor    // the expression is potentially evaluated (e.g., for typeid).
529e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor    ExprEvalContexts.back().Lambdas.push_back(Lambda);
530e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor    break;
531e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor
532e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  case ConstantEvaluated:
533e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  case PotentiallyEvaluated:
534e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  case PotentiallyEvaluatedIfUsed:
535e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor    break;
536e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor  }
537e2c5913c48f66bfec9e58a8ad1d90e5eeffad586Douglas Gregor
538503384f731b5abcbf870b0a5224eb920e631db0aDouglas Gregor  return MaybeBindToTemporary(Lambda);
539e2a7ad001fe1dc4a0d5fef312e7f7189e1f29369Douglas Gregor}
540