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" |
| 18 | #include "clang/AST/ExprCXX.h" |
| 19 | using namespace clang; |
| 20 | using namespace sema; |
| 21 | |
| 22 | void 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(), |
| 32 | /*IdLoc=*/SourceLocation(), |
| 33 | /*Id=*/0); |
| 34 | Class->startDefinition(); |
| 35 | Class->setLambda(true); |
| 36 | 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 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. |
| 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 Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 256 | // Enter a new evaluation context to insulate the block from any |
| 257 | // cleanups from the enclosing full-expression. |
| 258 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void 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(); |
| 268 | PopFunctionScopeInfo(); |
| 269 | } |
| 270 | |
| 271 | ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, |
| 272 | Stmt *Body, Scope *CurScope) { |
| 273 | // Leave the expression-evaluation context. |
| 274 | DiscardCleanupsInEvaluationContext(); |
| 275 | PopExpressionEvaluationContext(); |
| 276 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 277 | // Collect information from the lambda scope. |
| 278 | llvm::SmallVector<LambdaExpr::Capture, 4> Captures; |
| 279 | llvm::SmallVector<Expr *, 4> CaptureInits; |
| 280 | LambdaCaptureDefault CaptureDefault; |
| 281 | CXXRecordDecl *Class; |
| 282 | SourceRange IntroducerRange; |
| 283 | bool ExplicitParams; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 284 | bool LambdaExprNeedsCleanups; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 285 | { |
| 286 | LambdaScopeInfo *LSI = getCurLambda(); |
| 287 | Class = LSI->Lambda; |
| 288 | IntroducerRange = LSI->IntroducerRange; |
| 289 | ExplicitParams = LSI->ExplicitParams; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 290 | LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 291 | |
| 292 | // Translate captures. |
| 293 | for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) { |
| 294 | LambdaScopeInfo::Capture From = LSI->Captures[I]; |
| 295 | assert(!From.isBlockCapture() && "Cannot capture __block variables"); |
| 296 | bool IsImplicit = I >= LSI->NumExplicitCaptures; |
| 297 | |
| 298 | // Handle 'this' capture. |
| 299 | if (From.isThisCapture()) { |
| 300 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), |
| 301 | IsImplicit, |
| 302 | LCK_This)); |
| 303 | CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(), |
| 304 | getCurrentThisType(), |
| 305 | /*isImplicit=*/true)); |
| 306 | continue; |
| 307 | } |
| 308 | |
| 309 | VarDecl *Var = From.getVariable(); |
| 310 | // FIXME: Handle pack expansions. |
| 311 | LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef; |
| 312 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit, |
| 313 | Kind, Var)); |
| 314 | CaptureInits.push_back(From.getCopyExpr()); |
| 315 | } |
| 316 | |
| 317 | switch (LSI->ImpCaptureStyle) { |
| 318 | case CapturingScopeInfo::ImpCap_None: |
| 319 | CaptureDefault = LCD_None; |
| 320 | break; |
| 321 | |
| 322 | case CapturingScopeInfo::ImpCap_LambdaByval: |
| 323 | CaptureDefault = LCD_ByCopy; |
| 324 | break; |
| 325 | |
| 326 | case CapturingScopeInfo::ImpCap_LambdaByref: |
| 327 | CaptureDefault = LCD_ByRef; |
| 328 | break; |
| 329 | |
| 330 | case CapturingScopeInfo::ImpCap_Block: |
| 331 | llvm_unreachable("block capture in lambda"); |
| 332 | break; |
| 333 | } |
| 334 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 335 | // Finalize the lambda class. |
| 336 | SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end()); |
| 337 | ActOnFields(0, Class->getLocation(), Class, Fields, |
| 338 | SourceLocation(), SourceLocation(), 0); |
| 339 | CheckCompletedCXXClass(Class); |
| 340 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 341 | // C++ [expr.prim.lambda]p7: |
| 342 | // The lambda-expression's compound-statement yields the |
| 343 | // function-body (8.4) of the function call operator [...]. |
| 344 | ActOnFinishFunctionBody(LSI->CallOperator, Body, /*IsInstantation=*/false); |
| 345 | } |
| 346 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 347 | if (LambdaExprNeedsCleanups) |
| 348 | ExprNeedsCleanups = true; |
| 349 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 350 | Expr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, |
| 351 | CaptureDefault, Captures, ExplicitParams, |
| 352 | CaptureInits, Body->getLocEnd()); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 353 | Diag(StartLoc, diag::err_lambda_unsupported); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 354 | |
| 355 | return MaybeBindToTemporary(Lambda); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 356 | } |