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" |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTLambda.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
| 19 | #include "clang/Sema/Lookup.h" |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Scope.h" |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 21 | #include "clang/Sema/ScopeInfo.h" |
| 22 | #include "clang/Sema/SemaInternal.h" |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 23 | #include "TypeLocBuilder.h" |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | using namespace sema; |
| 26 | |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 27 | CXXRecordDecl *Sema::createLambdaClosureType(SourceRange IntroducerRange, |
Eli Friedman | 8da8a66 | 2012-09-19 01:18:11 +0000 | [diff] [blame] | 28 | TypeSourceInfo *Info, |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 29 | bool KnownDependent) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 30 | DeclContext *DC = CurContext; |
| 31 | while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext())) |
| 32 | DC = DC->getParent(); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 33 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 34 | // Start constructing the lambda class. |
Eli Friedman | 8da8a66 | 2012-09-19 01:18:11 +0000 | [diff] [blame] | 35 | CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(Context, DC, Info, |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 36 | IntroducerRange.getBegin(), |
| 37 | KnownDependent); |
Douglas Gregor | fa07ab5 | 2012-02-20 20:47:06 +0000 | [diff] [blame] | 38 | DC->addDecl(Class); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 39 | |
| 40 | return Class; |
| 41 | } |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | f54486a | 2012-04-04 17:40:10 +0000 | [diff] [blame] | 43 | /// \brief Determine whether the given context is or is enclosed in an inline |
| 44 | /// function. |
| 45 | static bool isInInlineFunction(const DeclContext *DC) { |
| 46 | while (!DC->isFileContext()) { |
| 47 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 48 | if (FD->isInlined()) |
| 49 | return true; |
| 50 | |
| 51 | DC = DC->getLexicalParent(); |
| 52 | } |
| 53 | |
| 54 | return false; |
| 55 | } |
| 56 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 57 | MangleNumberingContext * |
Eli Friedman | 5e867c8 | 2013-07-10 00:30:46 +0000 | [diff] [blame] | 58 | Sema::getCurrentMangleNumberContext(const DeclContext *DC, |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 59 | Decl *&ManglingContextDecl) { |
| 60 | // Compute the context for allocating mangling numbers in the current |
| 61 | // expression, if the ABI requires them. |
| 62 | ManglingContextDecl = ExprEvalContexts.back().ManglingContextDecl; |
| 63 | |
| 64 | enum ContextKind { |
| 65 | Normal, |
| 66 | DefaultArgument, |
| 67 | DataMember, |
| 68 | StaticDataMember |
| 69 | } Kind = Normal; |
| 70 | |
| 71 | // Default arguments of member function parameters that appear in a class |
| 72 | // definition, as well as the initializers of data members, receive special |
| 73 | // treatment. Identify them. |
| 74 | if (ManglingContextDecl) { |
| 75 | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) { |
| 76 | if (const DeclContext *LexicalDC |
| 77 | = Param->getDeclContext()->getLexicalParent()) |
| 78 | if (LexicalDC->isRecord()) |
| 79 | Kind = DefaultArgument; |
| 80 | } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) { |
| 81 | if (Var->getDeclContext()->isRecord()) |
| 82 | Kind = StaticDataMember; |
| 83 | } else if (isa<FieldDecl>(ManglingContextDecl)) { |
| 84 | Kind = DataMember; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Itanium ABI [5.1.7]: |
| 89 | // In the following contexts [...] the one-definition rule requires closure |
| 90 | // types in different translation units to "correspond": |
| 91 | bool IsInNonspecializedTemplate = |
| 92 | !ActiveTemplateInstantiations.empty() || CurContext->isDependentContext(); |
| 93 | switch (Kind) { |
| 94 | case Normal: |
| 95 | // -- the bodies of non-exported nonspecialized template functions |
| 96 | // -- the bodies of inline functions |
| 97 | if ((IsInNonspecializedTemplate && |
| 98 | !(ManglingContextDecl && isa<ParmVarDecl>(ManglingContextDecl))) || |
| 99 | isInInlineFunction(CurContext)) { |
| 100 | ManglingContextDecl = 0; |
| 101 | return &Context.getManglingNumberContext(DC); |
| 102 | } |
| 103 | |
| 104 | ManglingContextDecl = 0; |
| 105 | return 0; |
| 106 | |
| 107 | case StaticDataMember: |
| 108 | // -- the initializers of nonspecialized static members of template classes |
| 109 | if (!IsInNonspecializedTemplate) { |
| 110 | ManglingContextDecl = 0; |
| 111 | return 0; |
| 112 | } |
| 113 | // Fall through to get the current context. |
| 114 | |
| 115 | case DataMember: |
| 116 | // -- the in-class initializers of class members |
| 117 | case DefaultArgument: |
| 118 | // -- default arguments appearing in class definitions |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 119 | return &ExprEvalContexts.back().getMangleNumberingContext(Context); |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 120 | } |
Andy Gibbs | ce9cd91 | 2013-07-02 16:01:56 +0000 | [diff] [blame] | 121 | |
| 122 | llvm_unreachable("unexpected context"); |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Reid Kleckner | 942f9fe | 2013-09-10 20:14:30 +0000 | [diff] [blame] | 125 | MangleNumberingContext & |
| 126 | Sema::ExpressionEvaluationContextRecord::getMangleNumberingContext( |
| 127 | ASTContext &Ctx) { |
| 128 | assert(ManglingContextDecl && "Need to have a context declaration"); |
| 129 | if (!MangleNumbering) |
| 130 | MangleNumbering = Ctx.createMangleNumberingContext(); |
| 131 | return *MangleNumbering; |
| 132 | } |
| 133 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 134 | static inline TemplateParameterList * |
| 135 | getGenericLambdaTemplateParameterList(LambdaScopeInfo *LSI, Sema &SemaRef) { |
| 136 | if (LSI->GLTemplateParameterList) |
| 137 | return LSI->GLTemplateParameterList; |
| 138 | else if (LSI->AutoTemplateParams.size()) { |
| 139 | SourceRange IntroRange = LSI->IntroducerRange; |
| 140 | SourceLocation LAngleLoc = IntroRange.getBegin(); |
| 141 | SourceLocation RAngleLoc = IntroRange.getEnd(); |
| 142 | LSI->GLTemplateParameterList = |
| 143 | TemplateParameterList::Create(SemaRef.Context, |
| 144 | /* Template kw loc */ SourceLocation(), |
| 145 | LAngleLoc, |
| 146 | (NamedDecl**)LSI->AutoTemplateParams.data(), |
| 147 | LSI->AutoTemplateParams.size(), RAngleLoc); |
| 148 | } |
| 149 | return LSI->GLTemplateParameterList; |
| 150 | } |
| 151 | |
| 152 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 153 | CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 154 | SourceRange IntroducerRange, |
| 155 | TypeSourceInfo *MethodTypeInfo, |
| 156 | SourceLocation EndLoc, |
| 157 | ArrayRef<ParmVarDecl *> Params) { |
| 158 | QualType MethodType = MethodTypeInfo->getType(); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 159 | TemplateParameterList *TemplateParams = |
| 160 | getGenericLambdaTemplateParameterList(getCurLambda(), *this); |
| 161 | // If a lambda appears in a dependent context or is a generic lambda (has |
| 162 | // template parameters) and has an 'auto' return type, deduce it to a |
| 163 | // dependent type. |
| 164 | if (Class->isDependentContext() || TemplateParams) { |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 165 | const FunctionProtoType *FPT = MethodType->castAs<FunctionProtoType>(); |
| 166 | QualType Result = FPT->getResultType(); |
| 167 | if (Result->isUndeducedType()) { |
| 168 | Result = SubstAutoType(Result, Context.DependentTy); |
| 169 | MethodType = Context.getFunctionType(Result, FPT->getArgTypes(), |
| 170 | FPT->getExtProtoInfo()); |
| 171 | } |
| 172 | } |
| 173 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 174 | // C++11 [expr.prim.lambda]p5: |
| 175 | // The closure type for a lambda-expression has a public inline function |
| 176 | // call operator (13.5.4) whose parameters and return type are described by |
| 177 | // the lambda-expression's parameter-declaration-clause and |
| 178 | // trailing-return-type respectively. |
| 179 | DeclarationName MethodName |
| 180 | = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
| 181 | DeclarationNameLoc MethodNameLoc; |
| 182 | MethodNameLoc.CXXOperatorName.BeginOpNameLoc |
| 183 | = IntroducerRange.getBegin().getRawEncoding(); |
| 184 | MethodNameLoc.CXXOperatorName.EndOpNameLoc |
| 185 | = IntroducerRange.getEnd().getRawEncoding(); |
| 186 | CXXMethodDecl *Method |
| 187 | = CXXMethodDecl::Create(Context, Class, EndLoc, |
| 188 | DeclarationNameInfo(MethodName, |
| 189 | IntroducerRange.getBegin(), |
| 190 | MethodNameLoc), |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 191 | MethodType, MethodTypeInfo, |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 192 | SC_None, |
| 193 | /*isInline=*/true, |
| 194 | /*isConstExpr=*/false, |
| 195 | EndLoc); |
| 196 | Method->setAccess(AS_public); |
| 197 | |
| 198 | // Temporarily set the lexical declaration context to the current |
| 199 | // context, so that the Scope stack matches the lexical nesting. |
Douglas Gregor | fa07ab5 | 2012-02-20 20:47:06 +0000 | [diff] [blame] | 200 | Method->setLexicalDeclContext(CurContext); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 201 | // Create a function template if we have a template parameter list |
| 202 | FunctionTemplateDecl *const TemplateMethod = TemplateParams ? |
| 203 | FunctionTemplateDecl::Create(Context, Class, |
| 204 | Method->getLocation(), MethodName, |
| 205 | TemplateParams, |
| 206 | Method) : 0; |
| 207 | if (TemplateMethod) { |
| 208 | TemplateMethod->setLexicalDeclContext(CurContext); |
| 209 | TemplateMethod->setAccess(AS_public); |
| 210 | Method->setDescribedFunctionTemplate(TemplateMethod); |
| 211 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 213 | // Add parameters. |
| 214 | if (!Params.empty()) { |
| 215 | Method->setParams(Params); |
| 216 | CheckParmsForFunctionDef(const_cast<ParmVarDecl **>(Params.begin()), |
| 217 | const_cast<ParmVarDecl **>(Params.end()), |
| 218 | /*CheckParameterNames=*/false); |
| 219 | |
| 220 | for (CXXMethodDecl::param_iterator P = Method->param_begin(), |
| 221 | PEnd = Method->param_end(); |
| 222 | P != PEnd; ++P) |
| 223 | (*P)->setOwningFunction(Method); |
| 224 | } |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 225 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 226 | Decl *ManglingContextDecl; |
| 227 | if (MangleNumberingContext *MCtx = |
| 228 | getCurrentMangleNumberContext(Class->getDeclContext(), |
| 229 | ManglingContextDecl)) { |
| 230 | unsigned ManglingNumber = MCtx->getManglingNumber(Method); |
| 231 | Class->setLambdaMangling(ManglingNumber, ManglingContextDecl); |
Douglas Gregor | f54486a | 2012-04-04 17:40:10 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 234 | return Method; |
| 235 | } |
| 236 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 237 | void Sema::buildLambdaScope(LambdaScopeInfo *LSI, |
| 238 | CXXMethodDecl *CallOperator, |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 239 | SourceRange IntroducerRange, |
| 240 | LambdaCaptureDefault CaptureDefault, |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 241 | SourceLocation CaptureDefaultLoc, |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 242 | bool ExplicitParams, |
| 243 | bool ExplicitResultType, |
| 244 | bool Mutable) { |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 245 | LSI->CallOperator = CallOperator; |
| 246 | LSI->Lambda = CallOperator->getParent(); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 247 | if (CaptureDefault == LCD_ByCopy) |
| 248 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval; |
| 249 | else if (CaptureDefault == LCD_ByRef) |
| 250 | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref; |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 251 | LSI->CaptureDefaultLoc = CaptureDefaultLoc; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 252 | LSI->IntroducerRange = IntroducerRange; |
| 253 | LSI->ExplicitParams = ExplicitParams; |
| 254 | LSI->Mutable = Mutable; |
| 255 | |
| 256 | if (ExplicitResultType) { |
| 257 | LSI->ReturnType = CallOperator->getResultType(); |
Douglas Gregor | 53393f2 | 2012-02-14 21:20:44 +0000 | [diff] [blame] | 258 | |
| 259 | if (!LSI->ReturnType->isDependentType() && |
| 260 | !LSI->ReturnType->isVoidType()) { |
| 261 | if (RequireCompleteType(CallOperator->getLocStart(), LSI->ReturnType, |
| 262 | diag::err_lambda_incomplete_result)) { |
| 263 | // Do nothing. |
Douglas Gregor | 53393f2 | 2012-02-14 21:20:44 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 266 | } else { |
| 267 | LSI->HasImplicitReturnType = true; |
| 268 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) { |
| 272 | LSI->finishedExplicitCaptures(); |
| 273 | } |
| 274 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 275 | void Sema::addLambdaParameters(CXXMethodDecl *CallOperator, Scope *CurScope) { |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 276 | // Introduce our parameters into the function scope |
| 277 | for (unsigned p = 0, NumParams = CallOperator->getNumParams(); |
| 278 | p < NumParams; ++p) { |
| 279 | ParmVarDecl *Param = CallOperator->getParamDecl(p); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 280 | |
| 281 | // If this has an identifier, add it to the scope stack. |
| 282 | if (CurScope && Param->getIdentifier()) { |
| 283 | CheckShadow(CurScope, Param); |
| 284 | |
| 285 | PushOnScopeChains(Param, CurScope); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 290 | /// If this expression is an enumerator-like expression of some type |
| 291 | /// T, return the type T; otherwise, return null. |
| 292 | /// |
| 293 | /// Pointer comparisons on the result here should always work because |
| 294 | /// it's derived from either the parent of an EnumConstantDecl |
| 295 | /// (i.e. the definition) or the declaration returned by |
| 296 | /// EnumType::getDecl() (i.e. the definition). |
| 297 | static EnumDecl *findEnumForBlockReturn(Expr *E) { |
| 298 | // An expression is an enumerator-like expression of type T if, |
| 299 | // ignoring parens and parens-like expressions: |
| 300 | E = E->IgnoreParens(); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 301 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 302 | // - it is an enumerator whose enum type is T or |
| 303 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 304 | if (EnumConstantDecl *D |
| 305 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
| 306 | return cast<EnumDecl>(D->getDeclContext()); |
| 307 | } |
| 308 | return 0; |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 309 | } |
| 310 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 311 | // - it is a comma expression whose RHS is an enumerator-like |
| 312 | // expression of type T or |
| 313 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 314 | if (BO->getOpcode() == BO_Comma) |
| 315 | return findEnumForBlockReturn(BO->getRHS()); |
| 316 | return 0; |
| 317 | } |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 318 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 319 | // - it is a statement-expression whose value expression is an |
| 320 | // enumerator-like expression of type T or |
| 321 | if (StmtExpr *SE = dyn_cast<StmtExpr>(E)) { |
| 322 | if (Expr *last = dyn_cast_or_null<Expr>(SE->getSubStmt()->body_back())) |
| 323 | return findEnumForBlockReturn(last); |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | // - it is a ternary conditional operator (not the GNU ?: |
| 328 | // extension) whose second and third operands are |
| 329 | // enumerator-like expressions of type T or |
| 330 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
| 331 | if (EnumDecl *ED = findEnumForBlockReturn(CO->getTrueExpr())) |
| 332 | if (ED == findEnumForBlockReturn(CO->getFalseExpr())) |
| 333 | return ED; |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | // (implicitly:) |
| 338 | // - it is an implicit integral conversion applied to an |
| 339 | // enumerator-like expression of type T or |
| 340 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 70133b5 | 2013-05-08 03:34:22 +0000 | [diff] [blame] | 341 | // We can sometimes see integral conversions in valid |
| 342 | // enumerator-like expressions. |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 343 | if (ICE->getCastKind() == CK_IntegralCast) |
| 344 | return findEnumForBlockReturn(ICE->getSubExpr()); |
John McCall | 70133b5 | 2013-05-08 03:34:22 +0000 | [diff] [blame] | 345 | |
| 346 | // Otherwise, just rely on the type. |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | // - it is an expression of that formal enum type. |
| 350 | if (const EnumType *ET = E->getType()->getAs<EnumType>()) { |
| 351 | return ET->getDecl(); |
| 352 | } |
| 353 | |
| 354 | // Otherwise, nope. |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /// Attempt to find a type T for which the returned expression of the |
| 359 | /// given statement is an enumerator-like expression of that type. |
| 360 | static EnumDecl *findEnumForBlockReturn(ReturnStmt *ret) { |
| 361 | if (Expr *retValue = ret->getRetValue()) |
| 362 | return findEnumForBlockReturn(retValue); |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /// Attempt to find a common type T for which all of the returned |
| 367 | /// expressions in a block are enumerator-like expressions of that |
| 368 | /// type. |
| 369 | static EnumDecl *findCommonEnumForBlockReturns(ArrayRef<ReturnStmt*> returns) { |
| 370 | ArrayRef<ReturnStmt*>::iterator i = returns.begin(), e = returns.end(); |
| 371 | |
| 372 | // Try to find one for the first return. |
| 373 | EnumDecl *ED = findEnumForBlockReturn(*i); |
| 374 | if (!ED) return 0; |
| 375 | |
| 376 | // Check that the rest of the returns have the same enum. |
| 377 | for (++i; i != e; ++i) { |
| 378 | if (findEnumForBlockReturn(*i) != ED) |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | // Never infer an anonymous enum type. |
| 383 | if (!ED->hasNameForLinkage()) return 0; |
| 384 | |
| 385 | return ED; |
| 386 | } |
| 387 | |
| 388 | /// Adjust the given return statements so that they formally return |
| 389 | /// the given type. It should require, at most, an IntegralCast. |
| 390 | static void adjustBlockReturnsToEnum(Sema &S, ArrayRef<ReturnStmt*> returns, |
| 391 | QualType returnType) { |
| 392 | for (ArrayRef<ReturnStmt*>::iterator |
| 393 | i = returns.begin(), e = returns.end(); i != e; ++i) { |
| 394 | ReturnStmt *ret = *i; |
| 395 | Expr *retValue = ret->getRetValue(); |
| 396 | if (S.Context.hasSameType(retValue->getType(), returnType)) |
| 397 | continue; |
| 398 | |
| 399 | // Right now we only support integral fixup casts. |
| 400 | assert(returnType->isIntegralOrUnscopedEnumerationType()); |
| 401 | assert(retValue->getType()->isIntegralOrUnscopedEnumerationType()); |
| 402 | |
| 403 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(retValue); |
| 404 | |
| 405 | Expr *E = (cleanups ? cleanups->getSubExpr() : retValue); |
| 406 | E = ImplicitCastExpr::Create(S.Context, returnType, CK_IntegralCast, |
| 407 | E, /*base path*/ 0, VK_RValue); |
| 408 | if (cleanups) { |
| 409 | cleanups->setSubExpr(E); |
| 410 | } else { |
| 411 | ret->setRetValue(E); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 412 | } |
| 413 | } |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) { |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 417 | assert(CSI.HasImplicitReturnType); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 418 | // If it was ever a placeholder, it had to been deduced to DependentTy. |
| 419 | assert(CSI.ReturnType.isNull() || !CSI.ReturnType->isUndeducedType()); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 420 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 421 | // C++ Core Issue #975, proposed resolution: |
| 422 | // If a lambda-expression does not include a trailing-return-type, |
| 423 | // it is as if the trailing-return-type denotes the following type: |
| 424 | // - if there are no return statements in the compound-statement, |
| 425 | // or all return statements return either an expression of type |
| 426 | // void or no expression or braced-init-list, the type void; |
| 427 | // - otherwise, if all return statements return an expression |
| 428 | // and the types of the returned expressions after |
| 429 | // lvalue-to-rvalue conversion (4.1 [conv.lval]), |
| 430 | // array-to-pointer conversion (4.2 [conv.array]), and |
| 431 | // function-to-pointer conversion (4.3 [conv.func]) are the |
| 432 | // same, that common type; |
| 433 | // - otherwise, the program is ill-formed. |
| 434 | // |
| 435 | // In addition, in blocks in non-C++ modes, if all of the return |
| 436 | // statements are enumerator-like expressions of some type T, where |
| 437 | // T has a name for linkage, then we infer the return type of the |
| 438 | // block to be that type. |
| 439 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 440 | // First case: no return statements, implicit void return type. |
| 441 | ASTContext &Ctx = getASTContext(); |
| 442 | if (CSI.Returns.empty()) { |
| 443 | // It's possible there were simply no /valid/ return statements. |
| 444 | // In this case, the first one we found may have at least given us a type. |
| 445 | if (CSI.ReturnType.isNull()) |
| 446 | CSI.ReturnType = Ctx.VoidTy; |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | // Second case: at least one return statement has dependent type. |
| 451 | // Delay type checking until instantiation. |
| 452 | assert(!CSI.ReturnType.isNull() && "We should have a tentative return type."); |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 453 | if (CSI.ReturnType->isDependentType()) |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 454 | return; |
| 455 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 456 | // Try to apply the enum-fuzz rule. |
| 457 | if (!getLangOpts().CPlusPlus) { |
| 458 | assert(isa<BlockScopeInfo>(CSI)); |
| 459 | const EnumDecl *ED = findCommonEnumForBlockReturns(CSI.Returns); |
| 460 | if (ED) { |
| 461 | CSI.ReturnType = Context.getTypeDeclType(ED); |
| 462 | adjustBlockReturnsToEnum(*this, CSI.Returns, CSI.ReturnType); |
| 463 | return; |
| 464 | } |
| 465 | } |
| 466 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 467 | // Third case: only one return statement. Don't bother doing extra work! |
| 468 | SmallVectorImpl<ReturnStmt*>::iterator I = CSI.Returns.begin(), |
| 469 | E = CSI.Returns.end(); |
| 470 | if (I+1 == E) |
| 471 | return; |
| 472 | |
| 473 | // General case: many return statements. |
| 474 | // Check that they all have compatible return types. |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 475 | |
| 476 | // We require the return types to strictly match here. |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 477 | // Note that we've already done the required promotions as part of |
| 478 | // processing the return statement. |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 479 | for (; I != E; ++I) { |
| 480 | const ReturnStmt *RS = *I; |
| 481 | const Expr *RetE = RS->getRetValue(); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 482 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 483 | QualType ReturnType = (RetE ? RetE->getType() : Context.VoidTy); |
| 484 | if (Context.hasSameType(ReturnType, CSI.ReturnType)) |
| 485 | continue; |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 486 | |
John McCall | 41d0164 | 2013-03-09 00:54:31 +0000 | [diff] [blame] | 487 | // FIXME: This is a poor diagnostic for ReturnStmts without expressions. |
| 488 | // TODO: It's possible that the *first* return is the divergent one. |
| 489 | Diag(RS->getLocStart(), |
| 490 | diag::err_typecheck_missing_return_type_incompatible) |
| 491 | << ReturnType << CSI.ReturnType |
| 492 | << isa<LambdaScopeInfo>(CSI); |
| 493 | // Continue iterating so that we keep emitting diagnostics. |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 497 | VarDecl *Sema::checkInitCapture(SourceLocation Loc, bool ByRef, |
| 498 | IdentifierInfo *Id, Expr *Init) { |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 499 | // C++1y [expr.prim.lambda]p11: |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 500 | // An init-capture behaves as if it declares and explicitly captures |
| 501 | // a variable of the form |
| 502 | // "auto init-capture;" |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 503 | QualType DeductType = Context.getAutoDeductType(); |
| 504 | TypeLocBuilder TLB; |
| 505 | TLB.pushTypeSpec(DeductType).setNameLoc(Loc); |
| 506 | if (ByRef) { |
| 507 | DeductType = BuildReferenceType(DeductType, true, Loc, Id); |
| 508 | assert(!DeductType.isNull() && "can't build reference to auto"); |
| 509 | TLB.push<ReferenceTypeLoc>(DeductType).setSigilLoc(Loc); |
| 510 | } |
Eli Friedman | 44ee0a7 | 2013-06-07 20:31:48 +0000 | [diff] [blame] | 511 | TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType); |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 512 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 513 | // Create a dummy variable representing the init-capture. This is not actually |
| 514 | // used as a variable, and only exists as a way to name and refer to the |
| 515 | // init-capture. |
| 516 | // FIXME: Pass in separate source locations for '&' and identifier. |
Richard Smith | 39edfeb | 2013-09-28 04:31:26 +0000 | [diff] [blame] | 517 | VarDecl *NewVD = VarDecl::Create(Context, CurContext, Loc, |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 518 | Loc, Id, TSI->getType(), TSI, SC_Auto); |
| 519 | NewVD->setInitCapture(true); |
| 520 | NewVD->setReferenced(true); |
| 521 | NewVD->markUsed(Context); |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 522 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 523 | // We do not need to distinguish between direct-list-initialization |
| 524 | // and copy-list-initialization here, because we will always deduce |
| 525 | // std::initializer_list<T>, and direct- and copy-list-initialization |
| 526 | // always behave the same for such a type. |
| 527 | // FIXME: We should model whether an '=' was present. |
| 528 | bool DirectInit = isa<ParenListExpr>(Init) || isa<InitListExpr>(Init); |
| 529 | AddInitializerToDecl(NewVD, Init, DirectInit, /*ContainsAuto*/true); |
| 530 | return NewVD; |
| 531 | } |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 532 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 533 | FieldDecl *Sema::buildInitCaptureField(LambdaScopeInfo *LSI, VarDecl *Var) { |
| 534 | FieldDecl *Field = FieldDecl::Create( |
| 535 | Context, LSI->Lambda, Var->getLocation(), Var->getLocation(), |
| 536 | 0, Var->getType(), Var->getTypeSourceInfo(), 0, false, ICIS_NoInit); |
| 537 | Field->setImplicit(true); |
| 538 | Field->setAccess(AS_private); |
| 539 | LSI->Lambda->addDecl(Field); |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 540 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 541 | LSI->addCapture(Var, /*isBlock*/false, Var->getType()->isReferenceType(), |
| 542 | /*isNested*/false, Var->getLocation(), SourceLocation(), |
| 543 | Var->getType(), Var->getInit()); |
| 544 | return Field; |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 547 | void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 548 | Declarator &ParamInfo, Scope *CurScope) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 549 | // Determine if we're within a context where we know that the lambda will |
| 550 | // be dependent, because there are template parameters in scope. |
| 551 | bool KnownDependent = false; |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 552 | LambdaScopeInfo *const LSI = getCurLambda(); |
| 553 | assert(LSI && "LambdaScopeInfo should be on stack!"); |
| 554 | TemplateParameterList *TemplateParams = |
| 555 | getGenericLambdaTemplateParameterList(LSI, *this); |
| 556 | |
| 557 | if (Scope *TmplScope = CurScope->getTemplateParamParent()) { |
| 558 | // Since we have our own TemplateParams, so check if an outer scope |
| 559 | // has template params, only then are we in a dependent scope. |
| 560 | if (TemplateParams) { |
| 561 | TmplScope = TmplScope->getParent(); |
| 562 | TmplScope = TmplScope ? TmplScope->getTemplateParamParent() : 0; |
| 563 | } |
| 564 | if (TmplScope && !TmplScope->decl_empty()) |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 565 | KnownDependent = true; |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 566 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 567 | // Determine the signature of the call operator. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 568 | TypeSourceInfo *MethodTyInfo; |
| 569 | bool ExplicitParams = true; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 570 | bool ExplicitResultType = true; |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 571 | bool ContainsUnexpandedParameterPack = false; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 572 | SourceLocation EndLoc; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 573 | SmallVector<ParmVarDecl *, 8> Params; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 574 | if (ParamInfo.getNumTypeObjects() == 0) { |
| 575 | // C++11 [expr.prim.lambda]p4: |
| 576 | // If a lambda-expression does not include a lambda-declarator, it is as |
| 577 | // if the lambda-declarator were (). |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 578 | FunctionProtoType::ExtProtoInfo EPI(Context.getDefaultCallingConvention( |
| 579 | /*IsVariadic=*/false, /*IsCXXMethod=*/true)); |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 580 | EPI.HasTrailingReturn = true; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 581 | EPI.TypeQuals |= DeclSpec::TQ_const; |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 582 | // C++1y [expr.prim.lambda]: |
| 583 | // The lambda return type is 'auto', which is replaced by the |
| 584 | // trailing-return type if provided and/or deduced from 'return' |
| 585 | // statements |
| 586 | // We don't do this before C++1y, because we don't support deduced return |
| 587 | // types there. |
| 588 | QualType DefaultTypeForNoTrailingReturn = |
| 589 | getLangOpts().CPlusPlus1y ? Context.getAutoDeductType() |
| 590 | : Context.DependentTy; |
| 591 | QualType MethodTy = |
| 592 | Context.getFunctionType(DefaultTypeForNoTrailingReturn, None, EPI); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 593 | MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy); |
| 594 | ExplicitParams = false; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 595 | ExplicitResultType = false; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 596 | EndLoc = Intro.Range.getEnd(); |
| 597 | } else { |
| 598 | assert(ParamInfo.isFunctionDeclarator() && |
| 599 | "lambda-declarator is a function"); |
| 600 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo(); |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 602 | // C++11 [expr.prim.lambda]p5: |
| 603 | // This function call operator is declared const (9.3.1) if and only if |
| 604 | // the lambda-expression's parameter-declaration-clause is not followed |
| 605 | // by mutable. It is neither virtual nor declared volatile. [...] |
| 606 | if (!FTI.hasMutableQualifier()) |
| 607 | FTI.TypeQuals |= DeclSpec::TQ_const; |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 608 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 609 | MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 610 | assert(MethodTyInfo && "no type from lambda-declarator"); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 611 | EndLoc = ParamInfo.getSourceRange().getEnd(); |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 612 | |
| 613 | ExplicitResultType = FTI.hasTrailingReturnType(); |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 614 | |
Eli Friedman | 7c3c6bc | 2012-09-20 01:40:23 +0000 | [diff] [blame] | 615 | if (FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
| 616 | cast<ParmVarDecl>(FTI.ArgInfo[0].Param)->getType()->isVoidType()) { |
| 617 | // Empty arg list, don't push any params. |
| 618 | checkVoidParamDecl(cast<ParmVarDecl>(FTI.ArgInfo[0].Param)); |
| 619 | } else { |
| 620 | Params.reserve(FTI.NumArgs); |
| 621 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
| 622 | Params.push_back(cast<ParmVarDecl>(FTI.ArgInfo[i].Param)); |
| 623 | } |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 624 | |
| 625 | // Check for unexpanded parameter packs in the method type. |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 626 | if (MethodTyInfo->getType()->containsUnexpandedParameterPack()) |
| 627 | ContainsUnexpandedParameterPack = true; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 628 | } |
Eli Friedman | 8da8a66 | 2012-09-19 01:18:11 +0000 | [diff] [blame] | 629 | |
| 630 | CXXRecordDecl *Class = createLambdaClosureType(Intro.Range, MethodTyInfo, |
| 631 | KnownDependent); |
| 632 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 633 | CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range, |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 634 | MethodTyInfo, EndLoc, Params); |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 635 | if (ExplicitParams) |
| 636 | CheckCXXDefaultArguments(Method); |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 637 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 638 | // Attributes on the lambda apply to the method. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 639 | ProcessDeclAttributes(CurScope, Method, ParamInfo); |
| 640 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 641 | // Introduce the function call operator as the current declaration context. |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 642 | PushDeclContext(CurScope, Method); |
| 643 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 644 | // Build the lambda scope. |
| 645 | buildLambdaScope(LSI, Method, |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 646 | Intro.Range, |
| 647 | Intro.Default, Intro.DefaultLoc, |
| 648 | ExplicitParams, |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 649 | ExplicitResultType, |
David Blaikie | 4ef832f | 2012-08-10 00:55:35 +0000 | [diff] [blame] | 650 | !Method->isConst()); |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 651 | |
| 652 | // Distinct capture names, for diagnostics. |
| 653 | llvm::SmallSet<IdentifierInfo*, 8> CaptureNames; |
| 654 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 655 | // Handle explicit captures. |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 656 | SourceLocation PrevCaptureLoc |
| 657 | = Intro.Default == LCD_None? Intro.Range.getBegin() : Intro.DefaultLoc; |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 658 | for (SmallVectorImpl<LambdaCapture>::const_iterator |
| 659 | C = Intro.Captures.begin(), |
| 660 | E = Intro.Captures.end(); |
| 661 | C != E; |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 662 | PrevCaptureLoc = C->Loc, ++C) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 663 | if (C->Kind == LCK_This) { |
| 664 | // C++11 [expr.prim.lambda]p8: |
| 665 | // An identifier or this shall not appear more than once in a |
| 666 | // lambda-capture. |
| 667 | if (LSI->isCXXThisCaptured()) { |
| 668 | Diag(C->Loc, diag::err_capture_more_than_once) |
| 669 | << "'this'" |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 670 | << SourceRange(LSI->getCXXThisCapture().getLocation()) |
| 671 | << FixItHint::CreateRemoval( |
| 672 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 673 | continue; |
| 674 | } |
| 675 | |
| 676 | // C++11 [expr.prim.lambda]p8: |
| 677 | // If a lambda-capture includes a capture-default that is =, the |
| 678 | // lambda-capture shall not contain this [...]. |
| 679 | if (Intro.Default == LCD_ByCopy) { |
Douglas Gregor | 3ac109c | 2012-02-10 17:46:20 +0000 | [diff] [blame] | 680 | Diag(C->Loc, diag::err_this_capture_with_copy_default) |
| 681 | << FixItHint::CreateRemoval( |
| 682 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 683 | continue; |
| 684 | } |
| 685 | |
| 686 | // C++11 [expr.prim.lambda]p12: |
| 687 | // If this is captured by a local lambda expression, its nearest |
| 688 | // enclosing function shall be a non-static member function. |
| 689 | QualType ThisCaptureType = getCurrentThisType(); |
| 690 | if (ThisCaptureType.isNull()) { |
| 691 | Diag(C->Loc, diag::err_this_capture) << true; |
| 692 | continue; |
| 693 | } |
| 694 | |
| 695 | CheckCXXThisCapture(C->Loc, /*Explicit=*/true); |
| 696 | continue; |
| 697 | } |
| 698 | |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 699 | assert(C->Id && "missing identifier for capture"); |
| 700 | |
Richard Smith | 0a664b8 | 2013-05-09 21:36:41 +0000 | [diff] [blame] | 701 | if (C->Init.isInvalid()) |
| 702 | continue; |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 703 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 704 | VarDecl *Var; |
| 705 | if (C->Init.isUsable()) { |
Richard Smith | 9beaf20 | 2013-09-28 05:38:27 +0000 | [diff] [blame^] | 706 | Diag(C->Loc, getLangOpts().CPlusPlus1y |
| 707 | ? diag::warn_cxx11_compat_init_capture |
| 708 | : diag::ext_init_capture); |
| 709 | |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 710 | if (C->Init.get()->containsUnexpandedParameterPack()) |
| 711 | ContainsUnexpandedParameterPack = true; |
| 712 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 713 | Var = checkInitCapture(C->Loc, C->Kind == LCK_ByRef, |
| 714 | C->Id, C->Init.take()); |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 715 | // C++1y [expr.prim.lambda]p11: |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 716 | // An init-capture behaves as if it declares and explicitly |
| 717 | // captures a variable [...] whose declarative region is the |
| 718 | // lambda-expression's compound-statement |
| 719 | if (Var) |
| 720 | PushOnScopeChains(Var, CurScope, false); |
| 721 | } else { |
| 722 | // C++11 [expr.prim.lambda]p8: |
| 723 | // If a lambda-capture includes a capture-default that is &, the |
| 724 | // identifiers in the lambda-capture shall not be preceded by &. |
| 725 | // If a lambda-capture includes a capture-default that is =, [...] |
| 726 | // each identifier it contains shall be preceded by &. |
| 727 | if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) { |
| 728 | Diag(C->Loc, diag::err_reference_capture_with_reference_default) |
| 729 | << FixItHint::CreateRemoval( |
| 730 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 731 | continue; |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 732 | } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) { |
| 733 | Diag(C->Loc, diag::err_copy_capture_with_copy_default) |
| 734 | << FixItHint::CreateRemoval( |
| 735 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
| 736 | continue; |
| 737 | } |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 738 | |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 739 | // C++11 [expr.prim.lambda]p10: |
| 740 | // The identifiers in a capture-list are looked up using the usual |
| 741 | // rules for unqualified name lookup (3.4.1) |
| 742 | DeclarationNameInfo Name(C->Id, C->Loc); |
| 743 | LookupResult R(*this, Name, LookupOrdinaryName); |
| 744 | LookupName(R, CurScope); |
| 745 | if (R.isAmbiguous()) |
| 746 | continue; |
| 747 | if (R.empty()) { |
| 748 | // FIXME: Disable corrections that would add qualification? |
| 749 | CXXScopeSpec ScopeSpec; |
| 750 | DeclFilterCCC<VarDecl> Validator; |
| 751 | if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator)) |
| 752 | continue; |
| 753 | } |
| 754 | |
| 755 | Var = R.getAsSingle<VarDecl>(); |
| 756 | } |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 757 | |
| 758 | // C++11 [expr.prim.lambda]p8: |
| 759 | // An identifier or this shall not appear more than once in a |
| 760 | // lambda-capture. |
| 761 | if (!CaptureNames.insert(C->Id)) { |
| 762 | if (Var && LSI->isCaptured(Var)) { |
| 763 | Diag(C->Loc, diag::err_capture_more_than_once) |
| 764 | << C->Id << SourceRange(LSI->getCapture(Var).getLocation()) |
| 765 | << FixItHint::CreateRemoval( |
| 766 | SourceRange(PP.getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
| 767 | } else |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 768 | // Previous capture captured something different (one or both was |
| 769 | // an init-cpature): no fixit. |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 770 | Diag(C->Loc, diag::err_capture_more_than_once) << C->Id; |
| 771 | continue; |
| 772 | } |
| 773 | |
| 774 | // C++11 [expr.prim.lambda]p10: |
| 775 | // [...] each such lookup shall find a variable with automatic storage |
| 776 | // duration declared in the reaching scope of the local lambda expression. |
| 777 | // Note that the 'reaching scope' check happens in tryCaptureVariable(). |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 778 | if (!Var) { |
| 779 | Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id; |
| 780 | continue; |
| 781 | } |
| 782 | |
Eli Friedman | 9cd5b24 | 2012-09-18 21:11:30 +0000 | [diff] [blame] | 783 | // Ignore invalid decls; they'll just confuse the code later. |
| 784 | if (Var->isInvalidDecl()) |
| 785 | continue; |
| 786 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 787 | if (!Var->hasLocalStorage()) { |
| 788 | Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id; |
| 789 | Diag(Var->getLocation(), diag::note_previous_decl) << C->Id; |
| 790 | continue; |
| 791 | } |
| 792 | |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 793 | // C++11 [expr.prim.lambda]p23: |
| 794 | // A capture followed by an ellipsis is a pack expansion (14.5.3). |
| 795 | SourceLocation EllipsisLoc; |
| 796 | if (C->EllipsisLoc.isValid()) { |
| 797 | if (Var->isParameterPack()) { |
| 798 | EllipsisLoc = C->EllipsisLoc; |
| 799 | } else { |
| 800 | Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
| 801 | << SourceRange(C->Loc); |
| 802 | |
| 803 | // Just ignore the ellipsis. |
| 804 | } |
| 805 | } else if (Var->isParameterPack()) { |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 806 | ContainsUnexpandedParameterPack = true; |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 807 | } |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 808 | |
| 809 | if (C->Init.isUsable()) { |
| 810 | buildInitCaptureField(LSI, Var); |
| 811 | } else { |
| 812 | TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef : |
| 813 | TryCapture_ExplicitByVal; |
| 814 | tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc); |
| 815 | } |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 816 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 817 | finishLambdaExplicitCaptures(LSI); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 818 | |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 819 | LSI->ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack; |
| 820 | |
Douglas Gregor | c6889e7 | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 821 | // Add lambda parameters into scope. |
| 822 | addLambdaParameters(Method, CurScope); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 824 | // Enter a new evaluation context to insulate the lambda from any |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 825 | // cleanups from the enclosing full-expression. |
| 826 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 829 | void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope, |
| 830 | bool IsInstantiation) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 831 | // Leave the expression-evaluation context. |
| 832 | DiscardCleanupsInEvaluationContext(); |
| 833 | PopExpressionEvaluationContext(); |
| 834 | |
| 835 | // Leave the context of the lambda. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 836 | if (!IsInstantiation) |
| 837 | PopDeclContext(); |
Douglas Gregor | 630d5ff | 2012-02-09 01:28:42 +0000 | [diff] [blame] | 838 | |
| 839 | // Finalize the lambda. |
| 840 | LambdaScopeInfo *LSI = getCurLambda(); |
| 841 | CXXRecordDecl *Class = LSI->Lambda; |
| 842 | Class->setInvalidDecl(); |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 843 | SmallVector<Decl*, 4> Fields; |
| 844 | for (RecordDecl::field_iterator i = Class->field_begin(), |
| 845 | e = Class->field_end(); i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 846 | Fields.push_back(*i); |
Douglas Gregor | 630d5ff | 2012-02-09 01:28:42 +0000 | [diff] [blame] | 847 | ActOnFields(0, Class->getLocation(), Class, Fields, |
| 848 | SourceLocation(), SourceLocation(), 0); |
| 849 | CheckCompletedCXXClass(Class); |
| 850 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 851 | PopFunctionScopeInfo(); |
| 852 | } |
| 853 | |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 854 | /// \brief Add a lambda's conversion to function pointer, as described in |
| 855 | /// C++11 [expr.prim.lambda]p6. |
| 856 | static void addFunctionPointerConversion(Sema &S, |
| 857 | SourceRange IntroducerRange, |
| 858 | CXXRecordDecl *Class, |
| 859 | CXXMethodDecl *CallOperator) { |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 860 | // FIXME: The conversion operator needs to be fixed for generic lambdas. |
| 861 | if (Class->isGenericLambda()) return; |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 862 | // Add the conversion to function pointer. |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 863 | const FunctionProtoType *Proto |
| 864 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 865 | QualType FunctionPtrTy; |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 866 | QualType FunctionTy; |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 867 | { |
| 868 | FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 869 | CallingConv CC = S.Context.getDefaultCallingConvention( |
| 870 | Proto->isVariadic(), /*IsCXXMethod=*/false); |
| 871 | ExtInfo.ExtInfo = ExtInfo.ExtInfo.withCallingConv(CC); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 872 | ExtInfo.TypeQuals = 0; |
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 873 | FunctionTy = S.Context.getFunctionType(Proto->getResultType(), |
| 874 | Proto->getArgTypes(), ExtInfo); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 875 | FunctionPtrTy = S.Context.getPointerType(FunctionTy); |
| 876 | } |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 877 | |
| 878 | FunctionProtoType::ExtProtoInfo ExtInfo(S.Context.getDefaultCallingConvention( |
| 879 | /*IsVariadic=*/false, /*IsCXXMethod=*/true)); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 880 | ExtInfo.TypeQuals = Qualifiers::Const; |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 881 | QualType ConvTy = S.Context.getFunctionType(FunctionPtrTy, None, ExtInfo); |
| 882 | |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 883 | SourceLocation Loc = IntroducerRange.getBegin(); |
| 884 | DeclarationName Name |
| 885 | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
| 886 | S.Context.getCanonicalType(FunctionPtrTy)); |
| 887 | DeclarationNameLoc NameLoc; |
| 888 | NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(FunctionPtrTy, |
| 889 | Loc); |
| 890 | CXXConversionDecl *Conversion |
| 891 | = CXXConversionDecl::Create(S.Context, Class, Loc, |
| 892 | DeclarationNameInfo(Name, Loc, NameLoc), |
| 893 | ConvTy, |
| 894 | S.Context.getTrivialTypeSourceInfo(ConvTy, |
| 895 | Loc), |
Eli Friedman | 38fa961 | 2013-06-13 19:39:48 +0000 | [diff] [blame] | 896 | /*isInline=*/true, /*isExplicit=*/false, |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 897 | /*isConstexpr=*/false, |
| 898 | CallOperator->getBody()->getLocEnd()); |
| 899 | Conversion->setAccess(AS_public); |
| 900 | Conversion->setImplicit(true); |
| 901 | Class->addDecl(Conversion); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 902 | // Add a non-static member function that will be the result of |
| 903 | // the conversion with a certain unique ID. |
| 904 | Name = &S.Context.Idents.get(getLambdaStaticInvokerName()); |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 905 | CXXMethodDecl *Invoke |
| 906 | = CXXMethodDecl::Create(S.Context, Class, Loc, |
| 907 | DeclarationNameInfo(Name, Loc), FunctionTy, |
| 908 | CallOperator->getTypeSourceInfo(), |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 909 | SC_Static, /*IsInline=*/true, |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 910 | /*IsConstexpr=*/false, |
| 911 | CallOperator->getBody()->getLocEnd()); |
| 912 | SmallVector<ParmVarDecl *, 4> InvokeParams; |
| 913 | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { |
| 914 | ParmVarDecl *From = CallOperator->getParamDecl(I); |
| 915 | InvokeParams.push_back(ParmVarDecl::Create(S.Context, Invoke, |
| 916 | From->getLocStart(), |
| 917 | From->getLocation(), |
| 918 | From->getIdentifier(), |
| 919 | From->getType(), |
| 920 | From->getTypeSourceInfo(), |
| 921 | From->getStorageClass(), |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 922 | /*DefaultArg=*/0)); |
| 923 | } |
| 924 | Invoke->setParams(InvokeParams); |
| 925 | Invoke->setAccess(AS_private); |
| 926 | Invoke->setImplicit(true); |
| 927 | Class->addDecl(Invoke); |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 930 | /// \brief Add a lambda's conversion to block pointer. |
| 931 | static void addBlockPointerConversion(Sema &S, |
| 932 | SourceRange IntroducerRange, |
| 933 | CXXRecordDecl *Class, |
| 934 | CXXMethodDecl *CallOperator) { |
| 935 | const FunctionProtoType *Proto |
| 936 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
| 937 | QualType BlockPtrTy; |
| 938 | { |
| 939 | FunctionProtoType::ExtProtoInfo ExtInfo = Proto->getExtProtoInfo(); |
| 940 | ExtInfo.TypeQuals = 0; |
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 941 | QualType FunctionTy = S.Context.getFunctionType( |
| 942 | Proto->getResultType(), Proto->getArgTypes(), ExtInfo); |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 943 | BlockPtrTy = S.Context.getBlockPointerType(FunctionTy); |
| 944 | } |
Reid Kleckner | ef07203 | 2013-08-27 23:08:25 +0000 | [diff] [blame] | 945 | |
| 946 | FunctionProtoType::ExtProtoInfo ExtInfo(S.Context.getDefaultCallingConvention( |
| 947 | /*IsVariadic=*/false, /*IsCXXMethod=*/true)); |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 948 | ExtInfo.TypeQuals = Qualifiers::Const; |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 949 | QualType ConvTy = S.Context.getFunctionType(BlockPtrTy, None, ExtInfo); |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 950 | |
| 951 | SourceLocation Loc = IntroducerRange.getBegin(); |
| 952 | DeclarationName Name |
| 953 | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
| 954 | S.Context.getCanonicalType(BlockPtrTy)); |
| 955 | DeclarationNameLoc NameLoc; |
| 956 | NameLoc.NamedType.TInfo = S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc); |
| 957 | CXXConversionDecl *Conversion |
| 958 | = CXXConversionDecl::Create(S.Context, Class, Loc, |
| 959 | DeclarationNameInfo(Name, Loc, NameLoc), |
| 960 | ConvTy, |
| 961 | S.Context.getTrivialTypeSourceInfo(ConvTy, Loc), |
Eli Friedman | 95099ef | 2013-06-13 20:56:27 +0000 | [diff] [blame] | 962 | /*isInline=*/true, /*isExplicit=*/false, |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 963 | /*isConstexpr=*/false, |
| 964 | CallOperator->getBody()->getLocEnd()); |
| 965 | Conversion->setAccess(AS_public); |
| 966 | Conversion->setImplicit(true); |
| 967 | Class->addDecl(Conversion); |
| 968 | } |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 970 | ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 971 | Scope *CurScope, |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 972 | bool IsInstantiation) { |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 973 | // Collect information from the lambda scope. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 974 | SmallVector<LambdaExpr::Capture, 4> Captures; |
| 975 | SmallVector<Expr *, 4> CaptureInits; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 976 | LambdaCaptureDefault CaptureDefault; |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 977 | SourceLocation CaptureDefaultLoc; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 978 | CXXRecordDecl *Class; |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 979 | CXXMethodDecl *CallOperator; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 980 | SourceRange IntroducerRange; |
| 981 | bool ExplicitParams; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 982 | bool ExplicitResultType; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 983 | bool LambdaExprNeedsCleanups; |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 984 | bool ContainsUnexpandedParameterPack; |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 985 | SmallVector<VarDecl *, 4> ArrayIndexVars; |
| 986 | SmallVector<unsigned, 4> ArrayIndexStarts; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 987 | { |
| 988 | LambdaScopeInfo *LSI = getCurLambda(); |
Douglas Gregor | ef7d78b | 2012-02-10 08:36:38 +0000 | [diff] [blame] | 989 | CallOperator = LSI->CallOperator; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 990 | Class = LSI->Lambda; |
| 991 | IntroducerRange = LSI->IntroducerRange; |
| 992 | ExplicitParams = LSI->ExplicitParams; |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 993 | ExplicitResultType = !LSI->HasImplicitReturnType; |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 994 | LambdaExprNeedsCleanups = LSI->ExprNeedsCleanups; |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 995 | ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack; |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 996 | ArrayIndexVars.swap(LSI->ArrayIndexVars); |
| 997 | ArrayIndexStarts.swap(LSI->ArrayIndexStarts); |
| 998 | |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 999 | // Translate captures. |
| 1000 | for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) { |
| 1001 | LambdaScopeInfo::Capture From = LSI->Captures[I]; |
| 1002 | assert(!From.isBlockCapture() && "Cannot capture __block variables"); |
| 1003 | bool IsImplicit = I >= LSI->NumExplicitCaptures; |
| 1004 | |
| 1005 | // Handle 'this' capture. |
| 1006 | if (From.isThisCapture()) { |
| 1007 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), |
| 1008 | IsImplicit, |
| 1009 | LCK_This)); |
| 1010 | CaptureInits.push_back(new (Context) CXXThisExpr(From.getLocation(), |
| 1011 | getCurrentThisType(), |
| 1012 | /*isImplicit=*/true)); |
| 1013 | continue; |
| 1014 | } |
| 1015 | |
| 1016 | VarDecl *Var = From.getVariable(); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 1017 | LambdaCaptureKind Kind = From.isCopyCapture()? LCK_ByCopy : LCK_ByRef; |
| 1018 | Captures.push_back(LambdaExpr::Capture(From.getLocation(), IsImplicit, |
Douglas Gregor | a736524 | 2012-02-14 19:27:52 +0000 | [diff] [blame] | 1019 | Kind, Var, From.getEllipsisLoc())); |
Richard Smith | 0d8e964 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 1020 | CaptureInits.push_back(From.getInitExpr()); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | switch (LSI->ImpCaptureStyle) { |
| 1024 | case CapturingScopeInfo::ImpCap_None: |
| 1025 | CaptureDefault = LCD_None; |
| 1026 | break; |
| 1027 | |
| 1028 | case CapturingScopeInfo::ImpCap_LambdaByval: |
| 1029 | CaptureDefault = LCD_ByCopy; |
| 1030 | break; |
| 1031 | |
Tareq A. Siraj | 6afcf88 | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 1032 | case CapturingScopeInfo::ImpCap_CapturedRegion: |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 1033 | case CapturingScopeInfo::ImpCap_LambdaByref: |
| 1034 | CaptureDefault = LCD_ByRef; |
| 1035 | break; |
| 1036 | |
| 1037 | case CapturingScopeInfo::ImpCap_Block: |
| 1038 | llvm_unreachable("block capture in lambda"); |
| 1039 | break; |
| 1040 | } |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 1041 | CaptureDefaultLoc = LSI->CaptureDefaultLoc; |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 1042 | |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 1043 | // C++11 [expr.prim.lambda]p4: |
| 1044 | // If a lambda-expression does not include a |
| 1045 | // trailing-return-type, it is as if the trailing-return-type |
| 1046 | // denotes the following type: |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 1047 | // |
| 1048 | // Skip for C++1y return type deduction semantics which uses |
| 1049 | // different machinery. |
| 1050 | // FIXME: Refactor and Merge the return type deduction machinery. |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 1051 | // FIXME: Assumes current resolution to core issue 975. |
Richard Smith | 41d0958 | 2013-09-25 05:02:54 +0000 | [diff] [blame] | 1052 | if (LSI->HasImplicitReturnType && !getLangOpts().CPlusPlus1y) { |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 1053 | deduceClosureReturnType(*LSI); |
| 1054 | |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 1055 | // - if there are no return statements in the |
| 1056 | // compound-statement, or all return statements return |
| 1057 | // either an expression of type void or no expression or |
| 1058 | // braced-init-list, the type void; |
| 1059 | if (LSI->ReturnType.isNull()) { |
| 1060 | LSI->ReturnType = Context.VoidTy; |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | // Create a function type with the inferred return type. |
| 1064 | const FunctionProtoType *Proto |
| 1065 | = CallOperator->getType()->getAs<FunctionProtoType>(); |
Reid Kleckner | 0567a79 | 2013-06-10 20:51:09 +0000 | [diff] [blame] | 1066 | QualType FunctionTy = Context.getFunctionType( |
| 1067 | LSI->ReturnType, Proto->getArgTypes(), Proto->getExtProtoInfo()); |
Douglas Gregor | 54042f1 | 2012-02-09 10:18:50 +0000 | [diff] [blame] | 1068 | CallOperator->setType(FunctionTy); |
| 1069 | } |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 1070 | // C++ [expr.prim.lambda]p7: |
| 1071 | // The lambda-expression's compound-statement yields the |
| 1072 | // function-body (8.4) of the function call operator [...]. |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 1073 | ActOnFinishFunctionBody(CallOperator, Body, IsInstantiation); |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 1074 | CallOperator->setLexicalDeclContext(Class); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1075 | Decl *TemplateOrNonTemplateCallOperatorDecl = |
| 1076 | CallOperator->getDescribedFunctionTemplate() |
| 1077 | ? CallOperator->getDescribedFunctionTemplate() |
| 1078 | : cast<Decl>(CallOperator); |
| 1079 | |
| 1080 | TemplateOrNonTemplateCallOperatorDecl->setLexicalDeclContext(Class); |
| 1081 | Class->addDecl(TemplateOrNonTemplateCallOperatorDecl); |
| 1082 | |
Douglas Gregor | b09ab8c | 2012-02-21 20:05:31 +0000 | [diff] [blame] | 1083 | PopExpressionEvaluationContext(); |
Douglas Gregor | 215e4e1 | 2012-02-12 17:34:23 +0000 | [diff] [blame] | 1084 | |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 1085 | // C++11 [expr.prim.lambda]p6: |
| 1086 | // The closure type for a lambda-expression with no lambda-capture |
| 1087 | // has a public non-virtual non-explicit const conversion function |
| 1088 | // to pointer to function having the same parameter and return |
| 1089 | // types as the closure type's function call operator. |
Douglas Gregor | c25d1c9 | 2012-02-15 22:00:51 +0000 | [diff] [blame] | 1090 | if (Captures.empty() && CaptureDefault == LCD_None) |
| 1091 | addFunctionPointerConversion(*this, IntroducerRange, Class, |
| 1092 | CallOperator); |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 1094 | // Objective-C++: |
| 1095 | // The closure type for a lambda-expression has a public non-virtual |
| 1096 | // non-explicit const conversion function to a block pointer having the |
| 1097 | // same parameter and return types as the closure type's function call |
| 1098 | // operator. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1099 | if (getLangOpts().Blocks && getLangOpts().ObjC1) |
Douglas Gregor | c2956e5 | 2012-02-15 22:08:38 +0000 | [diff] [blame] | 1100 | addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator); |
| 1101 | |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 1102 | // Finalize the lambda class. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1103 | SmallVector<Decl*, 4> Fields; |
| 1104 | for (RecordDecl::field_iterator i = Class->field_begin(), |
| 1105 | e = Class->field_end(); i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1106 | Fields.push_back(*i); |
Douglas Gregor | b555971 | 2012-02-10 16:13:20 +0000 | [diff] [blame] | 1107 | ActOnFields(0, Class->getLocation(), Class, Fields, |
| 1108 | SourceLocation(), SourceLocation(), 0); |
| 1109 | CheckCompletedCXXClass(Class); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 1112 | if (LambdaExprNeedsCleanups) |
| 1113 | ExprNeedsCleanups = true; |
Douglas Gregor | 9e8c92a | 2012-02-20 19:44:39 +0000 | [diff] [blame] | 1114 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 1115 | LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 1116 | CaptureDefault, CaptureDefaultLoc, |
| 1117 | Captures, |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 1118 | ExplicitParams, ExplicitResultType, |
| 1119 | CaptureInits, ArrayIndexVars, |
Richard Smith | 612409e | 2012-07-25 03:56:55 +0000 | [diff] [blame] | 1120 | ArrayIndexStarts, Body->getLocEnd(), |
| 1121 | ContainsUnexpandedParameterPack); |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1122 | Class->setLambdaExpr(Lambda); |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 1123 | // C++11 [expr.prim.lambda]p2: |
| 1124 | // A lambda-expression shall not appear in an unevaluated operand |
| 1125 | // (Clause 5). |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 1126 | if (!CurContext->isDependentContext()) { |
| 1127 | switch (ExprEvalContexts.back().Context) { |
| 1128 | case Unevaluated: |
John McCall | aeeacf7 | 2013-05-03 00:10:13 +0000 | [diff] [blame] | 1129 | case UnevaluatedAbstract: |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 1130 | // We don't actually diagnose this case immediately, because we |
| 1131 | // could be within a context where we might find out later that |
| 1132 | // the expression is potentially evaluated (e.g., for typeid). |
| 1133 | ExprEvalContexts.back().Lambdas.push_back(Lambda); |
| 1134 | break; |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 1135 | |
Douglas Gregor | d5387e8 | 2012-02-14 00:00:48 +0000 | [diff] [blame] | 1136 | case ConstantEvaluated: |
| 1137 | case PotentiallyEvaluated: |
| 1138 | case PotentiallyEvaluatedIfUsed: |
| 1139 | break; |
| 1140 | } |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 1141 | } |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1142 | // TODO: Implement capturing. |
| 1143 | if (Lambda->isGenericLambda()) { |
| 1144 | if (Lambda->getCaptureDefault() != LCD_None) { |
| 1145 | Diag(Lambda->getIntroducerRange().getBegin(), |
| 1146 | diag::err_glambda_not_fully_implemented) |
| 1147 | << " capturing not implemented yet"; |
| 1148 | return ExprError(); |
| 1149 | } |
| 1150 | } |
Douglas Gregor | 503384f | 2012-02-09 00:47:04 +0000 | [diff] [blame] | 1151 | return MaybeBindToTemporary(Lambda); |
Douglas Gregor | e2a7ad0 | 2012-02-08 21:18:48 +0000 | [diff] [blame] | 1152 | } |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 1153 | |
| 1154 | ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation, |
| 1155 | SourceLocation ConvLocation, |
| 1156 | CXXConversionDecl *Conv, |
| 1157 | Expr *Src) { |
| 1158 | // Make sure that the lambda call operator is marked used. |
| 1159 | CXXRecordDecl *Lambda = Conv->getParent(); |
| 1160 | CXXMethodDecl *CallOperator |
| 1161 | = cast<CXXMethodDecl>( |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1162 | Lambda->lookup( |
| 1163 | Context.DeclarationNames.getCXXOperatorName(OO_Call)).front()); |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 1164 | CallOperator->setReferenced(); |
Eli Friedman | 86164e8 | 2013-09-05 00:02:25 +0000 | [diff] [blame] | 1165 | CallOperator->markUsed(Context); |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 1166 | |
| 1167 | ExprResult Init = PerformCopyInitialization( |
| 1168 | InitializedEntity::InitializeBlock(ConvLocation, |
| 1169 | Src->getType(), |
| 1170 | /*NRVO=*/false), |
| 1171 | CurrentLocation, Src); |
| 1172 | if (!Init.isInvalid()) |
| 1173 | Init = ActOnFinishFullExpr(Init.take()); |
| 1174 | |
| 1175 | if (Init.isInvalid()) |
| 1176 | return ExprError(); |
| 1177 | |
| 1178 | // Create the new block to be returned. |
| 1179 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, ConvLocation); |
| 1180 | |
| 1181 | // Set the type information. |
| 1182 | Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo()); |
| 1183 | Block->setIsVariadic(CallOperator->isVariadic()); |
| 1184 | Block->setBlockMissingReturnType(false); |
| 1185 | |
| 1186 | // Add parameters. |
| 1187 | SmallVector<ParmVarDecl *, 4> BlockParams; |
| 1188 | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) { |
| 1189 | ParmVarDecl *From = CallOperator->getParamDecl(I); |
| 1190 | BlockParams.push_back(ParmVarDecl::Create(Context, Block, |
| 1191 | From->getLocStart(), |
| 1192 | From->getLocation(), |
| 1193 | From->getIdentifier(), |
| 1194 | From->getType(), |
| 1195 | From->getTypeSourceInfo(), |
| 1196 | From->getStorageClass(), |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 1197 | /*DefaultArg=*/0)); |
| 1198 | } |
| 1199 | Block->setParams(BlockParams); |
| 1200 | |
| 1201 | Block->setIsConversionFromLambda(true); |
| 1202 | |
| 1203 | // Add capture. The capture uses a fake variable, which doesn't correspond |
| 1204 | // to any actual memory location. However, the initializer copy-initializes |
| 1205 | // the lambda object. |
| 1206 | TypeSourceInfo *CapVarTSI = |
| 1207 | Context.getTrivialTypeSourceInfo(Src->getType()); |
| 1208 | VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation, |
| 1209 | ConvLocation, 0, |
| 1210 | Src->getType(), CapVarTSI, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1211 | SC_None); |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 1212 | BlockDecl::Capture Capture(/*Variable=*/CapVar, /*ByRef=*/false, |
| 1213 | /*Nested=*/false, /*Copy=*/Init.take()); |
| 1214 | Block->setCaptures(Context, &Capture, &Capture + 1, |
| 1215 | /*CapturesCXXThis=*/false); |
| 1216 | |
| 1217 | // Add a fake function body to the block. IR generation is responsible |
| 1218 | // 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] | 1219 | Block->setBody(new (Context) CompoundStmt(ConvLocation)); |
Eli Friedman | 23f0267 | 2012-03-01 04:01:32 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Create the block literal expression. |
| 1222 | Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType()); |
| 1223 | ExprCleanupObjects.push_back(Block); |
| 1224 | ExprNeedsCleanups = true; |
| 1225 | |
| 1226 | return BuildBlock; |
| 1227 | } |