blob: f97b5232acfd7f90f5b9dbd081ba0ff7382f3a3f [file] [log] [blame]
Douglas Gregore2a7ad02012-02-08 21:18:48 +00001//===--- SemaLambda.cpp - Semantic Analysis for C++11 Lambdas -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for C++ lambda expressions.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Sema/DeclSpec.h"
14#include "clang/Sema/Initialization.h"
15#include "clang/Sema/Lookup.h"
16#include "clang/Sema/ScopeInfo.h"
17#include "clang/Sema/SemaInternal.h"
Douglas Gregor3ac109c2012-02-10 17:46:20 +000018#include "clang/Lex/Preprocessor.h"
Douglas Gregore2a7ad02012-02-08 21:18:48 +000019#include "clang/AST/ExprCXX.h"
20using namespace clang;
21using namespace sema;
22
Douglas Gregordfca6f52012-02-13 22:00:16 +000023CXXRecordDecl *Sema::createLambdaClosureType(SourceRange IntroducerRange) {
Douglas Gregore2a7ad02012-02-08 21:18:48 +000024 DeclContext *DC = CurContext;
25 while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
26 DC = DC->getParent();
Douglas Gregordfca6f52012-02-13 22:00:16 +000027
Douglas Gregore2a7ad02012-02-08 21:18:48 +000028 // Start constructing the lambda class.
Douglas Gregorda8962a2012-02-13 15:44:47 +000029 CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(Context, DC,
Douglas Gregordfca6f52012-02-13 22:00:16 +000030 IntroducerRange.getBegin());
Douglas Gregore2a7ad02012-02-08 21:18:48 +000031 CurContext->addDecl(Class);
Douglas Gregordfca6f52012-02-13 22:00:16 +000032
33 return Class;
34}
Douglas Gregore2a7ad02012-02-08 21:18:48 +000035
Douglas Gregordfca6f52012-02-13 22:00:16 +000036CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class,
37 SourceRange IntroducerRange,
38 TypeSourceInfo *MethodType,
39 SourceLocation EndLoc) {
40 // C++11 [expr.prim.lambda]p5:
41 // The closure type for a lambda-expression has a public inline function
42 // call operator (13.5.4) whose parameters and return type are described by
43 // the lambda-expression's parameter-declaration-clause and
44 // trailing-return-type respectively.
45 DeclarationName MethodName
46 = Context.DeclarationNames.getCXXOperatorName(OO_Call);
47 DeclarationNameLoc MethodNameLoc;
48 MethodNameLoc.CXXOperatorName.BeginOpNameLoc
49 = IntroducerRange.getBegin().getRawEncoding();
50 MethodNameLoc.CXXOperatorName.EndOpNameLoc
51 = IntroducerRange.getEnd().getRawEncoding();
52 CXXMethodDecl *Method
53 = CXXMethodDecl::Create(Context, Class, EndLoc,
54 DeclarationNameInfo(MethodName,
55 IntroducerRange.getBegin(),
56 MethodNameLoc),
57 MethodType->getType(), MethodType,
58 /*isStatic=*/false,
59 SC_None,
60 /*isInline=*/true,
61 /*isConstExpr=*/false,
62 EndLoc);
63 Method->setAccess(AS_public);
64
65 // Temporarily set the lexical declaration context to the current
66 // context, so that the Scope stack matches the lexical nesting.
67 Method->setLexicalDeclContext(Class->getDeclContext());
68
69 return Method;
70}
71
72LambdaScopeInfo *Sema::enterLambdaScope(CXXMethodDecl *CallOperator,
73 SourceRange IntroducerRange,
74 LambdaCaptureDefault CaptureDefault,
75 bool ExplicitParams,
76 bool ExplicitResultType,
77 bool Mutable) {
78 PushLambdaScope(CallOperator->getParent(), CallOperator);
79 LambdaScopeInfo *LSI = getCurLambda();
80 if (CaptureDefault == LCD_ByCopy)
81 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval;
82 else if (CaptureDefault == LCD_ByRef)
83 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref;
84 LSI->IntroducerRange = IntroducerRange;
85 LSI->ExplicitParams = ExplicitParams;
86 LSI->Mutable = Mutable;
87
88 if (ExplicitResultType) {
89 LSI->ReturnType = CallOperator->getResultType();
90 } else {
91 LSI->HasImplicitReturnType = true;
92 }
93
94 return LSI;
95}
96
97void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) {
98 LSI->finishedExplicitCaptures();
99}
100
101void Sema::addLambdaParameters(CXXMethodDecl *CallOperator, Scope *CurScope,
102 llvm::ArrayRef<ParmVarDecl *> Params) {
103 CallOperator->setParams(Params);
104 CheckParmsForFunctionDef(const_cast<ParmVarDecl **>(Params.begin()),
105 const_cast<ParmVarDecl **>(Params.end()),
106 /*CheckParameterNames=*/false);
107
108 // Introduce our parameters into the function scope
109 for (unsigned p = 0, NumParams = CallOperator->getNumParams();
110 p < NumParams; ++p) {
111 ParmVarDecl *Param = CallOperator->getParamDecl(p);
112 Param->setOwningFunction(CallOperator);
113
114 // If this has an identifier, add it to the scope stack.
115 if (CurScope && Param->getIdentifier()) {
116 CheckShadow(CurScope, Param);
117
118 PushOnScopeChains(Param, CurScope);
119 }
120 }
121}
122
123void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
124 Declarator &ParamInfo,
125 Scope *CurScope) {
126 CXXRecordDecl *Class = createLambdaClosureType(Intro.Range);
127
128 // Determine the signature of the call operator.
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000129 TypeSourceInfo *MethodTyInfo;
130 bool ExplicitParams = true;
Douglas Gregordfca6f52012-02-13 22:00:16 +0000131 bool ExplicitResultType = true;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000132 SourceLocation EndLoc;
133 if (ParamInfo.getNumTypeObjects() == 0) {
134 // C++11 [expr.prim.lambda]p4:
135 // If a lambda-expression does not include a lambda-declarator, it is as
136 // if the lambda-declarator were ().
137 FunctionProtoType::ExtProtoInfo EPI;
Richard Smitheefb3d52012-02-10 09:58:53 +0000138 EPI.HasTrailingReturn = true;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000139 EPI.TypeQuals |= DeclSpec::TQ_const;
Douglas Gregordfca6f52012-02-13 22:00:16 +0000140 QualType MethodTy = Context.getFunctionType(Context.DependentTy,
141 /*Args=*/0, /*NumArgs=*/0, EPI);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000142 MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy);
143 ExplicitParams = false;
Douglas Gregordfca6f52012-02-13 22:00:16 +0000144 ExplicitResultType = false;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000145 EndLoc = Intro.Range.getEnd();
146 } else {
147 assert(ParamInfo.isFunctionDeclarator() &&
148 "lambda-declarator is a function");
149 DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo();
150
151 // C++11 [expr.prim.lambda]p5:
152 // This function call operator is declared const (9.3.1) if and only if
153 // the lambda-expression's parameter-declaration-clause is not followed
154 // by mutable. It is neither virtual nor declared volatile. [...]
155 if (!FTI.hasMutableQualifier())
156 FTI.TypeQuals |= DeclSpec::TQ_const;
157
158 // C++11 [expr.prim.lambda]p5:
159 // [...] Default arguments (8.3.6) shall not be specified in the
160 // parameter-declaration-clause of a lambda-declarator.
161 CheckExtraCXXDefaultArguments(ParamInfo);
162
163 MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope);
164 // FIXME: Can these asserts actually fail?
165 assert(MethodTyInfo && "no type from lambda-declarator");
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000166 EndLoc = ParamInfo.getSourceRange().getEnd();
Douglas Gregordfca6f52012-02-13 22:00:16 +0000167
168 ExplicitResultType
169 = MethodTyInfo->getType()->getAs<FunctionType>()->getResultType()
170 != Context.DependentTy;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000171 }
172
Douglas Gregordfca6f52012-02-13 22:00:16 +0000173 CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range,
174 MethodTyInfo, EndLoc);
175
Douglas Gregor503384f2012-02-09 00:47:04 +0000176 // Attributes on the lambda apply to the method.
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000177 ProcessDeclAttributes(CurScope, Method, ParamInfo);
178
Douglas Gregor503384f2012-02-09 00:47:04 +0000179 // Introduce the function call operator as the current declaration context.
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000180 PushDeclContext(CurScope, Method);
181
182 // Introduce the lambda scope.
Douglas Gregordfca6f52012-02-13 22:00:16 +0000183 LambdaScopeInfo *LSI
184 = enterLambdaScope(Method, Intro.Range, Intro.Default, ExplicitParams,
185 ExplicitResultType,
186 (Method->getTypeQualifiers() & Qualifiers::Const) == 0);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000187
188 // Handle explicit captures.
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000189 SourceLocation PrevCaptureLoc
190 = Intro.Default == LCD_None? Intro.Range.getBegin() : Intro.DefaultLoc;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000191 for (llvm::SmallVector<LambdaCapture, 4>::const_iterator
192 C = Intro.Captures.begin(),
193 E = Intro.Captures.end();
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000194 C != E;
195 PrevCaptureLoc = C->Loc, ++C) {
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000196 if (C->Kind == LCK_This) {
197 // C++11 [expr.prim.lambda]p8:
198 // An identifier or this shall not appear more than once in a
199 // lambda-capture.
200 if (LSI->isCXXThisCaptured()) {
201 Diag(C->Loc, diag::err_capture_more_than_once)
202 << "'this'"
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000203 << SourceRange(LSI->getCXXThisCapture().getLocation())
204 << FixItHint::CreateRemoval(
205 SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000206 continue;
207 }
208
209 // C++11 [expr.prim.lambda]p8:
210 // If a lambda-capture includes a capture-default that is =, the
211 // lambda-capture shall not contain this [...].
212 if (Intro.Default == LCD_ByCopy) {
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000213 Diag(C->Loc, diag::err_this_capture_with_copy_default)
214 << FixItHint::CreateRemoval(
215 SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000216 continue;
217 }
218
219 // C++11 [expr.prim.lambda]p12:
220 // If this is captured by a local lambda expression, its nearest
221 // enclosing function shall be a non-static member function.
222 QualType ThisCaptureType = getCurrentThisType();
223 if (ThisCaptureType.isNull()) {
224 Diag(C->Loc, diag::err_this_capture) << true;
225 continue;
226 }
227
228 CheckCXXThisCapture(C->Loc, /*Explicit=*/true);
229 continue;
230 }
231
232 assert(C->Id && "missing identifier for capture");
233
234 // C++11 [expr.prim.lambda]p8:
235 // If a lambda-capture includes a capture-default that is &, the
236 // identifiers in the lambda-capture shall not be preceded by &.
237 // If a lambda-capture includes a capture-default that is =, [...]
238 // each identifier it contains shall be preceded by &.
239 if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) {
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000240 Diag(C->Loc, diag::err_reference_capture_with_reference_default)
241 << FixItHint::CreateRemoval(
242 SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000243 continue;
244 } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) {
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000245 Diag(C->Loc, diag::err_copy_capture_with_copy_default)
246 << FixItHint::CreateRemoval(
247 SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000248 continue;
249 }
250
251 DeclarationNameInfo Name(C->Id, C->Loc);
252 LookupResult R(*this, Name, LookupOrdinaryName);
253 LookupName(R, CurScope);
254 if (R.isAmbiguous())
255 continue;
256 if (R.empty()) {
257 // FIXME: Disable corrections that would add qualification?
258 CXXScopeSpec ScopeSpec;
259 DeclFilterCCC<VarDecl> Validator;
260 if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator))
261 continue;
262 }
263
264 // C++11 [expr.prim.lambda]p10:
265 // The identifiers in a capture-list are looked up using the usual rules
266 // for unqualified name lookup (3.4.1); each such lookup shall find a
267 // variable with automatic storage duration declared in the reaching
268 // scope of the local lambda expression.
269 // FIXME: Check reaching scope.
270 VarDecl *Var = R.getAsSingle<VarDecl>();
271 if (!Var) {
272 Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id;
273 continue;
274 }
275
276 if (!Var->hasLocalStorage()) {
277 Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id;
278 Diag(Var->getLocation(), diag::note_previous_decl) << C->Id;
279 continue;
280 }
281
282 // C++11 [expr.prim.lambda]p8:
283 // An identifier or this shall not appear more than once in a
284 // lambda-capture.
285 if (LSI->isCaptured(Var)) {
286 Diag(C->Loc, diag::err_capture_more_than_once)
287 << C->Id
Douglas Gregor3ac109c2012-02-10 17:46:20 +0000288 << SourceRange(LSI->getCapture(Var).getLocation())
289 << FixItHint::CreateRemoval(
290 SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc));
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000291 continue;
292 }
293
294 TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef :
295 TryCapture_ExplicitByVal;
296 TryCaptureVar(Var, C->Loc, Kind);
297 }
Douglas Gregordfca6f52012-02-13 22:00:16 +0000298 finishLambdaExplicitCaptures(LSI);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000299
300 // Set the parameters on the decl, if specified.
301 if (isa<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc())) {
Douglas Gregordfca6f52012-02-13 22:00:16 +0000302 FunctionProtoTypeLoc Proto
303 = cast<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc());
304 addLambdaParameters(Method, CurScope, Proto.getParams());
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000305 }
306
307 // FIXME: Check return type is complete, !isObjCObjectType
308
Douglas Gregordfca6f52012-02-13 22:00:16 +0000309 // Enter a new evaluation context to insulate the lambda from any
Douglas Gregor503384f2012-02-09 00:47:04 +0000310 // cleanups from the enclosing full-expression.
311 PushExpressionEvaluationContext(PotentiallyEvaluated);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000312}
313
Douglas Gregordfca6f52012-02-13 22:00:16 +0000314void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope,
315 bool IsInstantiation) {
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000316 // Leave the expression-evaluation context.
317 DiscardCleanupsInEvaluationContext();
318 PopExpressionEvaluationContext();
319
320 // Leave the context of the lambda.
Douglas Gregordfca6f52012-02-13 22:00:16 +0000321 if (!IsInstantiation)
322 PopDeclContext();
Douglas Gregor630d5ff2012-02-09 01:28:42 +0000323
324 // Finalize the lambda.
325 LambdaScopeInfo *LSI = getCurLambda();
326 CXXRecordDecl *Class = LSI->Lambda;
327 Class->setInvalidDecl();
328 SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
329 ActOnFields(0, Class->getLocation(), Class, Fields,
330 SourceLocation(), SourceLocation(), 0);
331 CheckCompletedCXXClass(Class);
332
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000333 PopFunctionScopeInfo();
334}
335
Douglas Gregordfca6f52012-02-13 22:00:16 +0000336ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body,
337 Scope *CurScope, bool IsInstantiation) {
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000338 // Leave the expression-evaluation context.
339 DiscardCleanupsInEvaluationContext();
340 PopExpressionEvaluationContext();
341
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000342 // Collect information from the lambda scope.
343 llvm::SmallVector<LambdaExpr::Capture, 4> Captures;
344 llvm::SmallVector<Expr *, 4> CaptureInits;
345 LambdaCaptureDefault CaptureDefault;
346 CXXRecordDecl *Class;
Douglas Gregoref7d78b2012-02-10 08:36:38 +0000347 CXXMethodDecl *CallOperator;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000348 SourceRange IntroducerRange;
349 bool ExplicitParams;
Douglas Gregordfca6f52012-02-13 22:00:16 +0000350 bool ExplicitResultType;
Douglas Gregor503384f2012-02-09 00:47:04 +0000351 bool LambdaExprNeedsCleanups;
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000352 llvm::SmallVector<VarDecl *, 4> ArrayIndexVars;
353 llvm::SmallVector<unsigned, 4> ArrayIndexStarts;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000354 {
355 LambdaScopeInfo *LSI = getCurLambda();
Douglas Gregoref7d78b2012-02-10 08:36:38 +0000356 CallOperator = LSI->CallOperator;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000357 Class = LSI->Lambda;
358 IntroducerRange = LSI->IntroducerRange;
359 ExplicitParams = LSI->ExplicitParams;
Douglas Gregordfca6f52012-02-13 22:00:16 +0000360 ExplicitResultType = !LSI->HasImplicitReturnType;
Douglas Gregor503384f2012-02-09 00:47:04 +0000361 LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups;
Douglas Gregor9daa7bf2012-02-13 16:35:30 +0000362 ArrayIndexVars.swap(LSI->ArrayIndexVars);
363 ArrayIndexStarts.swap(LSI->ArrayIndexStarts);
364
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000365 // Translate captures.
366 for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
367 LambdaScopeInfo::Capture From = LSI->Captures[I];
368 assert(!From.isBlockCapture() && "Cannot capture __block variables");
369 bool IsImplicit = I >= LSI->NumExplicitCaptures;
370
371 // Handle 'this' capture.
372 if (From.isThisCapture()) {
373 Captures.push_back(LambdaExpr::Capture(From.getLocation(),
374 IsImplicit,
375 LCK_This));
376 CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(),
377 getCurrentThisType(),
378 /*isImplicit=*/true));
379 continue;
380 }
381
382 VarDecl *Var = From.getVariable();
383 // FIXME: Handle pack expansions.
384 LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef;
385 Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit,
386 Kind, Var));
387 CaptureInits.push_back(From.getCopyExpr());
388 }
389
390 switch (LSI->ImpCaptureStyle) {
391 case CapturingScopeInfo::ImpCap_None:
392 CaptureDefault = LCD_None;
393 break;
394
395 case CapturingScopeInfo::ImpCap_LambdaByval:
396 CaptureDefault = LCD_ByCopy;
397 break;
398
399 case CapturingScopeInfo::ImpCap_LambdaByref:
400 CaptureDefault = LCD_ByRef;
401 break;
402
403 case CapturingScopeInfo::ImpCap_Block:
404 llvm_unreachable("block capture in lambda");
405 break;
406 }
407
Douglas Gregor54042f12012-02-09 10:18:50 +0000408 // C++11 [expr.prim.lambda]p4:
409 // If a lambda-expression does not include a
410 // trailing-return-type, it is as if the trailing-return-type
411 // denotes the following type:
412 // FIXME: Assumes current resolution to core issue 975.
413 if (LSI->HasImplicitReturnType) {
414 // - if there are no return statements in the
415 // compound-statement, or all return statements return
416 // either an expression of type void or no expression or
417 // braced-init-list, the type void;
418 if (LSI->ReturnType.isNull()) {
419 LSI->ReturnType = Context.VoidTy;
420 } else {
421 // C++11 [expr.prim.lambda]p4:
422 // - if the compound-statement is of the form
423 //
424 // { attribute-specifier-seq[opt] return expression ; }
425 //
426 // the type of the returned expression after
427 // lvalue-to-rvalue conversion (4.1), array-to-pointer
428 // conver- sion (4.2), and function-to-pointer conversion
429 // (4.3);
430 //
431 // Since we're accepting the resolution to a post-C++11 core
432 // issue with a non-trivial extension, provide a warning (by
433 // default).
434 CompoundStmt *CompoundBody = cast<CompoundStmt>(Body);
435 if (!(CompoundBody->size() == 1 &&
436 isa<ReturnStmt>(*CompoundBody->body_begin())) &&
437 !Context.hasSameType(LSI->ReturnType, Context.VoidTy))
438 Diag(IntroducerRange.getBegin(),
439 diag::ext_lambda_implies_void_return);
440 }
441
442 // Create a function type with the inferred return type.
443 const FunctionProtoType *Proto
444 = CallOperator->getType()->getAs<FunctionProtoType>();
445 QualType FunctionTy
446 = Context.getFunctionType(LSI->ReturnType,
447 Proto->arg_type_begin(),
448 Proto->getNumArgs(),
449 Proto->getExtProtoInfo());
450 CallOperator->setType(FunctionTy);
451 }
452
Douglas Gregor215e4e12012-02-12 17:34:23 +0000453 // C++ [expr.prim.lambda]p7:
454 // The lambda-expression's compound-statement yields the
455 // function-body (8.4) of the function call operator [...].
Douglas Gregordfca6f52012-02-13 22:00:16 +0000456 ActOnFinishFunctionBody(CallOperator, Body, IsInstantiation);
Douglas Gregor215e4e12012-02-12 17:34:23 +0000457 CallOperator->setLexicalDeclContext(Class);
458 Class->addDecl(CallOperator);
459
Douglas Gregorb5559712012-02-10 16:13:20 +0000460 // C++11 [expr.prim.lambda]p6:
461 // The closure type for a lambda-expression with no lambda-capture
462 // has a public non-virtual non-explicit const conversion function
463 // to pointer to function having the same parameter and return
464 // types as the closure type's function call operator.
465 if (Captures.empty() && CaptureDefault == LCD_None) {
466 const FunctionProtoType *Proto
467 = CallOperator->getType()->getAs<FunctionProtoType>();
468 QualType FunctionPtrTy;
469 {
470 FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo();
471 ExtInfo.TypeQuals = 0;
472 QualType FunctionTy
473 = Context.getFunctionType(Proto->getResultType(),
474 Proto->arg_type_begin(),
475 Proto->getNumArgs(),
476 ExtInfo);
477 FunctionPtrTy = Context.getPointerType(FunctionTy);
478 }
479
480 FunctionProtoType::ExtProtoInfo ExtInfo;
481 ExtInfo.TypeQuals = Qualifiers::Const;
482 QualType ConvTy = Context.getFunctionType(FunctionPtrTy, 0, 0, ExtInfo);
483
484 SourceLocation Loc = IntroducerRange.getBegin();
485 DeclarationName Name
486 = Context.DeclarationNames.getCXXConversionFunctionName(
487 Context.getCanonicalType(FunctionPtrTy));
488 DeclarationNameLoc NameLoc;
489 NameLoc.NamedType.TInfo = Context.getTrivialTypeSourceInfo(FunctionPtrTy,
490 Loc);
491 CXXConversionDecl *Conversion
492 = CXXConversionDecl::Create(Context, Class, Loc,
493 DeclarationNameInfo(Name, Loc, NameLoc),
494 ConvTy,
495 Context.getTrivialTypeSourceInfo(ConvTy,
496 Loc),
497 /*isInline=*/false, /*isExplicit=*/false,
498 /*isConstexpr=*/false, Body->getLocEnd());
499 Conversion->setAccess(AS_public);
500 Conversion->setImplicit(true);
501 Class->addDecl(Conversion);
502 }
Douglas Gregor503384f2012-02-09 00:47:04 +0000503
Douglas Gregorb5559712012-02-10 16:13:20 +0000504 // Finalize the lambda class.
505 SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
Douglas Gregorb5559712012-02-10 16:13:20 +0000506 ActOnFields(0, Class->getLocation(), Class, Fields,
507 SourceLocation(), SourceLocation(), 0);
508 CheckCompletedCXXClass(Class);
509
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000510 }
511
Douglas Gregor503384f2012-02-09 00:47:04 +0000512 if (LambdaExprNeedsCleanups)
513 ExprNeedsCleanups = true;
514
Douglas Gregore2c59132012-02-09 08:14:43 +0000515 LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange,
516 CaptureDefault, Captures,
Douglas Gregordfca6f52012-02-13 22:00:16 +0000517 ExplicitParams, ExplicitResultType,
518 CaptureInits, ArrayIndexVars,
519 ArrayIndexStarts, Body->getLocEnd());
Douglas Gregore2c59132012-02-09 08:14:43 +0000520
521 // C++11 [expr.prim.lambda]p2:
522 // A lambda-expression shall not appear in an unevaluated operand
523 // (Clause 5).
524 switch (ExprEvalContexts.back().Context) {
525 case Unevaluated:
526 // We don't actually diagnose this case immediately, because we
527 // could be within a context where we might find out later that
528 // the expression is potentially evaluated (e.g., for typeid).
529 ExprEvalContexts.back().Lambdas.push_back(Lambda);
530 break;
531
532 case ConstantEvaluated:
533 case PotentiallyEvaluated:
534 case PotentiallyEvaluatedIfUsed:
535 break;
536 }
537
Douglas Gregor503384f2012-02-09 00:47:04 +0000538 return MaybeBindToTemporary(Lambda);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000539}