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