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" |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ScopeInfo.h" |
| 18 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
| 21 | using namespace clang; |
| 22 | using namespace sema; |
| 23 | |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 24 | CXXRecordDecl *Sema::createLambdaClosureType(SourceRange IntroducerRange, |
Eli Friedman | 8da8a66 | 2012-09-19 01:18:11 +0000 | [diff] [blame] | 25 | TypeSourceInfo *Info, |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 26 | bool KnownDependent) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 27 | DeclContext *DC = CurContext; |
| 28 | while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) |
| 29 | DC = DC->getParent(); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 31 | // Start constructing the lambda class. |
Eli Friedman | 8da8a66 | 2012-09-19 01:18:11 +0000 | [diff] [blame] | 32 | CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(Context, DC, Info, |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 33 | IntroducerRange.getBegin(), |
| 34 | KnownDependent); |
Douglas Gregor | fa07ab5 | 2012-02-20 20:47:06 +0000 | [diff] [blame] | 35 | DC->addDecl(Class); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 36 | |
| 37 | return Class; |
| 38 | } |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | f54486a | 2012-04-04 17:40:10 +0000 | [diff] [blame] | 40 | /// \brief Determine whether the given context is or is enclosed in an inline |
| 41 | /// function. |
| 42 | static bool isInInlineFunction(const DeclContext *DC) { |
| 43 | while (!DC->isFileContext()) { |
| 44 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 45 | if (FD->isInlined()) |
| 46 | return true; |
| 47 | |
| 48 | DC = DC->getLexicalParent(); |
| 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 54 | CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, |
Douglas Gregor | f54486a | 2012-04-04 17:40:10 +0000 | [diff] [blame] | 55 | SourceRange IntroducerRange, |
| 56 | TypeSourceInfo *MethodType, |
| 57 | SourceLocation EndLoc, |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 58 | llvm::ArrayRef<ParmVarDecl *> Params) { |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 59 | // C++11 [expr.prim.lambda]p5: |
| 60 | // The closure type for a lambda-expression has a public inline function |
| 61 | // call operator (13.5.4) whose parameters and return type are described by |
| 62 | // the lambda-expression's parameter-declaration-clause and |
| 63 | // trailing-return-type respectively. |
| 64 | DeclarationName MethodName |
| 65 | = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
| 66 | DeclarationNameLoc MethodNameLoc; |
| 67 | MethodNameLoc.CXXOperatorName.BeginOpNameLoc |
| 68 | = IntroducerRange.getBegin().getRawEncoding(); |
| 69 | MethodNameLoc.CXXOperatorName.EndOpNameLoc |
| 70 | = IntroducerRange.getEnd().getRawEncoding(); |
| 71 | CXXMethodDecl *Method |
| 72 | = CXXMethodDecl::Create(Context, Class, EndLoc, |
| 73 | DeclarationNameInfo(MethodName, |
| 74 | IntroducerRange.getBegin(), |
| 75 | MethodNameLoc), |
| 76 | MethodType->getType(), MethodType, |
| 77 | /*isStatic=*/false, |
| 78 | SC_None, |
| 79 | /*isInline=*/true, |
| 80 | /*isConstExpr=*/false, |
| 81 | EndLoc); |
| 82 | Method->setAccess(AS_public); |
| 83 | |
| 84 | // Temporarily set the lexical declaration context to the current |
| 85 | // context, so that the Scope stack matches the lexical nesting. |
Douglas Gregor | fa07ab5 | 2012-02-20 20:47:06 +0000 | [diff] [blame] | 86 | Method->setLexicalDeclContext(CurContext); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 88 | // Add parameters. |
| 89 | if (!Params.empty()) { |
| 90 | Method->setParams(Params); |
| 91 | CheckParmsForFunctionDef(const_cast<ParmVarDecl **>(Params.begin()), |
| 92 | const_cast<ParmVarDecl **>(Params.end()), |
| 93 | /*CheckParameterNames=*/false); |
| 94 | |
| 95 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 96 | PEnd = Method->param_end(); |
| 97 | P != PEnd; ++P) |
| 98 | (*P)->setOwningFunction(Method); |
| 99 | } |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 100 | |
| 101 | // Allocate a mangling number for this lambda expression, if the ABI |
| 102 | // requires one. |
| 103 | Decl *ContextDecl = ExprEvalContexts.back().LambdaContextDecl; |
| 104 | |
| 105 | enum ContextKind { |
| 106 | Normal, |
| 107 | DefaultArgument, |
| 108 | DataMember, |
| 109 | StaticDataMember |
| 110 | } Kind = Normal; |
| 111 | |
| 112 | // Default arguments of member function parameters that appear in a class |
| 113 | // definition, as well as the initializers of data members, receive special |
| 114 | // treatment. Identify them. |
| 115 | if (ContextDecl) { |
| 116 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ContextDecl)) { |
| 117 | if (const DeclContext *LexicalDC |
| 118 | = Param->getDeclContext()->getLexicalParent()) |
| 119 | if (LexicalDC->isRecord()) |
| 120 | Kind = DefaultArgument; |
| 121 | } else if (VarDecl *Var = dyn_cast<VarDecl>(ContextDecl)) { |
| 122 | if (Var->getDeclContext()->isRecord()) |
| 123 | Kind = StaticDataMember; |
| 124 | } else if (isa<FieldDecl>(ContextDecl)) { |
| 125 | Kind = DataMember; |
Douglas Gregor | f54486a | 2012-04-04 17:40:10 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 129 | // Itanium ABI [5.1.7]: |
| 130 | // In the following contexts [...] the one-definition rule requires closure |
| 131 | // types in different translation units to "correspond": |
| 132 | bool IsInNonspecializedTemplate = |
| 133 | !ActiveTemplateInstantiations.empty() || CurContext->isDependentContext(); |
| 134 | unsigned ManglingNumber; |
| 135 | switch (Kind) { |
| 136 | case Normal: |
| 137 | // -- the bodies of non-exported nonspecialized template functions |
| 138 | // -- the bodies of inline functions |
| 139 | if ((IsInNonspecializedTemplate && |
| 140 | !(ContextDecl && isa<ParmVarDecl>(ContextDecl))) || |
| 141 | isInInlineFunction(CurContext)) |
| 142 | ManglingNumber = Context.getLambdaManglingNumber(Method); |
| 143 | else |
| 144 | ManglingNumber = 0; |
| 145 | |
| 146 | // There is no special context for this lambda. |
| 147 | ContextDecl = 0; |
| 148 | break; |
| 149 | |
| 150 | case StaticDataMember: |
| 151 | // -- the initializers of nonspecialized static members of template classes |
| 152 | if (!IsInNonspecializedTemplate) { |
| 153 | ManglingNumber = 0; |
| 154 | ContextDecl = 0; |
| 155 | break; |
| 156 | } |
| 157 | // Fall through to assign a mangling number. |
| 158 | |
| 159 | case DataMember: |
| 160 | // -- the in-class initializers of class members |
| 161 | case DefaultArgument: |
| 162 | // -- default arguments appearing in class definitions |
| 163 | ManglingNumber = ExprEvalContexts.back().getLambdaMangleContext() |
| 164 | .getManglingNumber(Method); |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | Class->setLambdaMangling(ManglingNumber, ContextDecl); |
| 169 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 170 | return Method; |
| 171 | } |
| 172 | |
| 173 | LambdaScopeInfo *Sema::enterLambdaScope(CXXMethodDecl *CallOperator, |
| 174 | SourceRange IntroducerRange, |
| 175 | LambdaCaptureDefault CaptureDefault, |
| 176 | bool ExplicitParams, |
| 177 | bool ExplicitResultType, |
| 178 | bool Mutable) { |
| 179 | PushLambdaScope(CallOperator->getParent(), CallOperator); |
| 180 | LambdaScopeInfo *LSI = getCurLambda(); |
| 181 | if (CaptureDefault == LCD_ByCopy) |
| 182 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval; |
| 183 | else if (CaptureDefault == LCD_ByRef) |
| 184 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref; |
| 185 | LSI->IntroducerRange = IntroducerRange; |
| 186 | LSI->ExplicitParams = ExplicitParams; |
| 187 | LSI->Mutable = Mutable; |
| 188 | |
| 189 | if (ExplicitResultType) { |
| 190 | LSI->ReturnType = CallOperator->getResultType(); |
Douglas Gregor | 53393f2 | 2012-02-14 21:20:44 +0000 | [diff] [blame] | 191 | |
| 192 | if (!LSI->ReturnType->isDependentType() && |
| 193 | !LSI->ReturnType->isVoidType()) { |
| 194 | if (RequireCompleteType(CallOperator->getLocStart(), LSI->ReturnType, |
| 195 | diag::err_lambda_incomplete_result)) { |
| 196 | // Do nothing. |
| 197 | } else if (LSI->ReturnType->isObjCObjectOrInterfaceType()) { |
| 198 | Diag(CallOperator->getLocStart(), diag::err_lambda_objc_object_result) |
| 199 | << LSI->ReturnType; |
| 200 | } |
| 201 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 202 | } else { |
| 203 | LSI->HasImplicitReturnType = true; |
| 204 | } |
| 205 | |
| 206 | return LSI; |
| 207 | } |
| 208 | |
| 209 | void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) { |
| 210 | LSI->finishedExplicitCaptures(); |
| 211 | } |
| 212 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 213 | void Sema::addLambdaParameters(CXXMethodDecl *CallOperator, Scope *CurScope) { |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 214 | // Introduce our parameters into the function scope |
| 215 | for (unsigned p = 0, NumParams = CallOperator->getNumParams(); |
| 216 | p < NumParams; ++p) { |
| 217 | ParmVarDecl *Param = CallOperator->getParamDecl(p); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 218 | |
| 219 | // If this has an identifier, add it to the scope stack. |
| 220 | if (CurScope && Param->getIdentifier()) { |
| 221 | CheckShadow(CurScope, Param); |
| 222 | |
| 223 | PushOnScopeChains(Param, CurScope); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 228 | static bool checkReturnValueType(const ASTContext &Ctx, const Expr *E, |
| 229 | QualType &DeducedType, |
| 230 | QualType &AlternateType) { |
| 231 | // Handle ReturnStmts with no expressions. |
| 232 | if (!E) { |
| 233 | if (AlternateType.isNull()) |
| 234 | AlternateType = Ctx.VoidTy; |
| 235 | |
| 236 | return Ctx.hasSameType(DeducedType, Ctx.VoidTy); |
| 237 | } |
| 238 | |
| 239 | QualType StrictType = E->getType(); |
| 240 | QualType LooseType = StrictType; |
| 241 | |
| 242 | // In C, enum constants have the type of their underlying integer type, |
| 243 | // not the enum. When inferring block return types, we should allow |
| 244 | // the enum type if an enum constant is used, unless the enum is |
| 245 | // anonymous (in which case there can be no variables of its type). |
| 246 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 247 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenImpCasts()); |
| 248 | if (DRE) { |
| 249 | const Decl *D = DRE->getDecl(); |
| 250 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
| 251 | const EnumDecl *Enum = cast<EnumDecl>(ECD->getDeclContext()); |
| 252 | if (Enum->getDeclName() || Enum->getTypedefNameForAnonDecl()) |
| 253 | LooseType = Ctx.getTypeDeclType(Enum); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Special case for the first return statement we find. |
| 259 | // The return type has already been tentatively set, but we might still |
| 260 | // have an alternate type we should prefer. |
| 261 | if (AlternateType.isNull()) |
| 262 | AlternateType = LooseType; |
| 263 | |
| 264 | if (Ctx.hasSameType(DeducedType, StrictType)) { |
| 265 | // FIXME: The loose type is different when there are constants from two |
| 266 | // different enums. We could consider warning here. |
| 267 | if (AlternateType != Ctx.DependentTy) |
| 268 | if (!Ctx.hasSameType(AlternateType, LooseType)) |
| 269 | AlternateType = Ctx.VoidTy; |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | if (Ctx.hasSameType(DeducedType, LooseType)) { |
| 274 | // Use DependentTy to signal that we're using an alternate type and may |
| 275 | // need to add casts somewhere. |
| 276 | AlternateType = Ctx.DependentTy; |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | if (Ctx.hasSameType(AlternateType, StrictType) || |
| 281 | Ctx.hasSameType(AlternateType, LooseType)) { |
| 282 | DeducedType = AlternateType; |
| 283 | // Use DependentTy to signal that we're using an alternate type and may |
| 284 | // need to add casts somewhere. |
| 285 | AlternateType = Ctx.DependentTy; |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | void Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) { |
| 293 | assert(CSI.HasImplicitReturnType); |
| 294 | |
| 295 | // First case: no return statements, implicit void return type. |
| 296 | ASTContext &Ctx = getASTContext(); |
| 297 | if (CSI.Returns.empty()) { |
| 298 | // It's possible there were simply no /valid/ return statements. |
| 299 | // In this case, the first one we found may have at least given us a type. |
| 300 | if (CSI.ReturnType.isNull()) |
| 301 | CSI.ReturnType = Ctx.VoidTy; |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | // Second case: at least one return statement has dependent type. |
| 306 | // Delay type checking until instantiation. |
| 307 | assert(!CSI.ReturnType.isNull() && "We should have a tentative return type."); |
| 308 | if (CSI.ReturnType->isDependentType()) |
| 309 | return; |
| 310 | |
| 311 | // Third case: only one return statement. Don't bother doing extra work! |
| 312 | SmallVectorImpl<ReturnStmt*>::iterator I = CSI.Returns.begin(), |
| 313 | E = CSI.Returns.end(); |
| 314 | if (I+1 == E) |
| 315 | return; |
| 316 | |
| 317 | // General case: many return statements. |
| 318 | // Check that they all have compatible return types. |
| 319 | // For now, that means "identical", with an exception for enum constants. |
| 320 | // (In C, enum constants have the type of their underlying integer type, |
| 321 | // not the type of the enum. C++ uses the type of the enum.) |
| 322 | QualType AlternateType; |
| 323 | |
| 324 | // We require the return types to strictly match here. |
| 325 | for (; I != E; ++I) { |
| 326 | const ReturnStmt *RS = *I; |
| 327 | const Expr *RetE = RS->getRetValue(); |
| 328 | if (!checkReturnValueType(Ctx, RetE, CSI.ReturnType, AlternateType)) { |
| 329 | // FIXME: This is a poor diagnostic for ReturnStmts without expressions. |
| 330 | Diag(RS->getLocStart(), |
| 331 | diag::err_typecheck_missing_return_type_incompatible) |
| 332 | << (RetE ? RetE->getType() : Ctx.VoidTy) << CSI.ReturnType |
| 333 | << isa<LambdaScopeInfo>(CSI); |
| 334 | // Don't bother fixing up the return statements in the block if some of |
| 335 | // them are unfixable anyway. |
| 336 | AlternateType = Ctx.VoidTy; |
| 337 | // Continue iterating so that we keep emitting diagnostics. |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | // If our return statements turned out to be compatible, but we needed to |
| 342 | // pick a different return type, go through and fix the ones that need it. |
| 343 | if (AlternateType == Ctx.DependentTy) { |
| 344 | for (SmallVectorImpl<ReturnStmt*>::iterator I = CSI.Returns.begin(), |
| 345 | E = CSI.Returns.end(); |
| 346 | I != E; ++I) { |
| 347 | ReturnStmt *RS = *I; |
| 348 | Expr *RetE = RS->getRetValue(); |
| 349 | if (RetE->getType() == CSI.ReturnType) |
| 350 | continue; |
| 351 | |
| 352 | // Right now we only support integral fixup casts. |
| 353 | assert(CSI.ReturnType->isIntegralOrUnscopedEnumerationType()); |
| 354 | assert(RetE->getType()->isIntegralOrUnscopedEnumerationType()); |
| 355 | ExprResult Casted = ImpCastExprToType(RetE, CSI.ReturnType, |
| 356 | CK_IntegralCast); |
| 357 | assert(Casted.isUsable()); |
| 358 | RS->setRetValue(Casted.take()); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 363 | void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, |
| 364 | Declarator &ParamInfo, |
| 365 | Scope *CurScope) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 366 | // Determine if we're within a context where we know that the lambda will |
| 367 | // be dependent, because there are template parameters in scope. |
| 368 | bool KnownDependent = false; |
| 369 | if (Scope *TmplScope = CurScope->getTemplateParamParent()) |
| 370 | if (!TmplScope->decl_empty()) |
| 371 | KnownDependent = true; |
| 372 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 373 | // Determine the signature of the call operator. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 374 | TypeSourceInfo *MethodTyInfo; |
| 375 | bool ExplicitParams = true; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 376 | bool ExplicitResultType = true; |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 377 | bool ContainsUnexpandedParameterPack = false; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 378 | SourceLocation EndLoc; |
Richard Smith | 3bc2226 | 2012-08-30 13:13:20 +0000 | [diff] [blame] | 379 | llvm::SmallVector<ParmVarDecl *, 8> Params; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 380 | if (ParamInfo.getNumTypeObjects() == 0) { |
| 381 | // C++11 [expr.prim.lambda]p4: |
| 382 | // If a lambda-expression does not include a lambda-declarator, it is as |
| 383 | // if the lambda-declarator were (). |
| 384 | FunctionProtoType::ExtProtoInfo EPI; |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 385 | EPI.HasTrailingReturn = true; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 386 | EPI.TypeQuals |= DeclSpec::TQ_const; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 387 | QualType MethodTy = Context.getFunctionType(Context.DependentTy, |
| 388 | /*Args=*/0, /*NumArgs=*/0, EPI); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 389 | MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy); |
| 390 | ExplicitParams = false; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 391 | ExplicitResultType = false; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 392 | EndLoc = Intro.Range.getEnd(); |
| 393 | } else { |
| 394 | assert(ParamInfo.isFunctionDeclarator() && |
| 395 | "lambda-declarator is a function"); |
| 396 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo(); |
| 397 | |
| 398 | // C++11 [expr.prim.lambda]p5: |
| 399 | // This function call operator is declared const (9.3.1) if and only if |
| 400 | // the lambda-expression's parameter-declaration-clause is not followed |
| 401 | // by mutable. It is neither virtual nor declared volatile. [...] |
| 402 | if (!FTI.hasMutableQualifier()) |
| 403 | FTI.TypeQuals |= DeclSpec::TQ_const; |
| 404 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 405 | MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 406 | assert(MethodTyInfo && "no type from lambda-declarator"); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 407 | EndLoc = ParamInfo.getSourceRange().getEnd(); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 408 | |
| 409 | ExplicitResultType |
| 410 | = MethodTyInfo->getType()->getAs<FunctionType>()->getResultType() |
| 411 | != Context.DependentTy; |
Richard Smith | 3bc2226 | 2012-08-30 13:13:20 +0000 | [diff] [blame] | 412 | |
Eli Friedman | 7c3c6bc | 2012-09-20 01:40:23 +0000 | [diff] [blame] | 413 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 414 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
| 415 | // Empty arg list, don't push any params. |
| 416 | checkVoidParamDecl(cast<ParmVarDecl>(FTI.ArgInfo[0].Param)); |
| 417 | } else { |
| 418 | Params.reserve(FTI.NumArgs); |
| 419 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 420 | Params.push_back(cast<ParmVarDecl>(FTI.ArgInfo[i].Param)); |
| 421 | } |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 422 | |
| 423 | // Check for unexpanded parameter packs in the method type. |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 424 | if (MethodTyInfo->getType()->containsUnexpandedParameterPack()) |
| 425 | ContainsUnexpandedParameterPack = true; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 426 | } |
Eli Friedman | 8da8a66 | 2012-09-19 01:18:11 +0000 | [diff] [blame] | 427 | |
| 428 | CXXRecordDecl *Class = createLambdaClosureType(Intro.Range, MethodTyInfo, |
| 429 | KnownDependent); |
| 430 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 431 | CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range, |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 432 | MethodTyInfo, EndLoc, Params); |
| 433 | |
| 434 | if (ExplicitParams) |
| 435 | CheckCXXDefaultArguments(Method); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 437 | // Attributes on the lambda apply to the method. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 438 | ProcessDeclAttributes(CurScope, Method, ParamInfo); |
| 439 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 440 | // Introduce the function call operator as the current declaration context. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 441 | PushDeclContext(CurScope, Method); |
| 442 | |
| 443 | // Introduce the lambda scope. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 444 | LambdaScopeInfo *LSI |
| 445 | = enterLambdaScope(Method, Intro.Range, Intro.Default, ExplicitParams, |
| 446 | ExplicitResultType, |
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 447 | !Method->isConst()); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 448 | |
| 449 | // Handle explicit captures. |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 450 | SourceLocation PrevCaptureLoc |
| 451 | = Intro.Default == LCD_None? Intro.Range.getBegin() : Intro.DefaultLoc; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 452 | for (llvm::SmallVector<LambdaCapture, 4>::const_iterator |
| 453 | C = Intro.Captures.begin(), |
| 454 | E = Intro.Captures.end(); |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 455 | C != E; |
| 456 | PrevCaptureLoc = C->Loc, ++C) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 457 | if (C->Kind == LCK_This) { |
| 458 | // C++11 [expr.prim.lambda]p8: |
| 459 | // An identifier or this shall not appear more than once in a |
| 460 | // lambda-capture. |
| 461 | if (LSI->isCXXThisCaptured()) { |
| 462 | Diag(C->Loc, diag::err_capture_more_than_once) |
| 463 | << "'this'" |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 464 | << SourceRange(LSI->getCXXThisCapture().getLocation()) |
| 465 | << FixItHint::CreateRemoval( |
| 466 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 467 | continue; |
| 468 | } |
| 469 | |
| 470 | // C++11 [expr.prim.lambda]p8: |
| 471 | // If a lambda-capture includes a capture-default that is =, the |
| 472 | // lambda-capture shall not contain this [...]. |
| 473 | if (Intro.Default == LCD_ByCopy) { |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 474 | Diag(C->Loc, diag::err_this_capture_with_copy_default) |
| 475 | << FixItHint::CreateRemoval( |
| 476 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 477 | continue; |
| 478 | } |
| 479 | |
| 480 | // C++11 [expr.prim.lambda]p12: |
| 481 | // If this is captured by a local lambda expression, its nearest |
| 482 | // enclosing function shall be a non-static member function. |
| 483 | QualType ThisCaptureType = getCurrentThisType(); |
| 484 | if (ThisCaptureType.isNull()) { |
| 485 | Diag(C->Loc, diag::err_this_capture) << true; |
| 486 | continue; |
| 487 | } |
| 488 | |
| 489 | CheckCXXThisCapture(C->Loc, /*Explicit=*/true); |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | assert(C->Id && "missing identifier for capture"); |
| 494 | |
| 495 | // C++11 [expr.prim.lambda]p8: |
| 496 | // If a lambda-capture includes a capture-default that is &, the |
| 497 | // identifiers in the lambda-capture shall not be preceded by &. |
| 498 | // If a lambda-capture includes a capture-default that is =, [...] |
| 499 | // each identifier it contains shall be preceded by &. |
| 500 | if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) { |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 501 | Diag(C->Loc, diag::err_reference_capture_with_reference_default) |
| 502 | << FixItHint::CreateRemoval( |
| 503 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 504 | continue; |
| 505 | } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) { |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 506 | Diag(C->Loc, diag::err_copy_capture_with_copy_default) |
| 507 | << FixItHint::CreateRemoval( |
| 508 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 509 | continue; |
| 510 | } |
| 511 | |
| 512 | DeclarationNameInfo Name(C->Id, C->Loc); |
| 513 | LookupResult R(*this, Name, LookupOrdinaryName); |
| 514 | LookupName(R, CurScope); |
| 515 | if (R.isAmbiguous()) |
| 516 | continue; |
| 517 | if (R.empty()) { |
| 518 | // FIXME: Disable corrections that would add qualification? |
| 519 | CXXScopeSpec ScopeSpec; |
| 520 | DeclFilterCCC<VarDecl> Validator; |
| 521 | if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator)) |
| 522 | continue; |
| 523 | } |
| 524 | |
| 525 | // C++11 [expr.prim.lambda]p10: |
| 526 | // The identifiers in a capture-list are looked up using the usual rules |
| 527 | // for unqualified name lookup (3.4.1); each such lookup shall find a |
| 528 | // variable with automatic storage duration declared in the reaching |
| 529 | // scope of the local lambda expression. |
Douglas Gregor | 53393f2 | 2012-02-14 21:20:44 +0000 | [diff] [blame] | 530 | // |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 531 | // Note that the 'reaching scope' check happens in tryCaptureVariable(). |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 532 | VarDecl *Var = R.getAsSingle<VarDecl>(); |
| 533 | if (!Var) { |
| 534 | Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id; |
| 535 | continue; |
| 536 | } |
| 537 | |
Eli Friedman | 9cd5b24 | 2012-09-18 21:11:30 +0000 | [diff] [blame] | 538 | // Ignore invalid decls; they'll just confuse the code later. |
| 539 | if (Var->isInvalidDecl()) |
| 540 | continue; |
| 541 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 542 | if (!Var->hasLocalStorage()) { |
| 543 | Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id; |
| 544 | Diag(Var->getLocation(), diag::note_previous_decl) << C->Id; |
| 545 | continue; |
| 546 | } |
| 547 | |
| 548 | // C++11 [expr.prim.lambda]p8: |
| 549 | // An identifier or this shall not appear more than once in a |
| 550 | // lambda-capture. |
| 551 | if (LSI->isCaptured(Var)) { |
| 552 | Diag(C->Loc, diag::err_capture_more_than_once) |
| 553 | << C->Id |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 554 | << SourceRange(LSI->getCapture(Var).getLocation()) |
| 555 | << FixItHint::CreateRemoval( |
| 556 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 557 | continue; |
| 558 | } |
| 559 | |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 560 | // C++11 [expr.prim.lambda]p23: |
| 561 | // A capture followed by an ellipsis is a pack expansion (14.5.3). |
| 562 | SourceLocation EllipsisLoc; |
| 563 | if (C->EllipsisLoc.isValid()) { |
| 564 | if (Var->isParameterPack()) { |
| 565 | EllipsisLoc = C->EllipsisLoc; |
| 566 | } else { |
| 567 | Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 568 | << SourceRange(C->Loc); |
| 569 | |
| 570 | // Just ignore the ellipsis. |
| 571 | } |
| 572 | } else if (Var->isParameterPack()) { |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 573 | ContainsUnexpandedParameterPack = true; |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 576 | TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef : |
| 577 | TryCapture_ExplicitByVal; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 578 | tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 579 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 580 | finishLambdaExplicitCaptures(LSI); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 581 | |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 582 | LSI->ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; |
| 583 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 584 | // Add lambda parameters into scope. |
| 585 | addLambdaParameters(Method, CurScope); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 586 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 587 | // Enter a new evaluation context to insulate the lambda from any |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 588 | // cleanups from the enclosing full-expression. |
| 589 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 592 | void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope, |
| 593 | bool IsInstantiation) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 594 | // Leave the expression-evaluation context. |
| 595 | DiscardCleanupsInEvaluationContext(); |
| 596 | PopExpressionEvaluationContext(); |
| 597 | |
| 598 | // Leave the context of the lambda. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 599 | if (!IsInstantiation) |
| 600 | PopDeclContext(); |
Douglas Gregor | 630d5ff | 2012-02-09 01:28:42 +0000 | [diff] [blame] | 601 | |
| 602 | // Finalize the lambda. |
| 603 | LambdaScopeInfo *LSI = getCurLambda(); |
| 604 | CXXRecordDecl *Class = LSI->Lambda; |
| 605 | Class->setInvalidDecl(); |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 606 | SmallVector<Decl*, 4> Fields; |
| 607 | for (RecordDecl::field_iterator i = Class->field_begin(), |
| 608 | e = Class->field_end(); i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 609 | Fields.push_back(*i); |
Douglas Gregor | 630d5ff | 2012-02-09 01:28:42 +0000 | [diff] [blame] | 610 | ActOnFields(0, Class->getLocation(), Class, Fields, |
| 611 | SourceLocation(), SourceLocation(), 0); |
| 612 | CheckCompletedCXXClass(Class); |
| 613 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 614 | PopFunctionScopeInfo(); |
| 615 | } |
| 616 | |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 617 | /// \brief Add a lambda's conversion to function pointer, as described in |
| 618 | /// C++11 [expr.prim.lambda]p6. |
| 619 | static void addFunctionPointerConversion(Sema &S, |
| 620 | SourceRange IntroducerRange, |
| 621 | CXXRecordDecl *Class, |
| 622 | CXXMethodDecl *CallOperator) { |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 623 | // Add the conversion to function pointer. |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 624 | const FunctionProtoType *Proto |
| 625 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 626 | QualType FunctionPtrTy; |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 627 | QualType FunctionTy; |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 628 | { |
| 629 | FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); |
| 630 | ExtInfo.TypeQuals = 0; |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 631 | FunctionTy = S.Context.getFunctionType(Proto->getResultType(), |
| 632 | Proto->arg_type_begin(), |
| 633 | Proto->getNumArgs(), |
| 634 | ExtInfo); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 635 | FunctionPtrTy = S.Context.getPointerType(FunctionTy); |
| 636 | } |
| 637 | |
| 638 | FunctionProtoType::ExtProtoInfo ExtInfo; |
| 639 | ExtInfo.TypeQuals = Qualifiers::Const; |
| 640 | QualType ConvTy = S.Context.getFunctionType(FunctionPtrTy, 0, 0, ExtInfo); |
| 641 | |
| 642 | SourceLocation Loc = IntroducerRange.getBegin(); |
| 643 | DeclarationName Name |
| 644 | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
| 645 | S.Context.getCanonicalType(FunctionPtrTy)); |
| 646 | DeclarationNameLoc NameLoc; |
| 647 | NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(FunctionPtrTy, |
| 648 | Loc); |
| 649 | CXXConversionDecl *Conversion |
| 650 | = CXXConversionDecl::Create(S.Context, Class, Loc, |
| 651 | DeclarationNameInfo(Name, Loc, NameLoc), |
| 652 | ConvTy, |
| 653 | S.Context.getTrivialTypeSourceInfo(ConvTy, |
| 654 | Loc), |
| 655 | /*isInline=*/false, /*isExplicit=*/false, |
| 656 | /*isConstexpr=*/false, |
| 657 | CallOperator->getBody()->getLocEnd()); |
| 658 | Conversion->setAccess(AS_public); |
| 659 | Conversion->setImplicit(true); |
| 660 | Class->addDecl(Conversion); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 661 | |
| 662 | // Add a non-static member function "__invoke" that will be the result of |
| 663 | // the conversion. |
| 664 | Name = &S.Context.Idents.get("__invoke"); |
| 665 | CXXMethodDecl *Invoke |
| 666 | = CXXMethodDecl::Create(S.Context, Class, Loc, |
| 667 | DeclarationNameInfo(Name, Loc), FunctionTy, |
| 668 | CallOperator->getTypeSourceInfo(), |
| 669 | /*IsStatic=*/true, SC_Static, /*IsInline=*/true, |
| 670 | /*IsConstexpr=*/false, |
| 671 | CallOperator->getBody()->getLocEnd()); |
| 672 | SmallVector<ParmVarDecl *, 4> InvokeParams; |
| 673 | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { |
| 674 | ParmVarDecl *From = CallOperator->getParamDecl(I); |
| 675 | InvokeParams.push_back(ParmVarDecl::Create(S.Context, Invoke, |
| 676 | From->getLocStart(), |
| 677 | From->getLocation(), |
| 678 | From->getIdentifier(), |
| 679 | From->getType(), |
| 680 | From->getTypeSourceInfo(), |
| 681 | From->getStorageClass(), |
| 682 | From->getStorageClassAsWritten(), |
| 683 | /*DefaultArg=*/0)); |
| 684 | } |
| 685 | Invoke->setParams(InvokeParams); |
| 686 | Invoke->setAccess(AS_private); |
| 687 | Invoke->setImplicit(true); |
| 688 | Class->addDecl(Invoke); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 691 | /// \brief Add a lambda's conversion to block pointer. |
| 692 | static void addBlockPointerConversion(Sema &S, |
| 693 | SourceRange IntroducerRange, |
| 694 | CXXRecordDecl *Class, |
| 695 | CXXMethodDecl *CallOperator) { |
| 696 | const FunctionProtoType *Proto |
| 697 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 698 | QualType BlockPtrTy; |
| 699 | { |
| 700 | FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); |
| 701 | ExtInfo.TypeQuals = 0; |
| 702 | QualType FunctionTy |
| 703 | = S.Context.getFunctionType(Proto->getResultType(), |
| 704 | Proto->arg_type_begin(), |
| 705 | Proto->getNumArgs(), |
| 706 | ExtInfo); |
| 707 | BlockPtrTy = S.Context.getBlockPointerType(FunctionTy); |
| 708 | } |
| 709 | |
| 710 | FunctionProtoType::ExtProtoInfo ExtInfo; |
| 711 | ExtInfo.TypeQuals = Qualifiers::Const; |
| 712 | QualType ConvTy = S.Context.getFunctionType(BlockPtrTy, 0, 0, ExtInfo); |
| 713 | |
| 714 | SourceLocation Loc = IntroducerRange.getBegin(); |
| 715 | DeclarationName Name |
| 716 | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
| 717 | S.Context.getCanonicalType(BlockPtrTy)); |
| 718 | DeclarationNameLoc NameLoc; |
| 719 | NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc); |
| 720 | CXXConversionDecl *Conversion |
| 721 | = CXXConversionDecl::Create(S.Context, Class, Loc, |
| 722 | DeclarationNameInfo(Name, Loc, NameLoc), |
| 723 | ConvTy, |
| 724 | S.Context.getTrivialTypeSourceInfo(ConvTy, Loc), |
| 725 | /*isInline=*/false, /*isExplicit=*/false, |
| 726 | /*isConstexpr=*/false, |
| 727 | CallOperator->getBody()->getLocEnd()); |
| 728 | Conversion->setAccess(AS_public); |
| 729 | Conversion->setImplicit(true); |
| 730 | Class->addDecl(Conversion); |
| 731 | } |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 732 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 733 | ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 734 | Scope *CurScope, |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 735 | bool IsInstantiation) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 736 | // Collect information from the lambda scope. |
| 737 | llvm::SmallVector<LambdaExpr::Capture, 4> Captures; |
| 738 | llvm::SmallVector<Expr *, 4> CaptureInits; |
| 739 | LambdaCaptureDefault CaptureDefault; |
| 740 | CXXRecordDecl *Class; |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 741 | CXXMethodDecl *CallOperator; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 742 | SourceRange IntroducerRange; |
| 743 | bool ExplicitParams; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 744 | bool ExplicitResultType; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 745 | bool LambdaExprNeedsCleanups; |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 746 | bool ContainsUnexpandedParameterPack; |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 747 | llvm::SmallVector<VarDecl *, 4> ArrayIndexVars; |
| 748 | llvm::SmallVector<unsigned, 4> ArrayIndexStarts; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 749 | { |
| 750 | LambdaScopeInfo *LSI = getCurLambda(); |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 751 | CallOperator = LSI->CallOperator; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 752 | Class = LSI->Lambda; |
| 753 | IntroducerRange = LSI->IntroducerRange; |
| 754 | ExplicitParams = LSI->ExplicitParams; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 755 | ExplicitResultType = !LSI->HasImplicitReturnType; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 756 | LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups; |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 757 | ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack; |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 758 | ArrayIndexVars.swap(LSI->ArrayIndexVars); |
| 759 | ArrayIndexStarts.swap(LSI->ArrayIndexStarts); |
| 760 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 761 | // Translate captures. |
| 762 | for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) { |
| 763 | LambdaScopeInfo::Capture From = LSI->Captures[I]; |
| 764 | assert(!From.isBlockCapture() && "Cannot capture __block variables"); |
| 765 | bool IsImplicit = I >= LSI->NumExplicitCaptures; |
| 766 | |
| 767 | // Handle 'this' capture. |
| 768 | if (From.isThisCapture()) { |
| 769 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), |
| 770 | IsImplicit, |
| 771 | LCK_This)); |
| 772 | CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(), |
| 773 | getCurrentThisType(), |
| 774 | /*isImplicit=*/true)); |
| 775 | continue; |
| 776 | } |
| 777 | |
| 778 | VarDecl *Var = From.getVariable(); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 779 | LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef; |
| 780 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit, |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 781 | Kind, Var, From.getEllipsisLoc())); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 782 | CaptureInits.push_back(From.getCopyExpr()); |
| 783 | } |
| 784 | |
| 785 | switch (LSI->ImpCaptureStyle) { |
| 786 | case CapturingScopeInfo::ImpCap_None: |
| 787 | CaptureDefault = LCD_None; |
| 788 | break; |
| 789 | |
| 790 | case CapturingScopeInfo::ImpCap_LambdaByval: |
| 791 | CaptureDefault = LCD_ByCopy; |
| 792 | break; |
| 793 | |
| 794 | case CapturingScopeInfo::ImpCap_LambdaByref: |
| 795 | CaptureDefault = LCD_ByRef; |
| 796 | break; |
| 797 | |
| 798 | case CapturingScopeInfo::ImpCap_Block: |
| 799 | llvm_unreachable("block capture in lambda"); |
| 800 | break; |
| 801 | } |
| 802 | |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 803 | // C++11 [expr.prim.lambda]p4: |
| 804 | // If a lambda-expression does not include a |
| 805 | // trailing-return-type, it is as if the trailing-return-type |
| 806 | // denotes the following type: |
| 807 | // FIXME: Assumes current resolution to core issue 975. |
| 808 | if (LSI->HasImplicitReturnType) { |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 809 | deduceClosureReturnType(*LSI); |
| 810 | |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 811 | // - if there are no return statements in the |
| 812 | // compound-statement, or all return statements return |
| 813 | // either an expression of type void or no expression or |
| 814 | // braced-init-list, the type void; |
| 815 | if (LSI->ReturnType.isNull()) { |
| 816 | LSI->ReturnType = Context.VoidTy; |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | // Create a function type with the inferred return type. |
| 820 | const FunctionProtoType *Proto |
| 821 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 822 | QualType FunctionTy |
| 823 | = Context.getFunctionType(LSI->ReturnType, |
| 824 | Proto->arg_type_begin(), |
| 825 | Proto->getNumArgs(), |
| 826 | Proto->getExtProtoInfo()); |
| 827 | CallOperator->setType(FunctionTy); |
| 828 | } |
| 829 | |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 830 | // C++ [expr.prim.lambda]p7: |
| 831 | // The lambda-expression's compound-statement yields the |
| 832 | // function-body (8.4) of the function call operator [...]. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 833 | ActOnFinishFunctionBody(CallOperator, Body, IsInstantiation); |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 834 | CallOperator->setLexicalDeclContext(Class); |
| 835 | Class->addDecl(CallOperator); |
Douglas Gregor | b09ab8c | 2012-02-21 20:05:31 +0000 | [diff] [blame] | 836 | PopExpressionEvaluationContext(); |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 838 | // C++11 [expr.prim.lambda]p6: |
| 839 | // The closure type for a lambda-expression with no lambda-capture |
| 840 | // has a public non-virtual non-explicit const conversion function |
| 841 | // to pointer to function having the same parameter and return |
| 842 | // types as the closure type's function call operator. |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 843 | if (Captures.empty() && CaptureDefault == LCD_None) |
| 844 | addFunctionPointerConversion(*this, IntroducerRange, Class, |
| 845 | CallOperator); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 846 | |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 847 | // Objective-C++: |
| 848 | // The closure type for a lambda-expression has a public non-virtual |
| 849 | // non-explicit const conversion function to a block pointer having the |
| 850 | // same parameter and return types as the closure type's function call |
| 851 | // operator. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 852 | if (getLangOpts().Blocks && getLangOpts().ObjC1) |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 853 | addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator); |
| 854 | |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 855 | // Finalize the lambda class. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 856 | SmallVector<Decl*, 4> Fields; |
| 857 | for (RecordDecl::field_iterator i = Class->field_begin(), |
| 858 | e = Class->field_end(); i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 859 | Fields.push_back(*i); |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 860 | ActOnFields(0, Class->getLocation(), Class, Fields, |
| 861 | SourceLocation(), SourceLocation(), 0); |
| 862 | CheckCompletedCXXClass(Class); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 865 | if (LambdaExprNeedsCleanups) |
| 866 | ExprNeedsCleanups = true; |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 868 | LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, |
| 869 | CaptureDefault, Captures, |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 870 | ExplicitParams, ExplicitResultType, |
| 871 | CaptureInits, ArrayIndexVars, |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 872 | ArrayIndexStarts, Body->getLocEnd(), |
| 873 | ContainsUnexpandedParameterPack); |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 874 | |
| 875 | // C++11 [expr.prim.lambda]p2: |
| 876 | // A lambda-expression shall not appear in an unevaluated operand |
| 877 | // (Clause 5). |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 878 | if (!CurContext->isDependentContext()) { |
| 879 | switch (ExprEvalContexts.back().Context) { |
| 880 | case Unevaluated: |
| 881 | // We don't actually diagnose this case immediately, because we |
| 882 | // could be within a context where we might find out later that |
| 883 | // the expression is potentially evaluated (e.g., for typeid). |
| 884 | ExprEvalContexts.back().Lambdas.push_back(Lambda); |
| 885 | break; |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 887 | case ConstantEvaluated: |
| 888 | case PotentiallyEvaluated: |
| 889 | case PotentiallyEvaluatedIfUsed: |
| 890 | break; |
| 891 | } |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 892 | } |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 894 | return MaybeBindToTemporary(Lambda); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 895 | } |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 896 | |
| 897 | ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation, |
| 898 | SourceLocation ConvLocation, |
| 899 | CXXConversionDecl *Conv, |
| 900 | Expr *Src) { |
| 901 | // Make sure that the lambda call operator is marked used. |
| 902 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 903 | CXXMethodDecl *CallOperator |
| 904 | = cast<CXXMethodDecl>( |
| 905 | *Lambda->lookup( |
| 906 | Context.DeclarationNames.getCXXOperatorName(OO_Call)).first); |
| 907 | CallOperator->setReferenced(); |
| 908 | CallOperator->setUsed(); |
| 909 | |
| 910 | ExprResult Init = PerformCopyInitialization( |
| 911 | InitializedEntity::InitializeBlock(ConvLocation, |
| 912 | Src->getType(), |
| 913 | /*NRVO=*/false), |
| 914 | CurrentLocation, Src); |
| 915 | if (!Init.isInvalid()) |
| 916 | Init = ActOnFinishFullExpr(Init.take()); |
| 917 | |
| 918 | if (Init.isInvalid()) |
| 919 | return ExprError(); |
| 920 | |
| 921 | // Create the new block to be returned. |
| 922 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, ConvLocation); |
| 923 | |
| 924 | // Set the type information. |
| 925 | Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo()); |
| 926 | Block->setIsVariadic(CallOperator->isVariadic()); |
| 927 | Block->setBlockMissingReturnType(false); |
| 928 | |
| 929 | // Add parameters. |
| 930 | SmallVector<ParmVarDecl *, 4> BlockParams; |
| 931 | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { |
| 932 | ParmVarDecl *From = CallOperator->getParamDecl(I); |
| 933 | BlockParams.push_back(ParmVarDecl::Create(Context, Block, |
| 934 | From->getLocStart(), |
| 935 | From->getLocation(), |
| 936 | From->getIdentifier(), |
| 937 | From->getType(), |
| 938 | From->getTypeSourceInfo(), |
| 939 | From->getStorageClass(), |
| 940 | From->getStorageClassAsWritten(), |
| 941 | /*DefaultArg=*/0)); |
| 942 | } |
| 943 | Block->setParams(BlockParams); |
| 944 | |
| 945 | Block->setIsConversionFromLambda(true); |
| 946 | |
| 947 | // Add capture. The capture uses a fake variable, which doesn't correspond |
| 948 | // to any actual memory location. However, the initializer copy-initializes |
| 949 | // the lambda object. |
| 950 | TypeSourceInfo *CapVarTSI = |
| 951 | Context.getTrivialTypeSourceInfo(Src->getType()); |
| 952 | VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation, |
| 953 | ConvLocation, 0, |
| 954 | Src->getType(), CapVarTSI, |
| 955 | SC_None, SC_None); |
| 956 | BlockDecl::Capture Capture(/*Variable=*/CapVar, /*ByRef=*/false, |
| 957 | /*Nested=*/false, /*Copy=*/Init.take()); |
| 958 | Block->setCaptures(Context, &Capture, &Capture + 1, |
| 959 | /*CapturesCXXThis=*/false); |
| 960 | |
| 961 | // Add a fake function body to the block. IR generation is responsible |
| 962 | // for filling in the actual body, which cannot be expressed as an AST. |
Benjamin Kramer | 3a2d0fb | 2012-07-04 17:03:41 +0000 | [diff] [blame] | 963 | Block->setBody(new (Context) CompoundStmt(ConvLocation)); |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 964 | |
| 965 | // Create the block literal expression. |
| 966 | Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType()); |
| 967 | ExprCleanupObjects.push_back(Block); |
| 968 | ExprNeedsCleanups = true; |
| 969 | |
| 970 | return BuildBlock; |
| 971 | } |