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