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