diff options
| author | Douglas Gregor <dgregor@apple.com> | 2012-02-15 22:08:38 +0000 |
|---|---|---|
| committer | Douglas Gregor <dgregor@apple.com> | 2012-02-15 22:08:38 +0000 |
| commit | 33e863cfc87105ea15c0d5f7c94380a553d84be7 (patch) | |
| tree | fa3246d83d4dd7c32207f1245a55166a34439799 /clang/lib/Sema | |
| parent | 690a1fb0455a09bc6f47da6df4a0e48b23ebee61 (diff) | |
| download | bcm5719-llvm-33e863cfc87105ea15c0d5f7c94380a553d84be7.tar.gz bcm5719-llvm-33e863cfc87105ea15c0d5f7c94380a553d84be7.zip | |
Lambda closure types have a conversion function to a block pointer
with the same parameter types and return type as the function call
operator. This is the real answer to
http://stackoverflow.com/questions/4148242/is-it-possible-to-convert-a-c0x-lambda-to-a-clang-block
:)
llvm-svn: 150620
Diffstat (limited to 'clang/lib/Sema')
| -rw-r--r-- | clang/lib/Sema/SemaLambda.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/clang/lib/Sema/SemaLambda.cpp b/clang/lib/Sema/SemaLambda.cpp index 9a137e1e796..4e5935701b3 100644 --- a/clang/lib/Sema/SemaLambda.cpp +++ b/clang/lib/Sema/SemaLambda.cpp @@ -411,6 +411,48 @@ static void addFunctionPointerConversion(Sema &S, Class->addDecl(Conversion); } +/// \brief Add a lambda's conversion to block pointer. +static void addBlockPointerConversion(Sema &S, + SourceRange IntroducerRange, + CXXRecordDecl *Class, + CXXMethodDecl *CallOperator) { + const FunctionProtoType *Proto + = CallOperator->getType()->getAs<FunctionProtoType>(); + QualType BlockPtrTy; + { + FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); + ExtInfo.TypeQuals = 0; + QualType FunctionTy + = S.Context.getFunctionType(Proto->getResultType(), + Proto->arg_type_begin(), + Proto->getNumArgs(), + ExtInfo); + BlockPtrTy = S.Context.getBlockPointerType(FunctionTy); + } + + FunctionProtoType::ExtProtoInfo ExtInfo; + ExtInfo.TypeQuals = Qualifiers::Const; + QualType ConvTy = S.Context.getFunctionType(BlockPtrTy, 0, 0, ExtInfo); + + SourceLocation Loc = IntroducerRange.getBegin(); + DeclarationName Name + = S.Context.DeclarationNames.getCXXConversionFunctionName( + S.Context.getCanonicalType(BlockPtrTy)); + DeclarationNameLoc NameLoc; + NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc); + CXXConversionDecl *Conversion + = CXXConversionDecl::Create(S.Context, Class, Loc, + DeclarationNameInfo(Name, Loc, NameLoc), + ConvTy, + S.Context.getTrivialTypeSourceInfo(ConvTy, Loc), + /*isInline=*/false, /*isExplicit=*/false, + /*isConstexpr=*/false, + CallOperator->getBody()->getLocEnd()); + Conversion->setAccess(AS_public); + Conversion->setImplicit(true); + Class->addDecl(Conversion); +} + ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, Scope *CurScope, bool IsInstantiation) { // Leave the expression-evaluation context. @@ -543,6 +585,14 @@ ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, addFunctionPointerConversion(*this, IntroducerRange, Class, CallOperator); + // Objective-C++: + // The closure type for a lambda-expression has a public non-virtual + // non-explicit const conversion function to a block pointer having the + // same parameter and return types as the closure type's function call + // operator. + if (getLangOptions().Blocks) + addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator); + // Finalize the lambda class. SmallVector<Decl*, 4> Fields(Class->field_begin(), Class->field_end()); ActOnFields(0, Class->getLocation(), Class, Fields, |

