blob: 6e9bea781db32d52a78a2b595c3e549ca875abdb [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"
18#include "clang/AST/ExprCXX.h"
19using namespace clang;
20using namespace sema;
21
22void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
23 Declarator &ParamInfo,
24 Scope *CurScope) {
25 DeclContext *DC = CurContext;
26 while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
27 DC = DC->getParent();
28
29 // Start constructing the lambda class.
30 CXXRecordDecl *Class = CXXRecordDecl::Create(Context, TTK_Class, DC,
31 Intro.Range.getBegin(),
Douglas Gregor5e058eb2012-02-09 02:20:38 +000032 /*IdLoc=*/Intro.Range.getBegin(),
Douglas Gregore2a7ad02012-02-08 21:18:48 +000033 /*Id=*/0);
34 Class->startDefinition();
Douglas Gregor4d8d22b2012-02-10 07:45:31 +000035 Class->makeLambda();
Douglas Gregore2a7ad02012-02-08 21:18:48 +000036 CurContext->addDecl(Class);
37
38 // Build the call operator; we don't really have all the relevant information
39 // at this point, but we need something to attach child declarations to.
40 QualType MethodTy;
41 TypeSourceInfo *MethodTyInfo;
42 bool ExplicitParams = true;
43 SourceLocation EndLoc;
44 if (ParamInfo.getNumTypeObjects() == 0) {
45 // C++11 [expr.prim.lambda]p4:
46 // If a lambda-expression does not include a lambda-declarator, it is as
47 // if the lambda-declarator were ().
48 FunctionProtoType::ExtProtoInfo EPI;
49 EPI.TypeQuals |= DeclSpec::TQ_const;
50 MethodTy = Context.getFunctionType(Context.DependentTy,
51 /*Args=*/0, /*NumArgs=*/0, EPI);
52 MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy);
53 ExplicitParams = false;
54 EndLoc = Intro.Range.getEnd();
55 } else {
56 assert(ParamInfo.isFunctionDeclarator() &&
57 "lambda-declarator is a function");
58 DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo();
59
60 // C++11 [expr.prim.lambda]p5:
61 // This function call operator is declared const (9.3.1) if and only if
62 // the lambda-expression's parameter-declaration-clause is not followed
63 // by mutable. It is neither virtual nor declared volatile. [...]
64 if (!FTI.hasMutableQualifier())
65 FTI.TypeQuals |= DeclSpec::TQ_const;
66
67 // C++11 [expr.prim.lambda]p5:
68 // [...] Default arguments (8.3.6) shall not be specified in the
69 // parameter-declaration-clause of a lambda-declarator.
70 CheckExtraCXXDefaultArguments(ParamInfo);
71
72 MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope);
73 // FIXME: Can these asserts actually fail?
74 assert(MethodTyInfo && "no type from lambda-declarator");
75 MethodTy = MethodTyInfo->getType();
76 assert(!MethodTy.isNull() && "no type from lambda declarator");
77 EndLoc = ParamInfo.getSourceRange().getEnd();
78 }
79
80 // C++11 [expr.prim.lambda]p5:
81 // The closure type for a lambda-expression has a public inline function
82 // call operator (13.5.4) whose parameters and return type are described by
83 // the lambda-expression's parameter-declaration-clause and
84 // trailing-return-type respectively.
85 DeclarationName MethodName
86 = Context.DeclarationNames.getCXXOperatorName(OO_Call);
87 DeclarationNameLoc MethodNameLoc;
88 MethodNameLoc.CXXOperatorName.BeginOpNameLoc
89 = Intro.Range.getBegin().getRawEncoding();
90 MethodNameLoc.CXXOperatorName.EndOpNameLoc
91 = Intro.Range.getEnd().getRawEncoding();
92 CXXMethodDecl *Method
93 = CXXMethodDecl::Create(Context, Class, EndLoc,
94 DeclarationNameInfo(MethodName,
95 Intro.Range.getBegin(),
96 MethodNameLoc),
97 MethodTy, MethodTyInfo,
98 /*isStatic=*/false,
99 SC_None,
100 /*isInline=*/true,
101 /*isConstExpr=*/false,
102 EndLoc);
103 Method->setAccess(AS_public);
104 Class->addDecl(Method);
105 Method->setLexicalDeclContext(DC); // FIXME: Minor hack.
Douglas Gregor503384f2012-02-09 00:47:04 +0000106
107 // Attributes on the lambda apply to the method.
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000108 ProcessDeclAttributes(CurScope, Method, ParamInfo);
109
Douglas Gregor503384f2012-02-09 00:47:04 +0000110 // Introduce the function call operator as the current declaration context.
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000111 PushDeclContext(CurScope, Method);
112
113 // Introduce the lambda scope.
114 PushLambdaScope(Class, Method);
115 LambdaScopeInfo *LSI = getCurLambda();
116 if (Intro.Default == LCD_ByCopy)
117 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval;
118 else if (Intro.Default == LCD_ByRef)
119 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref;
120 LSI->IntroducerRange = Intro.Range;
121 LSI->ExplicitParams = ExplicitParams;
122 LSI->Mutable = (Method->getTypeQualifiers() & Qualifiers::Const) == 0;
123
124 // Handle explicit captures.
125 for (llvm::SmallVector<LambdaCapture, 4>::const_iterator
126 C = Intro.Captures.begin(),
127 E = Intro.Captures.end();
128 C != E; ++C) {
129 if (C->Kind == LCK_This) {
130 // C++11 [expr.prim.lambda]p8:
131 // An identifier or this shall not appear more than once in a
132 // lambda-capture.
133 if (LSI->isCXXThisCaptured()) {
134 Diag(C->Loc, diag::err_capture_more_than_once)
135 << "'this'"
136 << SourceRange(LSI->getCXXThisCapture().getLocation());
137 continue;
138 }
139
140 // C++11 [expr.prim.lambda]p8:
141 // If a lambda-capture includes a capture-default that is =, the
142 // lambda-capture shall not contain this [...].
143 if (Intro.Default == LCD_ByCopy) {
144 Diag(C->Loc, diag::err_this_capture_with_copy_default);
145 continue;
146 }
147
148 // C++11 [expr.prim.lambda]p12:
149 // If this is captured by a local lambda expression, its nearest
150 // enclosing function shall be a non-static member function.
151 QualType ThisCaptureType = getCurrentThisType();
152 if (ThisCaptureType.isNull()) {
153 Diag(C->Loc, diag::err_this_capture) << true;
154 continue;
155 }
156
157 CheckCXXThisCapture(C->Loc, /*Explicit=*/true);
158 continue;
159 }
160
161 assert(C->Id && "missing identifier for capture");
162
163 // C++11 [expr.prim.lambda]p8:
164 // If a lambda-capture includes a capture-default that is &, the
165 // identifiers in the lambda-capture shall not be preceded by &.
166 // If a lambda-capture includes a capture-default that is =, [...]
167 // each identifier it contains shall be preceded by &.
168 if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) {
169 Diag(C->Loc, diag::err_reference_capture_with_reference_default);
170 continue;
171 } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) {
172 Diag(C->Loc, diag::err_copy_capture_with_copy_default);
173 continue;
174 }
175
176 DeclarationNameInfo Name(C->Id, C->Loc);
177 LookupResult R(*this, Name, LookupOrdinaryName);
178 LookupName(R, CurScope);
179 if (R.isAmbiguous())
180 continue;
181 if (R.empty()) {
182 // FIXME: Disable corrections that would add qualification?
183 CXXScopeSpec ScopeSpec;
184 DeclFilterCCC<VarDecl> Validator;
185 if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator))
186 continue;
187 }
188
189 // C++11 [expr.prim.lambda]p10:
190 // The identifiers in a capture-list are looked up using the usual rules
191 // for unqualified name lookup (3.4.1); each such lookup shall find a
192 // variable with automatic storage duration declared in the reaching
193 // scope of the local lambda expression.
194 // FIXME: Check reaching scope.
195 VarDecl *Var = R.getAsSingle<VarDecl>();
196 if (!Var) {
197 Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id;
198 continue;
199 }
200
201 if (!Var->hasLocalStorage()) {
202 Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id;
203 Diag(Var->getLocation(), diag::note_previous_decl) << C->Id;
204 continue;
205 }
206
207 // C++11 [expr.prim.lambda]p8:
208 // An identifier or this shall not appear more than once in a
209 // lambda-capture.
210 if (LSI->isCaptured(Var)) {
211 Diag(C->Loc, diag::err_capture_more_than_once)
212 << C->Id
213 << SourceRange(LSI->getCapture(Var).getLocation());
214 continue;
215 }
216
217 TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef :
218 TryCapture_ExplicitByVal;
219 TryCaptureVar(Var, C->Loc, Kind);
220 }
221 LSI->finishedExplicitCaptures();
222
223 // Set the parameters on the decl, if specified.
224 if (isa<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc())) {
225 FunctionProtoTypeLoc Proto =
226 cast<FunctionProtoTypeLoc>(MethodTyInfo->getTypeLoc());
227 Method->setParams(Proto.getParams());
228 CheckParmsForFunctionDef(Method->param_begin(),
229 Method->param_end(),
230 /*CheckParameterNames=*/false);
231
232 // Introduce our parameters into the function scope
233 for (unsigned p = 0, NumParams = Method->getNumParams(); p < NumParams; ++p) {
234 ParmVarDecl *Param = Method->getParamDecl(p);
235 Param->setOwningFunction(Method);
236
237 // If this has an identifier, add it to the scope stack.
238 if (Param->getIdentifier()) {
239 CheckShadow(CurScope, Param);
240
241 PushOnScopeChains(Param, CurScope);
242 }
243 }
244 }
245
246 const FunctionType *Fn = MethodTy->getAs<FunctionType>();
247 QualType RetTy = Fn->getResultType();
248 if (RetTy != Context.DependentTy) {
249 LSI->ReturnType = RetTy;
250 } else {
251 LSI->HasImplicitReturnType = true;
252 }
253
254 // FIXME: Check return type is complete, !isObjCObjectType
255
Douglas Gregor503384f2012-02-09 00:47:04 +0000256 // Enter a new evaluation context to insulate the block from any
257 // cleanups from the enclosing full-expression.
258 PushExpressionEvaluationContext(PotentiallyEvaluated);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000259}
260
261void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope) {
262 // Leave the expression-evaluation context.
263 DiscardCleanupsInEvaluationContext();
264 PopExpressionEvaluationContext();
265
266 // Leave the context of the lambda.
267 PopDeclContext();
Douglas Gregor630d5ff2012-02-09 01:28:42 +0000268
269 // Finalize the lambda.
270 LambdaScopeInfo *LSI = getCurLambda();
271 CXXRecordDecl *Class = LSI->Lambda;
272 Class->setInvalidDecl();
273 SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
274 ActOnFields(0, Class->getLocation(), Class, Fields,
275 SourceLocation(), SourceLocation(), 0);
276 CheckCompletedCXXClass(Class);
277
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000278 PopFunctionScopeInfo();
279}
280
281ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc,
282 Stmt *Body, Scope *CurScope) {
283 // Leave the expression-evaluation context.
284 DiscardCleanupsInEvaluationContext();
285 PopExpressionEvaluationContext();
286
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000287 // Collect information from the lambda scope.
288 llvm::SmallVector<LambdaExpr::Capture, 4> Captures;
289 llvm::SmallVector<Expr *, 4> CaptureInits;
290 LambdaCaptureDefault CaptureDefault;
291 CXXRecordDecl *Class;
292 SourceRange IntroducerRange;
293 bool ExplicitParams;
Douglas Gregor503384f2012-02-09 00:47:04 +0000294 bool LambdaExprNeedsCleanups;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000295 {
296 LambdaScopeInfo *LSI = getCurLambda();
Douglas Gregor54042f12012-02-09 10:18:50 +0000297 CXXMethodDecl *CallOperator = LSI->CallOperator;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000298 Class = LSI->Lambda;
299 IntroducerRange = LSI->IntroducerRange;
300 ExplicitParams = LSI->ExplicitParams;
Douglas Gregor503384f2012-02-09 00:47:04 +0000301 LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups;
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000302
303 // Translate captures.
304 for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
305 LambdaScopeInfo::Capture From = LSI->Captures[I];
306 assert(!From.isBlockCapture() && "Cannot capture __block variables");
307 bool IsImplicit = I >= LSI->NumExplicitCaptures;
308
309 // Handle 'this' capture.
310 if (From.isThisCapture()) {
311 Captures.push_back(LambdaExpr::Capture(From.getLocation(),
312 IsImplicit,
313 LCK_This));
314 CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(),
315 getCurrentThisType(),
316 /*isImplicit=*/true));
317 continue;
318 }
319
320 VarDecl *Var = From.getVariable();
321 // FIXME: Handle pack expansions.
322 LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef;
323 Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit,
324 Kind, Var));
325 CaptureInits.push_back(From.getCopyExpr());
326 }
327
328 switch (LSI->ImpCaptureStyle) {
329 case CapturingScopeInfo::ImpCap_None:
330 CaptureDefault = LCD_None;
331 break;
332
333 case CapturingScopeInfo::ImpCap_LambdaByval:
334 CaptureDefault = LCD_ByCopy;
335 break;
336
337 case CapturingScopeInfo::ImpCap_LambdaByref:
338 CaptureDefault = LCD_ByRef;
339 break;
340
341 case CapturingScopeInfo::ImpCap_Block:
342 llvm_unreachable("block capture in lambda");
343 break;
344 }
345
Douglas Gregor54042f12012-02-09 10:18:50 +0000346 // C++11 [expr.prim.lambda]p4:
347 // If a lambda-expression does not include a
348 // trailing-return-type, it is as if the trailing-return-type
349 // denotes the following type:
350 // FIXME: Assumes current resolution to core issue 975.
351 if (LSI->HasImplicitReturnType) {
352 // - if there are no return statements in the
353 // compound-statement, or all return statements return
354 // either an expression of type void or no expression or
355 // braced-init-list, the type void;
356 if (LSI->ReturnType.isNull()) {
357 LSI->ReturnType = Context.VoidTy;
358 } else {
359 // C++11 [expr.prim.lambda]p4:
360 // - if the compound-statement is of the form
361 //
362 // { attribute-specifier-seq[opt] return expression ; }
363 //
364 // the type of the returned expression after
365 // lvalue-to-rvalue conversion (4.1), array-to-pointer
366 // conver- sion (4.2), and function-to-pointer conversion
367 // (4.3);
368 //
369 // Since we're accepting the resolution to a post-C++11 core
370 // issue with a non-trivial extension, provide a warning (by
371 // default).
372 CompoundStmt *CompoundBody = cast<CompoundStmt>(Body);
373 if (!(CompoundBody->size() == 1 &&
374 isa<ReturnStmt>(*CompoundBody->body_begin())) &&
375 !Context.hasSameType(LSI->ReturnType, Context.VoidTy))
376 Diag(IntroducerRange.getBegin(),
377 diag::ext_lambda_implies_void_return);
378 }
379
380 // Create a function type with the inferred return type.
381 const FunctionProtoType *Proto
382 = CallOperator->getType()->getAs<FunctionProtoType>();
383 QualType FunctionTy
384 = Context.getFunctionType(LSI->ReturnType,
385 Proto->arg_type_begin(),
386 Proto->getNumArgs(),
387 Proto->getExtProtoInfo());
388 CallOperator->setType(FunctionTy);
389 }
390
Douglas Gregor503384f2012-02-09 00:47:04 +0000391 // Finalize the lambda class.
392 SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end());
393 ActOnFields(0, Class->getLocation(), Class, Fields,
394 SourceLocation(), SourceLocation(), 0);
395 CheckCompletedCXXClass(Class);
396
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000397 // C++ [expr.prim.lambda]p7:
398 // The lambda-expression's compound-statement yields the
399 // function-body (8.4) of the function call operator [...].
Douglas Gregor760b37b2012-02-09 08:52:43 +0000400 ActOnFinishFunctionBody(CallOperator, Body, /*IsInstantation=*/false);
401 CallOperator->setLexicalDeclContext(Class);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000402 }
403
Douglas Gregor503384f2012-02-09 00:47:04 +0000404 if (LambdaExprNeedsCleanups)
405 ExprNeedsCleanups = true;
406
Douglas Gregore2c59132012-02-09 08:14:43 +0000407 LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange,
408 CaptureDefault, Captures,
409 ExplicitParams, CaptureInits,
410 Body->getLocEnd());
Douglas Gregor4d8d22b2012-02-10 07:45:31 +0000411 Class->setLambda(Lambda);
Douglas Gregore2c59132012-02-09 08:14:43 +0000412
413 // C++11 [expr.prim.lambda]p2:
414 // A lambda-expression shall not appear in an unevaluated operand
415 // (Clause 5).
416 switch (ExprEvalContexts.back().Context) {
417 case Unevaluated:
418 // We don't actually diagnose this case immediately, because we
419 // could be within a context where we might find out later that
420 // the expression is potentially evaluated (e.g., for typeid).
421 ExprEvalContexts.back().Lambdas.push_back(Lambda);
422 break;
423
424 case ConstantEvaluated:
425 case PotentiallyEvaluated:
426 case PotentiallyEvaluatedIfUsed:
427 break;
428 }
429
Douglas Gregor503384f2012-02-09 00:47:04 +0000430 return MaybeBindToTemporary(Lambda);
Douglas Gregore2a7ad02012-02-08 21:18:48 +0000431}