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