[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / clang / lib / Sema / SemaLambda.cpp
blobca09b0481bcac762fc4b8d43bf40468e30096c75
1 //===--- SemaLambda.cpp - Semantic Analysis for C++11 Lambdas -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements semantic analysis for C++ lambda expressions.
11 //===----------------------------------------------------------------------===//
12 #include "clang/Sema/DeclSpec.h"
13 #include "TypeLocBuilder.h"
14 #include "clang/AST/ASTLambda.h"
15 #include "clang/AST/ExprCXX.h"
16 #include "clang/Basic/TargetInfo.h"
17 #include "clang/Sema/Initialization.h"
18 #include "clang/Sema/Lookup.h"
19 #include "clang/Sema/Scope.h"
20 #include "clang/Sema/ScopeInfo.h"
21 #include "clang/Sema/SemaInternal.h"
22 #include "clang/Sema/SemaLambda.h"
23 #include "clang/Sema/Template.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include <optional>
26 using namespace clang;
27 using namespace sema;
29 /// Examines the FunctionScopeInfo stack to determine the nearest
30 /// enclosing lambda (to the current lambda) that is 'capture-ready' for
31 /// the variable referenced in the current lambda (i.e. \p VarToCapture).
32 /// If successful, returns the index into Sema's FunctionScopeInfo stack
33 /// of the capture-ready lambda's LambdaScopeInfo.
34 ///
35 /// Climbs down the stack of lambdas (deepest nested lambda - i.e. current
36 /// lambda - is on top) to determine the index of the nearest enclosing/outer
37 /// lambda that is ready to capture the \p VarToCapture being referenced in
38 /// the current lambda.
39 /// As we climb down the stack, we want the index of the first such lambda -
40 /// that is the lambda with the highest index that is 'capture-ready'.
41 ///
42 /// A lambda 'L' is capture-ready for 'V' (var or this) if:
43 /// - its enclosing context is non-dependent
44 /// - and if the chain of lambdas between L and the lambda in which
45 /// V is potentially used (i.e. the lambda at the top of the scope info
46 /// stack), can all capture or have already captured V.
47 /// If \p VarToCapture is 'null' then we are trying to capture 'this'.
48 ///
49 /// Note that a lambda that is deemed 'capture-ready' still needs to be checked
50 /// for whether it is 'capture-capable' (see
51 /// getStackIndexOfNearestEnclosingCaptureCapableLambda), before it can truly
52 /// capture.
53 ///
54 /// \param FunctionScopes - Sema's stack of nested FunctionScopeInfo's (which a
55 /// LambdaScopeInfo inherits from). The current/deepest/innermost lambda
56 /// is at the top of the stack and has the highest index.
57 /// \param VarToCapture - the variable to capture. If NULL, capture 'this'.
58 ///
59 /// \returns An std::optional<unsigned> Index that if evaluates to 'true'
60 /// contains the index (into Sema's FunctionScopeInfo stack) of the innermost
61 /// lambda which is capture-ready. If the return value evaluates to 'false'
62 /// then no lambda is capture-ready for \p VarToCapture.
64 static inline std::optional<unsigned>
65 getStackIndexOfNearestEnclosingCaptureReadyLambda(
66 ArrayRef<const clang::sema::FunctionScopeInfo *> FunctionScopes,
67 ValueDecl *VarToCapture) {
68 // Label failure to capture.
69 const std::optional<unsigned> NoLambdaIsCaptureReady;
71 // Ignore all inner captured regions.
72 unsigned CurScopeIndex = FunctionScopes.size() - 1;
73 while (CurScopeIndex > 0 && isa<clang::sema::CapturedRegionScopeInfo>(
74 FunctionScopes[CurScopeIndex]))
75 --CurScopeIndex;
76 assert(
77 isa<clang::sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex]) &&
78 "The function on the top of sema's function-info stack must be a lambda");
80 // If VarToCapture is null, we are attempting to capture 'this'.
81 const bool IsCapturingThis = !VarToCapture;
82 const bool IsCapturingVariable = !IsCapturingThis;
84 // Start with the current lambda at the top of the stack (highest index).
85 DeclContext *EnclosingDC =
86 cast<sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex])->CallOperator;
88 do {
89 const clang::sema::LambdaScopeInfo *LSI =
90 cast<sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex]);
91 // IF we have climbed down to an intervening enclosing lambda that contains
92 // the variable declaration - it obviously can/must not capture the
93 // variable.
94 // Since its enclosing DC is dependent, all the lambdas between it and the
95 // innermost nested lambda are dependent (otherwise we wouldn't have
96 // arrived here) - so we don't yet have a lambda that can capture the
97 // variable.
98 if (IsCapturingVariable &&
99 VarToCapture->getDeclContext()->Equals(EnclosingDC))
100 return NoLambdaIsCaptureReady;
102 // For an enclosing lambda to be capture ready for an entity, all
103 // intervening lambda's have to be able to capture that entity. If even
104 // one of the intervening lambda's is not capable of capturing the entity
105 // then no enclosing lambda can ever capture that entity.
106 // For e.g.
107 // const int x = 10;
108 // [=](auto a) { #1
109 // [](auto b) { #2 <-- an intervening lambda that can never capture 'x'
110 // [=](auto c) { #3
111 // f(x, c); <-- can not lead to x's speculative capture by #1 or #2
112 // }; }; };
113 // If they do not have a default implicit capture, check to see
114 // if the entity has already been explicitly captured.
115 // If even a single dependent enclosing lambda lacks the capability
116 // to ever capture this variable, there is no further enclosing
117 // non-dependent lambda that can capture this variable.
118 if (LSI->ImpCaptureStyle == sema::LambdaScopeInfo::ImpCap_None) {
119 if (IsCapturingVariable && !LSI->isCaptured(VarToCapture))
120 return NoLambdaIsCaptureReady;
121 if (IsCapturingThis && !LSI->isCXXThisCaptured())
122 return NoLambdaIsCaptureReady;
124 EnclosingDC = getLambdaAwareParentOfDeclContext(EnclosingDC);
126 assert(CurScopeIndex);
127 --CurScopeIndex;
128 } while (!EnclosingDC->isTranslationUnit() &&
129 EnclosingDC->isDependentContext() &&
130 isLambdaCallOperator(EnclosingDC));
132 assert(CurScopeIndex < (FunctionScopes.size() - 1));
133 // If the enclosingDC is not dependent, then the immediately nested lambda
134 // (one index above) is capture-ready.
135 if (!EnclosingDC->isDependentContext())
136 return CurScopeIndex + 1;
137 return NoLambdaIsCaptureReady;
140 /// Examines the FunctionScopeInfo stack to determine the nearest
141 /// enclosing lambda (to the current lambda) that is 'capture-capable' for
142 /// the variable referenced in the current lambda (i.e. \p VarToCapture).
143 /// If successful, returns the index into Sema's FunctionScopeInfo stack
144 /// of the capture-capable lambda's LambdaScopeInfo.
146 /// Given the current stack of lambdas being processed by Sema and
147 /// the variable of interest, to identify the nearest enclosing lambda (to the
148 /// current lambda at the top of the stack) that can truly capture
149 /// a variable, it has to have the following two properties:
150 /// a) 'capture-ready' - be the innermost lambda that is 'capture-ready':
151 /// - climb down the stack (i.e. starting from the innermost and examining
152 /// each outer lambda step by step) checking if each enclosing
153 /// lambda can either implicitly or explicitly capture the variable.
154 /// Record the first such lambda that is enclosed in a non-dependent
155 /// context. If no such lambda currently exists return failure.
156 /// b) 'capture-capable' - make sure the 'capture-ready' lambda can truly
157 /// capture the variable by checking all its enclosing lambdas:
158 /// - check if all outer lambdas enclosing the 'capture-ready' lambda
159 /// identified above in 'a' can also capture the variable (this is done
160 /// via tryCaptureVariable for variables and CheckCXXThisCapture for
161 /// 'this' by passing in the index of the Lambda identified in step 'a')
163 /// \param FunctionScopes - Sema's stack of nested FunctionScopeInfo's (which a
164 /// LambdaScopeInfo inherits from). The current/deepest/innermost lambda
165 /// is at the top of the stack.
167 /// \param VarToCapture - the variable to capture. If NULL, capture 'this'.
170 /// \returns An std::optional<unsigned> Index that if evaluates to 'true'
171 /// contains the index (into Sema's FunctionScopeInfo stack) of the innermost
172 /// lambda which is capture-capable. If the return value evaluates to 'false'
173 /// then no lambda is capture-capable for \p VarToCapture.
175 std::optional<unsigned>
176 clang::getStackIndexOfNearestEnclosingCaptureCapableLambda(
177 ArrayRef<const sema::FunctionScopeInfo *> FunctionScopes,
178 ValueDecl *VarToCapture, Sema &S) {
180 const std::optional<unsigned> NoLambdaIsCaptureCapable;
182 const std::optional<unsigned> OptionalStackIndex =
183 getStackIndexOfNearestEnclosingCaptureReadyLambda(FunctionScopes,
184 VarToCapture);
185 if (!OptionalStackIndex)
186 return NoLambdaIsCaptureCapable;
188 const unsigned IndexOfCaptureReadyLambda = *OptionalStackIndex;
189 assert(((IndexOfCaptureReadyLambda != (FunctionScopes.size() - 1)) ||
190 S.getCurGenericLambda()) &&
191 "The capture ready lambda for a potential capture can only be the "
192 "current lambda if it is a generic lambda");
194 const sema::LambdaScopeInfo *const CaptureReadyLambdaLSI =
195 cast<sema::LambdaScopeInfo>(FunctionScopes[IndexOfCaptureReadyLambda]);
197 // If VarToCapture is null, we are attempting to capture 'this'
198 const bool IsCapturingThis = !VarToCapture;
199 const bool IsCapturingVariable = !IsCapturingThis;
201 if (IsCapturingVariable) {
202 // Check if the capture-ready lambda can truly capture the variable, by
203 // checking whether all enclosing lambdas of the capture-ready lambda allow
204 // the capture - i.e. make sure it is capture-capable.
205 QualType CaptureType, DeclRefType;
206 const bool CanCaptureVariable =
207 !S.tryCaptureVariable(VarToCapture,
208 /*ExprVarIsUsedInLoc*/ SourceLocation(),
209 clang::Sema::TryCapture_Implicit,
210 /*EllipsisLoc*/ SourceLocation(),
211 /*BuildAndDiagnose*/ false, CaptureType,
212 DeclRefType, &IndexOfCaptureReadyLambda);
213 if (!CanCaptureVariable)
214 return NoLambdaIsCaptureCapable;
215 } else {
216 // Check if the capture-ready lambda can truly capture 'this' by checking
217 // whether all enclosing lambdas of the capture-ready lambda can capture
218 // 'this'.
219 const bool CanCaptureThis =
220 !S.CheckCXXThisCapture(
221 CaptureReadyLambdaLSI->PotentialThisCaptureLocation,
222 /*Explicit*/ false, /*BuildAndDiagnose*/ false,
223 &IndexOfCaptureReadyLambda);
224 if (!CanCaptureThis)
225 return NoLambdaIsCaptureCapable;
227 return IndexOfCaptureReadyLambda;
230 static inline TemplateParameterList *
231 getGenericLambdaTemplateParameterList(LambdaScopeInfo *LSI, Sema &SemaRef) {
232 if (!LSI->GLTemplateParameterList && !LSI->TemplateParams.empty()) {
233 LSI->GLTemplateParameterList = TemplateParameterList::Create(
234 SemaRef.Context,
235 /*Template kw loc*/ SourceLocation(),
236 /*L angle loc*/ LSI->ExplicitTemplateParamsRange.getBegin(),
237 LSI->TemplateParams,
238 /*R angle loc*/LSI->ExplicitTemplateParamsRange.getEnd(),
239 LSI->RequiresClause.get());
241 return LSI->GLTemplateParameterList;
244 CXXRecordDecl *
245 Sema::createLambdaClosureType(SourceRange IntroducerRange, TypeSourceInfo *Info,
246 unsigned LambdaDependencyKind,
247 LambdaCaptureDefault CaptureDefault) {
248 DeclContext *DC = CurContext;
249 while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
250 DC = DC->getParent();
252 bool IsGenericLambda =
253 Info && getGenericLambdaTemplateParameterList(getCurLambda(), *this);
254 // Start constructing the lambda class.
255 CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(
256 Context, DC, Info, IntroducerRange.getBegin(), LambdaDependencyKind,
257 IsGenericLambda, CaptureDefault);
258 DC->addDecl(Class);
260 return Class;
263 /// 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;
271 DC = DC->getLexicalParent();
274 return false;
277 std::tuple<MangleNumberingContext *, Decl *>
278 Sema::getCurrentMangleNumberContext(const DeclContext *DC) {
279 // Compute the context for allocating mangling numbers in the current
280 // expression, if the ABI requires them.
281 Decl *ManglingContextDecl = ExprEvalContexts.back().ManglingContextDecl;
283 enum ContextKind {
284 Normal,
285 DefaultArgument,
286 DataMember,
287 InlineVariable,
288 TemplatedVariable,
289 Concept
290 } Kind = Normal;
292 bool IsInNonspecializedTemplate =
293 inTemplateInstantiation() || CurContext->isDependentContext();
295 // Default arguments of member function parameters that appear in a class
296 // definition, as well as the initializers of data members, receive special
297 // treatment. Identify them.
298 if (ManglingContextDecl) {
299 if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) {
300 if (const DeclContext *LexicalDC
301 = Param->getDeclContext()->getLexicalParent())
302 if (LexicalDC->isRecord())
303 Kind = DefaultArgument;
304 } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) {
305 if (Var->getMostRecentDecl()->isInline())
306 Kind = InlineVariable;
307 else if (Var->getDeclContext()->isRecord() && IsInNonspecializedTemplate)
308 Kind = TemplatedVariable;
309 else if (Var->getDescribedVarTemplate())
310 Kind = TemplatedVariable;
311 else if (auto *VTS = dyn_cast<VarTemplateSpecializationDecl>(Var)) {
312 if (!VTS->isExplicitSpecialization())
313 Kind = TemplatedVariable;
315 } else if (isa<FieldDecl>(ManglingContextDecl)) {
316 Kind = DataMember;
317 } else if (isa<ImplicitConceptSpecializationDecl>(ManglingContextDecl)) {
318 Kind = Concept;
322 // Itanium ABI [5.1.7]:
323 // In the following contexts [...] the one-definition rule requires closure
324 // types in different translation units to "correspond":
325 switch (Kind) {
326 case Normal: {
327 // -- the bodies of inline or templated functions
328 if ((IsInNonspecializedTemplate &&
329 !(ManglingContextDecl && isa<ParmVarDecl>(ManglingContextDecl))) ||
330 isInInlineFunction(CurContext)) {
331 while (auto *CD = dyn_cast<CapturedDecl>(DC))
332 DC = CD->getParent();
333 return std::make_tuple(&Context.getManglingNumberContext(DC), nullptr);
336 return std::make_tuple(nullptr, nullptr);
339 case Concept:
340 // Concept definitions aren't code generated and thus aren't mangled,
341 // however the ManglingContextDecl is important for the purposes of
342 // re-forming the template argument list of the lambda for constraint
343 // evaluation.
344 case DataMember:
345 // -- default member initializers
346 case DefaultArgument:
347 // -- default arguments appearing in class definitions
348 case InlineVariable:
349 case TemplatedVariable:
350 // -- the initializers of inline or templated variables
351 return std::make_tuple(
352 &Context.getManglingNumberContext(ASTContext::NeedExtraManglingDecl,
353 ManglingContextDecl),
354 ManglingContextDecl);
357 llvm_unreachable("unexpected context");
360 static QualType
361 buildTypeForLambdaCallOperator(Sema &S, clang::CXXRecordDecl *Class,
362 TemplateParameterList *TemplateParams,
363 TypeSourceInfo *MethodTypeInfo) {
364 assert(MethodTypeInfo && "expected a non null type");
366 QualType MethodType = MethodTypeInfo->getType();
367 // If a lambda appears in a dependent context or is a generic lambda (has
368 // template parameters) and has an 'auto' return type, deduce it to a
369 // dependent type.
370 if (Class->isDependentContext() || TemplateParams) {
371 const FunctionProtoType *FPT = MethodType->castAs<FunctionProtoType>();
372 QualType Result = FPT->getReturnType();
373 if (Result->isUndeducedType()) {
374 Result = S.SubstAutoTypeDependent(Result);
375 MethodType = S.Context.getFunctionType(Result, FPT->getParamTypes(),
376 FPT->getExtProtoInfo());
379 return MethodType;
382 // [C++2b] [expr.prim.lambda.closure] p4
383 // Given a lambda with a lambda-capture, the type of the explicit object
384 // parameter, if any, of the lambda's function call operator (possibly
385 // instantiated from a function call operator template) shall be either:
386 // - the closure type,
387 // - class type derived from the closure type, or
388 // - a reference to a possibly cv-qualified such type.
389 void Sema::DiagnoseInvalidExplicitObjectParameterInLambda(
390 CXXMethodDecl *Method) {
391 if (!isLambdaCallWithExplicitObjectParameter(Method))
392 return;
393 CXXRecordDecl *RD = Method->getParent();
394 if (Method->getType()->isDependentType())
395 return;
396 if (RD->isCapturelessLambda())
397 return;
398 QualType ExplicitObjectParameterType = Method->getParamDecl(0)
399 ->getType()
400 .getNonReferenceType()
401 .getUnqualifiedType()
402 .getDesugaredType(getASTContext());
403 QualType LambdaType = getASTContext().getRecordType(RD);
404 if (LambdaType == ExplicitObjectParameterType)
405 return;
406 if (IsDerivedFrom(RD->getLocation(), ExplicitObjectParameterType, LambdaType))
407 return;
408 Diag(Method->getParamDecl(0)->getLocation(),
409 diag::err_invalid_explicit_object_type_in_lambda)
410 << ExplicitObjectParameterType;
413 void Sema::handleLambdaNumbering(
414 CXXRecordDecl *Class, CXXMethodDecl *Method,
415 std::optional<CXXRecordDecl::LambdaNumbering> NumberingOverride) {
416 if (NumberingOverride) {
417 Class->setLambdaNumbering(*NumberingOverride);
418 return;
421 ContextRAII ManglingContext(*this, Class->getDeclContext());
423 auto getMangleNumberingContext =
424 [this](CXXRecordDecl *Class,
425 Decl *ManglingContextDecl) -> MangleNumberingContext * {
426 // Get mangle numbering context if there's any extra decl context.
427 if (ManglingContextDecl)
428 return &Context.getManglingNumberContext(
429 ASTContext::NeedExtraManglingDecl, ManglingContextDecl);
430 // Otherwise, from that lambda's decl context.
431 auto DC = Class->getDeclContext();
432 while (auto *CD = dyn_cast<CapturedDecl>(DC))
433 DC = CD->getParent();
434 return &Context.getManglingNumberContext(DC);
437 CXXRecordDecl::LambdaNumbering Numbering;
438 MangleNumberingContext *MCtx;
439 std::tie(MCtx, Numbering.ContextDecl) =
440 getCurrentMangleNumberContext(Class->getDeclContext());
441 if (!MCtx && (getLangOpts().CUDA || getLangOpts().SYCLIsDevice ||
442 getLangOpts().SYCLIsHost)) {
443 // Force lambda numbering in CUDA/HIP as we need to name lambdas following
444 // ODR. Both device- and host-compilation need to have a consistent naming
445 // on kernel functions. As lambdas are potential part of these `__global__`
446 // function names, they needs numbering following ODR.
447 // Also force for SYCL, since we need this for the
448 // __builtin_sycl_unique_stable_name implementation, which depends on lambda
449 // mangling.
450 MCtx = getMangleNumberingContext(Class, Numbering.ContextDecl);
451 assert(MCtx && "Retrieving mangle numbering context failed!");
452 Numbering.HasKnownInternalLinkage = true;
454 if (MCtx) {
455 Numbering.IndexInContext = MCtx->getNextLambdaIndex();
456 Numbering.ManglingNumber = MCtx->getManglingNumber(Method);
457 Numbering.DeviceManglingNumber = MCtx->getDeviceManglingNumber(Method);
458 Class->setLambdaNumbering(Numbering);
460 if (auto *Source =
461 dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
462 Source->AssignedLambdaNumbering(Class);
466 static void buildLambdaScopeReturnType(Sema &S, LambdaScopeInfo *LSI,
467 CXXMethodDecl *CallOperator,
468 bool ExplicitResultType) {
469 if (ExplicitResultType) {
470 LSI->HasImplicitReturnType = false;
471 LSI->ReturnType = CallOperator->getReturnType();
472 if (!LSI->ReturnType->isDependentType() && !LSI->ReturnType->isVoidType())
473 S.RequireCompleteType(CallOperator->getBeginLoc(), LSI->ReturnType,
474 diag::err_lambda_incomplete_result);
475 } else {
476 LSI->HasImplicitReturnType = true;
480 void Sema::buildLambdaScope(LambdaScopeInfo *LSI, CXXMethodDecl *CallOperator,
481 SourceRange IntroducerRange,
482 LambdaCaptureDefault CaptureDefault,
483 SourceLocation CaptureDefaultLoc,
484 bool ExplicitParams, bool Mutable) {
485 LSI->CallOperator = CallOperator;
486 CXXRecordDecl *LambdaClass = CallOperator->getParent();
487 LSI->Lambda = LambdaClass;
488 if (CaptureDefault == LCD_ByCopy)
489 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval;
490 else if (CaptureDefault == LCD_ByRef)
491 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref;
492 LSI->CaptureDefaultLoc = CaptureDefaultLoc;
493 LSI->IntroducerRange = IntroducerRange;
494 LSI->ExplicitParams = ExplicitParams;
495 LSI->Mutable = Mutable;
498 void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) {
499 LSI->finishedExplicitCaptures();
502 void Sema::ActOnLambdaExplicitTemplateParameterList(
503 LambdaIntroducer &Intro, SourceLocation LAngleLoc,
504 ArrayRef<NamedDecl *> TParams, SourceLocation RAngleLoc,
505 ExprResult RequiresClause) {
506 LambdaScopeInfo *LSI = getCurLambda();
507 assert(LSI && "Expected a lambda scope");
508 assert(LSI->NumExplicitTemplateParams == 0 &&
509 "Already acted on explicit template parameters");
510 assert(LSI->TemplateParams.empty() &&
511 "Explicit template parameters should come "
512 "before invented (auto) ones");
513 assert(!TParams.empty() &&
514 "No template parameters to act on");
515 LSI->TemplateParams.append(TParams.begin(), TParams.end());
516 LSI->NumExplicitTemplateParams = TParams.size();
517 LSI->ExplicitTemplateParamsRange = {LAngleLoc, RAngleLoc};
518 LSI->RequiresClause = RequiresClause;
521 /// If this expression is an enumerator-like expression of some type
522 /// T, return the type T; otherwise, return null.
524 /// Pointer comparisons on the result here should always work because
525 /// it's derived from either the parent of an EnumConstantDecl
526 /// (i.e. the definition) or the declaration returned by
527 /// EnumType::getDecl() (i.e. the definition).
528 static EnumDecl *findEnumForBlockReturn(Expr *E) {
529 // An expression is an enumerator-like expression of type T if,
530 // ignoring parens and parens-like expressions:
531 E = E->IgnoreParens();
533 // - it is an enumerator whose enum type is T or
534 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
535 if (EnumConstantDecl *D
536 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
537 return cast<EnumDecl>(D->getDeclContext());
539 return nullptr;
542 // - it is a comma expression whose RHS is an enumerator-like
543 // expression of type T or
544 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
545 if (BO->getOpcode() == BO_Comma)
546 return findEnumForBlockReturn(BO->getRHS());
547 return nullptr;
550 // - it is a statement-expression whose value expression is an
551 // enumerator-like expression of type T or
552 if (StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
553 if (Expr *last = dyn_cast_or_null<Expr>(SE->getSubStmt()->body_back()))
554 return findEnumForBlockReturn(last);
555 return nullptr;
558 // - it is a ternary conditional operator (not the GNU ?:
559 // extension) whose second and third operands are
560 // enumerator-like expressions of type T or
561 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
562 if (EnumDecl *ED = findEnumForBlockReturn(CO->getTrueExpr()))
563 if (ED == findEnumForBlockReturn(CO->getFalseExpr()))
564 return ED;
565 return nullptr;
568 // (implicitly:)
569 // - it is an implicit integral conversion applied to an
570 // enumerator-like expression of type T or
571 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
572 // We can sometimes see integral conversions in valid
573 // enumerator-like expressions.
574 if (ICE->getCastKind() == CK_IntegralCast)
575 return findEnumForBlockReturn(ICE->getSubExpr());
577 // Otherwise, just rely on the type.
580 // - it is an expression of that formal enum type.
581 if (const EnumType *ET = E->getType()->getAs<EnumType>()) {
582 return ET->getDecl();
585 // Otherwise, nope.
586 return nullptr;
589 /// Attempt to find a type T for which the returned expression of the
590 /// given statement is an enumerator-like expression of that type.
591 static EnumDecl *findEnumForBlockReturn(ReturnStmt *ret) {
592 if (Expr *retValue = ret->getRetValue())
593 return findEnumForBlockReturn(retValue);
594 return nullptr;
597 /// Attempt to find a common type T for which all of the returned
598 /// expressions in a block are enumerator-like expressions of that
599 /// type.
600 static EnumDecl *findCommonEnumForBlockReturns(ArrayRef<ReturnStmt*> returns) {
601 ArrayRef<ReturnStmt*>::iterator i = returns.begin(), e = returns.end();
603 // Try to find one for the first return.
604 EnumDecl *ED = findEnumForBlockReturn(*i);
605 if (!ED) return nullptr;
607 // Check that the rest of the returns have the same enum.
608 for (++i; i != e; ++i) {
609 if (findEnumForBlockReturn(*i) != ED)
610 return nullptr;
613 // Never infer an anonymous enum type.
614 if (!ED->hasNameForLinkage()) return nullptr;
616 return ED;
619 /// Adjust the given return statements so that they formally return
620 /// the given type. It should require, at most, an IntegralCast.
621 static void adjustBlockReturnsToEnum(Sema &S, ArrayRef<ReturnStmt*> returns,
622 QualType returnType) {
623 for (ArrayRef<ReturnStmt*>::iterator
624 i = returns.begin(), e = returns.end(); i != e; ++i) {
625 ReturnStmt *ret = *i;
626 Expr *retValue = ret->getRetValue();
627 if (S.Context.hasSameType(retValue->getType(), returnType))
628 continue;
630 // Right now we only support integral fixup casts.
631 assert(returnType->isIntegralOrUnscopedEnumerationType());
632 assert(retValue->getType()->isIntegralOrUnscopedEnumerationType());
634 ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(retValue);
636 Expr *E = (cleanups ? cleanups->getSubExpr() : retValue);
637 E = ImplicitCastExpr::Create(S.Context, returnType, CK_IntegralCast, E,
638 /*base path*/ nullptr, VK_PRValue,
639 FPOptionsOverride());
640 if (cleanups) {
641 cleanups->setSubExpr(E);
642 } else {
643 ret->setRetValue(E);
648 void Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) {
649 assert(CSI.HasImplicitReturnType);
650 // If it was ever a placeholder, it had to been deduced to DependentTy.
651 assert(CSI.ReturnType.isNull() || !CSI.ReturnType->isUndeducedType());
652 assert((!isa<LambdaScopeInfo>(CSI) || !getLangOpts().CPlusPlus14) &&
653 "lambda expressions use auto deduction in C++14 onwards");
655 // C++ core issue 975:
656 // If a lambda-expression does not include a trailing-return-type,
657 // it is as if the trailing-return-type denotes the following type:
658 // - if there are no return statements in the compound-statement,
659 // or all return statements return either an expression of type
660 // void or no expression or braced-init-list, the type void;
661 // - otherwise, if all return statements return an expression
662 // and the types of the returned expressions after
663 // lvalue-to-rvalue conversion (4.1 [conv.lval]),
664 // array-to-pointer conversion (4.2 [conv.array]), and
665 // function-to-pointer conversion (4.3 [conv.func]) are the
666 // same, that common type;
667 // - otherwise, the program is ill-formed.
669 // C++ core issue 1048 additionally removes top-level cv-qualifiers
670 // from the types of returned expressions to match the C++14 auto
671 // deduction rules.
673 // In addition, in blocks in non-C++ modes, if all of the return
674 // statements are enumerator-like expressions of some type T, where
675 // T has a name for linkage, then we infer the return type of the
676 // block to be that type.
678 // First case: no return statements, implicit void return type.
679 ASTContext &Ctx = getASTContext();
680 if (CSI.Returns.empty()) {
681 // It's possible there were simply no /valid/ return statements.
682 // In this case, the first one we found may have at least given us a type.
683 if (CSI.ReturnType.isNull())
684 CSI.ReturnType = Ctx.VoidTy;
685 return;
688 // Second case: at least one return statement has dependent type.
689 // Delay type checking until instantiation.
690 assert(!CSI.ReturnType.isNull() && "We should have a tentative return type.");
691 if (CSI.ReturnType->isDependentType())
692 return;
694 // Try to apply the enum-fuzz rule.
695 if (!getLangOpts().CPlusPlus) {
696 assert(isa<BlockScopeInfo>(CSI));
697 const EnumDecl *ED = findCommonEnumForBlockReturns(CSI.Returns);
698 if (ED) {
699 CSI.ReturnType = Context.getTypeDeclType(ED);
700 adjustBlockReturnsToEnum(*this, CSI.Returns, CSI.ReturnType);
701 return;
705 // Third case: only one return statement. Don't bother doing extra work!
706 if (CSI.Returns.size() == 1)
707 return;
709 // General case: many return statements.
710 // Check that they all have compatible return types.
712 // We require the return types to strictly match here.
713 // Note that we've already done the required promotions as part of
714 // processing the return statement.
715 for (const ReturnStmt *RS : CSI.Returns) {
716 const Expr *RetE = RS->getRetValue();
718 QualType ReturnType =
719 (RetE ? RetE->getType() : Context.VoidTy).getUnqualifiedType();
720 if (Context.getCanonicalFunctionResultType(ReturnType) ==
721 Context.getCanonicalFunctionResultType(CSI.ReturnType)) {
722 // Use the return type with the strictest possible nullability annotation.
723 auto RetTyNullability = ReturnType->getNullability();
724 auto BlockNullability = CSI.ReturnType->getNullability();
725 if (BlockNullability &&
726 (!RetTyNullability ||
727 hasWeakerNullability(*RetTyNullability, *BlockNullability)))
728 CSI.ReturnType = ReturnType;
729 continue;
732 // FIXME: This is a poor diagnostic for ReturnStmts without expressions.
733 // TODO: It's possible that the *first* return is the divergent one.
734 Diag(RS->getBeginLoc(),
735 diag::err_typecheck_missing_return_type_incompatible)
736 << ReturnType << CSI.ReturnType << isa<LambdaScopeInfo>(CSI);
737 // Continue iterating so that we keep emitting diagnostics.
741 QualType Sema::buildLambdaInitCaptureInitialization(
742 SourceLocation Loc, bool ByRef, SourceLocation EllipsisLoc,
743 std::optional<unsigned> NumExpansions, IdentifierInfo *Id,
744 bool IsDirectInit, Expr *&Init) {
745 // Create an 'auto' or 'auto&' TypeSourceInfo that we can use to
746 // deduce against.
747 QualType DeductType = Context.getAutoDeductType();
748 TypeLocBuilder TLB;
749 AutoTypeLoc TL = TLB.push<AutoTypeLoc>(DeductType);
750 TL.setNameLoc(Loc);
751 if (ByRef) {
752 DeductType = BuildReferenceType(DeductType, true, Loc, Id);
753 assert(!DeductType.isNull() && "can't build reference to auto");
754 TLB.push<ReferenceTypeLoc>(DeductType).setSigilLoc(Loc);
756 if (EllipsisLoc.isValid()) {
757 if (Init->containsUnexpandedParameterPack()) {
758 Diag(EllipsisLoc, getLangOpts().CPlusPlus20
759 ? diag::warn_cxx17_compat_init_capture_pack
760 : diag::ext_init_capture_pack);
761 DeductType = Context.getPackExpansionType(DeductType, NumExpansions,
762 /*ExpectPackInType=*/false);
763 TLB.push<PackExpansionTypeLoc>(DeductType).setEllipsisLoc(EllipsisLoc);
764 } else {
765 // Just ignore the ellipsis for now and form a non-pack variable. We'll
766 // diagnose this later when we try to capture it.
769 TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType);
771 // Deduce the type of the init capture.
772 QualType DeducedType = deduceVarTypeFromInitializer(
773 /*VarDecl*/nullptr, DeclarationName(Id), DeductType, TSI,
774 SourceRange(Loc, Loc), IsDirectInit, Init);
775 if (DeducedType.isNull())
776 return QualType();
778 // Are we a non-list direct initialization?
779 ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init);
781 // Perform initialization analysis and ensure any implicit conversions
782 // (such as lvalue-to-rvalue) are enforced.
783 InitializedEntity Entity =
784 InitializedEntity::InitializeLambdaCapture(Id, DeducedType, Loc);
785 InitializationKind Kind =
786 IsDirectInit
787 ? (CXXDirectInit ? InitializationKind::CreateDirect(
788 Loc, Init->getBeginLoc(), Init->getEndLoc())
789 : InitializationKind::CreateDirectList(Loc))
790 : InitializationKind::CreateCopy(Loc, Init->getBeginLoc());
792 MultiExprArg Args = Init;
793 if (CXXDirectInit)
794 Args =
795 MultiExprArg(CXXDirectInit->getExprs(), CXXDirectInit->getNumExprs());
796 QualType DclT;
797 InitializationSequence InitSeq(*this, Entity, Kind, Args);
798 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT);
800 if (Result.isInvalid())
801 return QualType();
803 Init = Result.getAs<Expr>();
804 return DeducedType;
807 VarDecl *Sema::createLambdaInitCaptureVarDecl(
808 SourceLocation Loc, QualType InitCaptureType, SourceLocation EllipsisLoc,
809 IdentifierInfo *Id, unsigned InitStyle, Expr *Init, DeclContext *DeclCtx) {
810 // FIXME: Retain the TypeSourceInfo from buildLambdaInitCaptureInitialization
811 // rather than reconstructing it here.
812 TypeSourceInfo *TSI = Context.getTrivialTypeSourceInfo(InitCaptureType, Loc);
813 if (auto PETL = TSI->getTypeLoc().getAs<PackExpansionTypeLoc>())
814 PETL.setEllipsisLoc(EllipsisLoc);
816 // Create a dummy variable representing the init-capture. This is not actually
817 // used as a variable, and only exists as a way to name and refer to the
818 // init-capture.
819 // FIXME: Pass in separate source locations for '&' and identifier.
820 VarDecl *NewVD = VarDecl::Create(Context, DeclCtx, Loc, Loc, Id,
821 InitCaptureType, TSI, SC_Auto);
822 NewVD->setInitCapture(true);
823 NewVD->setReferenced(true);
824 // FIXME: Pass in a VarDecl::InitializationStyle.
825 NewVD->setInitStyle(static_cast<VarDecl::InitializationStyle>(InitStyle));
826 NewVD->markUsed(Context);
827 NewVD->setInit(Init);
828 if (NewVD->isParameterPack())
829 getCurLambda()->LocalPacks.push_back(NewVD);
830 return NewVD;
833 void Sema::addInitCapture(LambdaScopeInfo *LSI, VarDecl *Var, bool ByRef) {
834 assert(Var->isInitCapture() && "init capture flag should be set");
835 LSI->addCapture(Var, /*isBlock=*/false, ByRef,
836 /*isNested=*/false, Var->getLocation(), SourceLocation(),
837 Var->getType(), /*Invalid=*/false);
840 // Unlike getCurLambda, getCurrentLambdaScopeUnsafe doesn't
841 // check that the current lambda is in a consistent or fully constructed state.
842 static LambdaScopeInfo *getCurrentLambdaScopeUnsafe(Sema &S) {
843 assert(!S.FunctionScopes.empty());
844 return cast<LambdaScopeInfo>(S.FunctionScopes[S.FunctionScopes.size() - 1]);
847 static TypeSourceInfo *
848 getDummyLambdaType(Sema &S, SourceLocation Loc = SourceLocation()) {
849 // C++11 [expr.prim.lambda]p4:
850 // If a lambda-expression does not include a lambda-declarator, it is as
851 // if the lambda-declarator were ().
852 FunctionProtoType::ExtProtoInfo EPI(S.Context.getDefaultCallingConvention(
853 /*IsVariadic=*/false, /*IsCXXMethod=*/true));
854 EPI.HasTrailingReturn = true;
855 EPI.TypeQuals.addConst();
856 LangAS AS = S.getDefaultCXXMethodAddrSpace();
857 if (AS != LangAS::Default)
858 EPI.TypeQuals.addAddressSpace(AS);
860 // C++1y [expr.prim.lambda]:
861 // The lambda return type is 'auto', which is replaced by the
862 // trailing-return type if provided and/or deduced from 'return'
863 // statements
864 // We don't do this before C++1y, because we don't support deduced return
865 // types there.
866 QualType DefaultTypeForNoTrailingReturn = S.getLangOpts().CPlusPlus14
867 ? S.Context.getAutoDeductType()
868 : S.Context.DependentTy;
869 QualType MethodTy = S.Context.getFunctionType(DefaultTypeForNoTrailingReturn,
870 std::nullopt, EPI);
871 return S.Context.getTrivialTypeSourceInfo(MethodTy, Loc);
874 static TypeSourceInfo *getLambdaType(Sema &S, LambdaIntroducer &Intro,
875 Declarator &ParamInfo, Scope *CurScope,
876 SourceLocation Loc,
877 bool &ExplicitResultType) {
879 ExplicitResultType = false;
881 assert(
882 (ParamInfo.getDeclSpec().getStorageClassSpec() ==
883 DeclSpec::SCS_unspecified ||
884 ParamInfo.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static) &&
885 "Unexpected storage specifier");
886 bool IsLambdaStatic =
887 ParamInfo.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static;
889 TypeSourceInfo *MethodTyInfo;
891 if (ParamInfo.getNumTypeObjects() == 0) {
892 MethodTyInfo = getDummyLambdaType(S, Loc);
893 } else {
894 // Check explicit parameters
895 S.CheckExplicitObjectLambda(ParamInfo);
897 DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo();
899 bool HasExplicitObjectParameter =
900 ParamInfo.isExplicitObjectMemberFunction();
902 ExplicitResultType = FTI.hasTrailingReturnType();
903 if (!FTI.hasMutableQualifier() && !IsLambdaStatic &&
904 !HasExplicitObjectParameter)
905 FTI.getOrCreateMethodQualifiers().SetTypeQual(DeclSpec::TQ_const, Loc);
907 if (ExplicitResultType && S.getLangOpts().HLSL) {
908 QualType RetTy = FTI.getTrailingReturnType().get();
909 if (!RetTy.isNull()) {
910 // HLSL does not support specifying an address space on a lambda return
911 // type.
912 LangAS AddressSpace = RetTy.getAddressSpace();
913 if (AddressSpace != LangAS::Default)
914 S.Diag(FTI.getTrailingReturnTypeLoc(),
915 diag::err_return_value_with_address_space);
919 MethodTyInfo = S.GetTypeForDeclarator(ParamInfo, CurScope);
920 assert(MethodTyInfo && "no type from lambda-declarator");
922 // Check for unexpanded parameter packs in the method type.
923 if (MethodTyInfo->getType()->containsUnexpandedParameterPack())
924 S.DiagnoseUnexpandedParameterPack(Intro.Range.getBegin(), MethodTyInfo,
925 S.UPPC_DeclarationType);
927 return MethodTyInfo;
930 CXXMethodDecl *Sema::CreateLambdaCallOperator(SourceRange IntroducerRange,
931 CXXRecordDecl *Class) {
933 // C++20 [expr.prim.lambda.closure]p3:
934 // The closure type for a lambda-expression has a public inline function
935 // call operator (for a non-generic lambda) or function call operator
936 // template (for a generic lambda) whose parameters and return type are
937 // described by the lambda-expression's parameter-declaration-clause
938 // and trailing-return-type respectively.
939 DeclarationName MethodName =
940 Context.DeclarationNames.getCXXOperatorName(OO_Call);
941 DeclarationNameLoc MethodNameLoc =
942 DeclarationNameLoc::makeCXXOperatorNameLoc(IntroducerRange.getBegin());
943 CXXMethodDecl *Method = CXXMethodDecl::Create(
944 Context, Class, SourceLocation(),
945 DeclarationNameInfo(MethodName, IntroducerRange.getBegin(),
946 MethodNameLoc),
947 QualType(), /*Tinfo=*/nullptr, SC_None,
948 getCurFPFeatures().isFPConstrained(),
949 /*isInline=*/true, ConstexprSpecKind::Unspecified, SourceLocation(),
950 /*TrailingRequiresClause=*/nullptr);
951 Method->setAccess(AS_public);
952 return Method;
955 void Sema::AddTemplateParametersToLambdaCallOperator(
956 CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
957 TemplateParameterList *TemplateParams) {
958 assert(TemplateParams && "no template parameters");
959 FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
960 Context, Class, CallOperator->getLocation(), CallOperator->getDeclName(),
961 TemplateParams, CallOperator);
962 TemplateMethod->setAccess(AS_public);
963 CallOperator->setDescribedFunctionTemplate(TemplateMethod);
966 void Sema::CompleteLambdaCallOperator(
967 CXXMethodDecl *Method, SourceLocation LambdaLoc,
968 SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
969 TypeSourceInfo *MethodTyInfo, ConstexprSpecKind ConstexprKind,
970 StorageClass SC, ArrayRef<ParmVarDecl *> Params,
971 bool HasExplicitResultType) {
973 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(*this);
975 if (TrailingRequiresClause)
976 Method->setTrailingRequiresClause(TrailingRequiresClause);
978 TemplateParameterList *TemplateParams =
979 getGenericLambdaTemplateParameterList(LSI, *this);
981 DeclContext *DC = Method->getLexicalDeclContext();
982 Method->setLexicalDeclContext(LSI->Lambda);
983 if (TemplateParams) {
984 FunctionTemplateDecl *TemplateMethod =
985 Method->getDescribedFunctionTemplate();
986 assert(TemplateMethod &&
987 "AddTemplateParametersToLambdaCallOperator should have been called");
989 LSI->Lambda->addDecl(TemplateMethod);
990 TemplateMethod->setLexicalDeclContext(DC);
991 } else {
992 LSI->Lambda->addDecl(Method);
994 LSI->Lambda->setLambdaIsGeneric(TemplateParams);
995 LSI->Lambda->setLambdaTypeInfo(MethodTyInfo);
997 Method->setLexicalDeclContext(DC);
998 Method->setLocation(LambdaLoc);
999 Method->setInnerLocStart(CallOperatorLoc);
1000 Method->setTypeSourceInfo(MethodTyInfo);
1001 Method->setType(buildTypeForLambdaCallOperator(*this, LSI->Lambda,
1002 TemplateParams, MethodTyInfo));
1003 Method->setConstexprKind(ConstexprKind);
1004 Method->setStorageClass(SC);
1005 if (!Params.empty()) {
1006 CheckParmsForFunctionDef(Params, /*CheckParameterNames=*/false);
1007 Method->setParams(Params);
1008 for (auto P : Method->parameters()) {
1009 assert(P && "null in a parameter list");
1010 P->setOwningFunction(Method);
1014 buildLambdaScopeReturnType(*this, LSI, Method, HasExplicitResultType);
1017 void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
1018 Scope *CurrentScope) {
1020 LambdaScopeInfo *LSI = getCurLambda();
1021 assert(LSI && "LambdaScopeInfo should be on stack!");
1023 if (Intro.Default == LCD_ByCopy)
1024 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval;
1025 else if (Intro.Default == LCD_ByRef)
1026 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref;
1027 LSI->CaptureDefaultLoc = Intro.DefaultLoc;
1028 LSI->IntroducerRange = Intro.Range;
1029 LSI->AfterParameterList = false;
1031 assert(LSI->NumExplicitTemplateParams == 0);
1033 // Determine if we're within a context where we know that the lambda will
1034 // be dependent, because there are template parameters in scope.
1035 CXXRecordDecl::LambdaDependencyKind LambdaDependencyKind =
1036 CXXRecordDecl::LDK_Unknown;
1037 if (LSI->NumExplicitTemplateParams > 0) {
1038 Scope *TemplateParamScope = CurScope->getTemplateParamParent();
1039 assert(TemplateParamScope &&
1040 "Lambda with explicit template param list should establish a "
1041 "template param scope");
1042 assert(TemplateParamScope->getParent());
1043 if (TemplateParamScope->getParent()->getTemplateParamParent() != nullptr)
1044 LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent;
1045 } else if (CurScope->getTemplateParamParent() != nullptr) {
1046 LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent;
1049 CXXRecordDecl *Class = createLambdaClosureType(
1050 Intro.Range, /*Info=*/nullptr, LambdaDependencyKind, Intro.Default);
1051 LSI->Lambda = Class;
1053 CXXMethodDecl *Method = CreateLambdaCallOperator(Intro.Range, Class);
1054 LSI->CallOperator = Method;
1055 Method->setLexicalDeclContext(CurContext);
1057 PushDeclContext(CurScope, Method);
1059 bool ContainsUnexpandedParameterPack = false;
1061 // Distinct capture names, for diagnostics.
1062 llvm::DenseMap<IdentifierInfo *, ValueDecl *> CaptureNames;
1064 // Handle explicit captures.
1065 SourceLocation PrevCaptureLoc =
1066 Intro.Default == LCD_None ? Intro.Range.getBegin() : Intro.DefaultLoc;
1067 for (auto C = Intro.Captures.begin(), E = Intro.Captures.end(); C != E;
1068 PrevCaptureLoc = C->Loc, ++C) {
1069 if (C->Kind == LCK_This || C->Kind == LCK_StarThis) {
1070 if (C->Kind == LCK_StarThis)
1071 Diag(C->Loc, !getLangOpts().CPlusPlus17
1072 ? diag::ext_star_this_lambda_capture_cxx17
1073 : diag::warn_cxx14_compat_star_this_lambda_capture);
1075 // C++11 [expr.prim.lambda]p8:
1076 // An identifier or this shall not appear more than once in a
1077 // lambda-capture.
1078 if (LSI->isCXXThisCaptured()) {
1079 Diag(C->Loc, diag::err_capture_more_than_once)
1080 << "'this'" << SourceRange(LSI->getCXXThisCapture().getLocation())
1081 << FixItHint::CreateRemoval(
1082 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1083 continue;
1086 // C++20 [expr.prim.lambda]p8:
1087 // If a lambda-capture includes a capture-default that is =,
1088 // each simple-capture of that lambda-capture shall be of the form
1089 // "&identifier", "this", or "* this". [ Note: The form [&,this] is
1090 // redundant but accepted for compatibility with ISO C++14. --end note ]
1091 if (Intro.Default == LCD_ByCopy && C->Kind != LCK_StarThis)
1092 Diag(C->Loc, !getLangOpts().CPlusPlus20
1093 ? diag::ext_equals_this_lambda_capture_cxx20
1094 : diag::warn_cxx17_compat_equals_this_lambda_capture);
1096 // C++11 [expr.prim.lambda]p12:
1097 // If this is captured by a local lambda expression, its nearest
1098 // enclosing function shall be a non-static member function.
1099 QualType ThisCaptureType = getCurrentThisType();
1100 if (ThisCaptureType.isNull()) {
1101 Diag(C->Loc, diag::err_this_capture) << true;
1102 continue;
1105 CheckCXXThisCapture(C->Loc, /*Explicit=*/true, /*BuildAndDiagnose*/ true,
1106 /*FunctionScopeIndexToStopAtPtr*/ nullptr,
1107 C->Kind == LCK_StarThis);
1108 if (!LSI->Captures.empty())
1109 LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
1110 continue;
1113 assert(C->Id && "missing identifier for capture");
1115 if (C->Init.isInvalid())
1116 continue;
1118 ValueDecl *Var = nullptr;
1119 if (C->Init.isUsable()) {
1120 Diag(C->Loc, getLangOpts().CPlusPlus14
1121 ? diag::warn_cxx11_compat_init_capture
1122 : diag::ext_init_capture);
1124 // If the initializer expression is usable, but the InitCaptureType
1125 // is not, then an error has occurred - so ignore the capture for now.
1126 // for e.g., [n{0}] { }; <-- if no <initializer_list> is included.
1127 // FIXME: we should create the init capture variable and mark it invalid
1128 // in this case.
1129 if (C->InitCaptureType.get().isNull())
1130 continue;
1132 if (C->Init.get()->containsUnexpandedParameterPack() &&
1133 !C->InitCaptureType.get()->getAs<PackExpansionType>())
1134 DiagnoseUnexpandedParameterPack(C->Init.get(), UPPC_Initializer);
1136 unsigned InitStyle;
1137 switch (C->InitKind) {
1138 case LambdaCaptureInitKind::NoInit:
1139 llvm_unreachable("not an init-capture?");
1140 case LambdaCaptureInitKind::CopyInit:
1141 InitStyle = VarDecl::CInit;
1142 break;
1143 case LambdaCaptureInitKind::DirectInit:
1144 InitStyle = VarDecl::CallInit;
1145 break;
1146 case LambdaCaptureInitKind::ListInit:
1147 InitStyle = VarDecl::ListInit;
1148 break;
1150 Var = createLambdaInitCaptureVarDecl(C->Loc, C->InitCaptureType.get(),
1151 C->EllipsisLoc, C->Id, InitStyle,
1152 C->Init.get(), Method);
1153 assert(Var && "createLambdaInitCaptureVarDecl returned a null VarDecl?");
1154 if (auto *V = dyn_cast<VarDecl>(Var))
1155 CheckShadow(CurrentScope, V);
1156 PushOnScopeChains(Var, CurrentScope, false);
1157 } else {
1158 assert(C->InitKind == LambdaCaptureInitKind::NoInit &&
1159 "init capture has valid but null init?");
1161 // C++11 [expr.prim.lambda]p8:
1162 // If a lambda-capture includes a capture-default that is &, the
1163 // identifiers in the lambda-capture shall not be preceded by &.
1164 // If a lambda-capture includes a capture-default that is =, [...]
1165 // each identifier it contains shall be preceded by &.
1166 if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) {
1167 Diag(C->Loc, diag::err_reference_capture_with_reference_default)
1168 << FixItHint::CreateRemoval(
1169 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1170 continue;
1171 } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) {
1172 Diag(C->Loc, diag::err_copy_capture_with_copy_default)
1173 << FixItHint::CreateRemoval(
1174 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1175 continue;
1178 // C++11 [expr.prim.lambda]p10:
1179 // The identifiers in a capture-list are looked up using the usual
1180 // rules for unqualified name lookup (3.4.1)
1181 DeclarationNameInfo Name(C->Id, C->Loc);
1182 LookupResult R(*this, Name, LookupOrdinaryName);
1183 LookupName(R, CurScope);
1184 if (R.isAmbiguous())
1185 continue;
1186 if (R.empty()) {
1187 // FIXME: Disable corrections that would add qualification?
1188 CXXScopeSpec ScopeSpec;
1189 DeclFilterCCC<VarDecl> Validator{};
1190 if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator))
1191 continue;
1194 if (auto *BD = R.getAsSingle<BindingDecl>())
1195 Var = BD;
1196 else
1197 Var = R.getAsSingle<VarDecl>();
1198 if (Var && DiagnoseUseOfDecl(Var, C->Loc))
1199 continue;
1202 // C++11 [expr.prim.lambda]p10:
1203 // [...] each such lookup shall find a variable with automatic storage
1204 // duration declared in the reaching scope of the local lambda expression.
1205 // Note that the 'reaching scope' check happens in tryCaptureVariable().
1206 if (!Var) {
1207 Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id;
1208 continue;
1211 // C++11 [expr.prim.lambda]p8:
1212 // An identifier or this shall not appear more than once in a
1213 // lambda-capture.
1214 if (auto [It, Inserted] = CaptureNames.insert(std::pair{C->Id, Var});
1215 !Inserted) {
1216 if (C->InitKind == LambdaCaptureInitKind::NoInit &&
1217 !Var->isInitCapture()) {
1218 Diag(C->Loc, diag::err_capture_more_than_once)
1219 << C->Id << It->second->getBeginLoc()
1220 << FixItHint::CreateRemoval(
1221 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1222 Var->setInvalidDecl();
1223 } else if (Var && Var->isPlaceholderVar(getLangOpts())) {
1224 DiagPlaceholderVariableDefinition(C->Loc);
1225 } else {
1226 // Previous capture captured something different (one or both was
1227 // an init-capture): no fixit.
1228 Diag(C->Loc, diag::err_capture_more_than_once) << C->Id;
1229 continue;
1233 // Ignore invalid decls; they'll just confuse the code later.
1234 if (Var->isInvalidDecl())
1235 continue;
1237 VarDecl *Underlying = Var->getPotentiallyDecomposedVarDecl();
1239 if (!Underlying->hasLocalStorage()) {
1240 Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id;
1241 Diag(Var->getLocation(), diag::note_previous_decl) << C->Id;
1242 continue;
1245 // C++11 [expr.prim.lambda]p23:
1246 // A capture followed by an ellipsis is a pack expansion (14.5.3).
1247 SourceLocation EllipsisLoc;
1248 if (C->EllipsisLoc.isValid()) {
1249 if (Var->isParameterPack()) {
1250 EllipsisLoc = C->EllipsisLoc;
1251 } else {
1252 Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
1253 << (C->Init.isUsable() ? C->Init.get()->getSourceRange()
1254 : SourceRange(C->Loc));
1256 // Just ignore the ellipsis.
1258 } else if (Var->isParameterPack()) {
1259 ContainsUnexpandedParameterPack = true;
1262 if (C->Init.isUsable()) {
1263 addInitCapture(LSI, cast<VarDecl>(Var), C->Kind == LCK_ByRef);
1264 PushOnScopeChains(Var, CurScope, false);
1265 } else {
1266 TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef
1267 : TryCapture_ExplicitByVal;
1268 tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc);
1270 if (!LSI->Captures.empty())
1271 LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
1273 finishLambdaExplicitCaptures(LSI);
1274 LSI->ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
1275 PopDeclContext();
1278 void Sema::ActOnLambdaClosureQualifiers(LambdaIntroducer &Intro,
1279 SourceLocation MutableLoc) {
1281 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(*this);
1282 LSI->Mutable = MutableLoc.isValid();
1283 ContextRAII Context(*this, LSI->CallOperator, /*NewThisContext*/ false);
1285 // C++11 [expr.prim.lambda]p9:
1286 // A lambda-expression whose smallest enclosing scope is a block scope is a
1287 // local lambda expression; any other lambda expression shall not have a
1288 // capture-default or simple-capture in its lambda-introducer.
1290 // For simple-captures, this is covered by the check below that any named
1291 // entity is a variable that can be captured.
1293 // For DR1632, we also allow a capture-default in any context where we can
1294 // odr-use 'this' (in particular, in a default initializer for a non-static
1295 // data member).
1296 if (Intro.Default != LCD_None &&
1297 !LSI->Lambda->getParent()->isFunctionOrMethod() &&
1298 (getCurrentThisType().isNull() ||
1299 CheckCXXThisCapture(SourceLocation(), /*Explicit=*/true,
1300 /*BuildAndDiagnose=*/false)))
1301 Diag(Intro.DefaultLoc, diag::err_capture_default_non_local);
1304 void Sema::ActOnLambdaClosureParameters(
1305 Scope *LambdaScope, MutableArrayRef<DeclaratorChunk::ParamInfo> Params) {
1306 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(*this);
1307 PushDeclContext(LambdaScope, LSI->CallOperator);
1309 for (const DeclaratorChunk::ParamInfo &P : Params) {
1310 auto *Param = cast<ParmVarDecl>(P.Param);
1311 Param->setOwningFunction(LSI->CallOperator);
1312 if (Param->getIdentifier())
1313 PushOnScopeChains(Param, LambdaScope, false);
1316 // After the parameter list, we may parse a noexcept/requires/trailing return
1317 // type which need to know whether the call operator constiture a dependent
1318 // context, so we need to setup the FunctionTemplateDecl of generic lambdas
1319 // now.
1320 TemplateParameterList *TemplateParams =
1321 getGenericLambdaTemplateParameterList(LSI, *this);
1322 if (TemplateParams) {
1323 AddTemplateParametersToLambdaCallOperator(LSI->CallOperator, LSI->Lambda,
1324 TemplateParams);
1325 LSI->Lambda->setLambdaIsGeneric(true);
1327 LSI->AfterParameterList = true;
1330 void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
1331 Declarator &ParamInfo,
1332 const DeclSpec &DS) {
1334 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(*this);
1335 LSI->CallOperator->setConstexprKind(DS.getConstexprSpecifier());
1337 SmallVector<ParmVarDecl *, 8> Params;
1338 bool ExplicitResultType;
1340 SourceLocation TypeLoc, CallOperatorLoc;
1341 if (ParamInfo.getNumTypeObjects() == 0) {
1342 CallOperatorLoc = TypeLoc = Intro.Range.getEnd();
1343 } else {
1344 unsigned Index;
1345 ParamInfo.isFunctionDeclarator(Index);
1346 const auto &Object = ParamInfo.getTypeObject(Index);
1347 TypeLoc =
1348 Object.Loc.isValid() ? Object.Loc : ParamInfo.getSourceRange().getEnd();
1349 CallOperatorLoc = ParamInfo.getSourceRange().getEnd();
1352 CXXRecordDecl *Class = LSI->Lambda;
1353 CXXMethodDecl *Method = LSI->CallOperator;
1355 TypeSourceInfo *MethodTyInfo = getLambdaType(
1356 *this, Intro, ParamInfo, getCurScope(), TypeLoc, ExplicitResultType);
1358 LSI->ExplicitParams = ParamInfo.getNumTypeObjects() != 0;
1360 if (ParamInfo.isFunctionDeclarator() != 0 &&
1361 !FTIHasSingleVoidParameter(ParamInfo.getFunctionTypeInfo())) {
1362 const auto &FTI = ParamInfo.getFunctionTypeInfo();
1363 Params.reserve(Params.size());
1364 for (unsigned I = 0; I < FTI.NumParams; ++I) {
1365 auto *Param = cast<ParmVarDecl>(FTI.Params[I].Param);
1366 Param->setScopeInfo(0, Params.size());
1367 Params.push_back(Param);
1371 bool IsLambdaStatic =
1372 ParamInfo.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static;
1374 CompleteLambdaCallOperator(
1375 Method, Intro.Range.getBegin(), CallOperatorLoc,
1376 ParamInfo.getTrailingRequiresClause(), MethodTyInfo,
1377 ParamInfo.getDeclSpec().getConstexprSpecifier(),
1378 IsLambdaStatic ? SC_Static : SC_None, Params, ExplicitResultType);
1380 CheckCXXDefaultArguments(Method);
1382 // This represents the function body for the lambda function, check if we
1383 // have to apply optnone due to a pragma.
1384 AddRangeBasedOptnone(Method);
1386 // code_seg attribute on lambda apply to the method.
1387 if (Attr *A = getImplicitCodeSegOrSectionAttrForFunction(
1388 Method, /*IsDefinition=*/true))
1389 Method->addAttr(A);
1391 // Attributes on the lambda apply to the method.
1392 ProcessDeclAttributes(CurScope, Method, ParamInfo);
1394 // CUDA lambdas get implicit host and device attributes.
1395 if (getLangOpts().CUDA)
1396 CUDASetLambdaAttrs(Method);
1398 // OpenMP lambdas might get assumumption attributes.
1399 if (LangOpts.OpenMP)
1400 ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Method);
1402 handleLambdaNumbering(Class, Method);
1404 for (auto &&C : LSI->Captures) {
1405 if (!C.isVariableCapture())
1406 continue;
1407 ValueDecl *Var = C.getVariable();
1408 if (Var && Var->isInitCapture()) {
1409 PushOnScopeChains(Var, CurScope, false);
1413 auto CheckRedefinition = [&](ParmVarDecl *Param) {
1414 for (const auto &Capture : Intro.Captures) {
1415 if (Capture.Id == Param->getIdentifier()) {
1416 Diag(Param->getLocation(), diag::err_parameter_shadow_capture);
1417 Diag(Capture.Loc, diag::note_var_explicitly_captured_here)
1418 << Capture.Id << true;
1419 return false;
1422 return true;
1425 for (ParmVarDecl *P : Params) {
1426 if (!P->getIdentifier())
1427 continue;
1428 if (CheckRedefinition(P))
1429 CheckShadow(CurScope, P);
1430 PushOnScopeChains(P, CurScope);
1433 // C++23 [expr.prim.lambda.capture]p5:
1434 // If an identifier in a capture appears as the declarator-id of a parameter
1435 // of the lambda-declarator's parameter-declaration-clause or as the name of a
1436 // template parameter of the lambda-expression's template-parameter-list, the
1437 // program is ill-formed.
1438 TemplateParameterList *TemplateParams =
1439 getGenericLambdaTemplateParameterList(LSI, *this);
1440 if (TemplateParams) {
1441 for (const auto *TP : TemplateParams->asArray()) {
1442 if (!TP->getIdentifier())
1443 continue;
1444 for (const auto &Capture : Intro.Captures) {
1445 if (Capture.Id == TP->getIdentifier()) {
1446 Diag(Capture.Loc, diag::err_template_param_shadow) << Capture.Id;
1447 Diag(TP->getLocation(), diag::note_template_param_here);
1453 // C++20: dcl.decl.general p4:
1454 // The optional requires-clause ([temp.pre]) in an init-declarator or
1455 // member-declarator shall be present only if the declarator declares a
1456 // templated function ([dcl.fct]).
1457 if (Expr *TRC = Method->getTrailingRequiresClause()) {
1458 // [temp.pre]/8:
1459 // An entity is templated if it is
1460 // - a template,
1461 // - an entity defined ([basic.def]) or created ([class.temporary]) in a
1462 // templated entity,
1463 // - a member of a templated entity,
1464 // - an enumerator for an enumeration that is a templated entity, or
1465 // - the closure type of a lambda-expression ([expr.prim.lambda.closure])
1466 // appearing in the declaration of a templated entity. [Note 6: A local
1467 // class, a local or block variable, or a friend function defined in a
1468 // templated entity is a templated entity. — end note]
1470 // A templated function is a function template or a function that is
1471 // templated. A templated class is a class template or a class that is
1472 // templated. A templated variable is a variable template or a variable
1473 // that is templated.
1475 // Note: we only have to check if this is defined in a template entity, OR
1476 // if we are a template, since the rest don't apply. The requires clause
1477 // applies to the call operator, which we already know is a member function,
1478 // AND defined.
1479 if (!Method->getDescribedFunctionTemplate() && !Method->isTemplated()) {
1480 Diag(TRC->getBeginLoc(), diag::err_constrained_non_templated_function);
1484 // Enter a new evaluation context to insulate the lambda from any
1485 // cleanups from the enclosing full-expression.
1486 PushExpressionEvaluationContext(
1487 LSI->CallOperator->isConsteval()
1488 ? ExpressionEvaluationContext::ImmediateFunctionContext
1489 : ExpressionEvaluationContext::PotentiallyEvaluated);
1490 ExprEvalContexts.back().InImmediateFunctionContext =
1491 LSI->CallOperator->isConsteval();
1492 ExprEvalContexts.back().InImmediateEscalatingFunctionContext =
1493 getLangOpts().CPlusPlus20 && LSI->CallOperator->isImmediateEscalating();
1496 void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope,
1497 bool IsInstantiation) {
1498 LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(FunctionScopes.back());
1500 // Leave the expression-evaluation context.
1501 DiscardCleanupsInEvaluationContext();
1502 PopExpressionEvaluationContext();
1504 // Leave the context of the lambda.
1505 if (!IsInstantiation)
1506 PopDeclContext();
1508 // Finalize the lambda.
1509 CXXRecordDecl *Class = LSI->Lambda;
1510 Class->setInvalidDecl();
1511 SmallVector<Decl*, 4> Fields(Class->fields());
1512 ActOnFields(nullptr, Class->getLocation(), Class, Fields, SourceLocation(),
1513 SourceLocation(), ParsedAttributesView());
1514 CheckCompletedCXXClass(nullptr, Class);
1516 PopFunctionScopeInfo();
1519 template <typename Func>
1520 static void repeatForLambdaConversionFunctionCallingConvs(
1521 Sema &S, const FunctionProtoType &CallOpProto, Func F) {
1522 CallingConv DefaultFree = S.Context.getDefaultCallingConvention(
1523 CallOpProto.isVariadic(), /*IsCXXMethod=*/false);
1524 CallingConv DefaultMember = S.Context.getDefaultCallingConvention(
1525 CallOpProto.isVariadic(), /*IsCXXMethod=*/true);
1526 CallingConv CallOpCC = CallOpProto.getCallConv();
1528 /// Implement emitting a version of the operator for many of the calling
1529 /// conventions for MSVC, as described here:
1530 /// https://devblogs.microsoft.com/oldnewthing/20150220-00/?p=44623.
1531 /// Experimentally, we determined that cdecl, stdcall, fastcall, and
1532 /// vectorcall are generated by MSVC when it is supported by the target.
1533 /// Additionally, we are ensuring that the default-free/default-member and
1534 /// call-operator calling convention are generated as well.
1535 /// NOTE: We intentionally generate a 'thiscall' on Win32 implicitly from the
1536 /// 'member default', despite MSVC not doing so. We do this in order to ensure
1537 /// that someone who intentionally places 'thiscall' on the lambda call
1538 /// operator will still get that overload, since we don't have the a way of
1539 /// detecting the attribute by the time we get here.
1540 if (S.getLangOpts().MSVCCompat) {
1541 CallingConv Convs[] = {
1542 CC_C, CC_X86StdCall, CC_X86FastCall, CC_X86VectorCall,
1543 DefaultFree, DefaultMember, CallOpCC};
1544 llvm::sort(Convs);
1545 llvm::iterator_range<CallingConv *> Range(
1546 std::begin(Convs), std::unique(std::begin(Convs), std::end(Convs)));
1547 const TargetInfo &TI = S.getASTContext().getTargetInfo();
1549 for (CallingConv C : Range) {
1550 if (TI.checkCallingConvention(C) == TargetInfo::CCCR_OK)
1551 F(C);
1553 return;
1556 if (CallOpCC == DefaultMember && DefaultMember != DefaultFree) {
1557 F(DefaultFree);
1558 F(DefaultMember);
1559 } else {
1560 F(CallOpCC);
1564 // Returns the 'standard' calling convention to be used for the lambda
1565 // conversion function, that is, the 'free' function calling convention unless
1566 // it is overridden by a non-default calling convention attribute.
1567 static CallingConv
1568 getLambdaConversionFunctionCallConv(Sema &S,
1569 const FunctionProtoType *CallOpProto) {
1570 CallingConv DefaultFree = S.Context.getDefaultCallingConvention(
1571 CallOpProto->isVariadic(), /*IsCXXMethod=*/false);
1572 CallingConv DefaultMember = S.Context.getDefaultCallingConvention(
1573 CallOpProto->isVariadic(), /*IsCXXMethod=*/true);
1574 CallingConv CallOpCC = CallOpProto->getCallConv();
1576 // If the call-operator hasn't been changed, return both the 'free' and
1577 // 'member' function calling convention.
1578 if (CallOpCC == DefaultMember && DefaultMember != DefaultFree)
1579 return DefaultFree;
1580 return CallOpCC;
1583 QualType Sema::getLambdaConversionFunctionResultType(
1584 const FunctionProtoType *CallOpProto, CallingConv CC) {
1585 const FunctionProtoType::ExtProtoInfo CallOpExtInfo =
1586 CallOpProto->getExtProtoInfo();
1587 FunctionProtoType::ExtProtoInfo InvokerExtInfo = CallOpExtInfo;
1588 InvokerExtInfo.ExtInfo = InvokerExtInfo.ExtInfo.withCallingConv(CC);
1589 InvokerExtInfo.TypeQuals = Qualifiers();
1590 assert(InvokerExtInfo.RefQualifier == RQ_None &&
1591 "Lambda's call operator should not have a reference qualifier");
1592 return Context.getFunctionType(CallOpProto->getReturnType(),
1593 CallOpProto->getParamTypes(), InvokerExtInfo);
1596 /// Add a lambda's conversion to function pointer, as described in
1597 /// C++11 [expr.prim.lambda]p6.
1598 static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
1599 CXXRecordDecl *Class,
1600 CXXMethodDecl *CallOperator,
1601 QualType InvokerFunctionTy) {
1602 // This conversion is explicitly disabled if the lambda's function has
1603 // pass_object_size attributes on any of its parameters.
1604 auto HasPassObjectSizeAttr = [](const ParmVarDecl *P) {
1605 return P->hasAttr<PassObjectSizeAttr>();
1607 if (llvm::any_of(CallOperator->parameters(), HasPassObjectSizeAttr))
1608 return;
1610 // Add the conversion to function pointer.
1611 QualType PtrToFunctionTy = S.Context.getPointerType(InvokerFunctionTy);
1613 // Create the type of the conversion function.
1614 FunctionProtoType::ExtProtoInfo ConvExtInfo(
1615 S.Context.getDefaultCallingConvention(
1616 /*IsVariadic=*/false, /*IsCXXMethod=*/true));
1617 // The conversion function is always const and noexcept.
1618 ConvExtInfo.TypeQuals = Qualifiers();
1619 ConvExtInfo.TypeQuals.addConst();
1620 ConvExtInfo.ExceptionSpec.Type = EST_BasicNoexcept;
1621 QualType ConvTy =
1622 S.Context.getFunctionType(PtrToFunctionTy, std::nullopt, ConvExtInfo);
1624 SourceLocation Loc = IntroducerRange.getBegin();
1625 DeclarationName ConversionName
1626 = S.Context.DeclarationNames.getCXXConversionFunctionName(
1627 S.Context.getCanonicalType(PtrToFunctionTy));
1628 // Construct a TypeSourceInfo for the conversion function, and wire
1629 // all the parameters appropriately for the FunctionProtoTypeLoc
1630 // so that everything works during transformation/instantiation of
1631 // generic lambdas.
1632 // The main reason for wiring up the parameters of the conversion
1633 // function with that of the call operator is so that constructs
1634 // like the following work:
1635 // auto L = [](auto b) { <-- 1
1636 // return [](auto a) -> decltype(a) { <-- 2
1637 // return a;
1638 // };
1639 // };
1640 // int (*fp)(int) = L(5);
1641 // Because the trailing return type can contain DeclRefExprs that refer
1642 // to the original call operator's variables, we hijack the call
1643 // operators ParmVarDecls below.
1644 TypeSourceInfo *ConvNamePtrToFunctionTSI =
1645 S.Context.getTrivialTypeSourceInfo(PtrToFunctionTy, Loc);
1646 DeclarationNameLoc ConvNameLoc =
1647 DeclarationNameLoc::makeNamedTypeLoc(ConvNamePtrToFunctionTSI);
1649 // The conversion function is a conversion to a pointer-to-function.
1650 TypeSourceInfo *ConvTSI = S.Context.getTrivialTypeSourceInfo(ConvTy, Loc);
1651 FunctionProtoTypeLoc ConvTL =
1652 ConvTSI->getTypeLoc().getAs<FunctionProtoTypeLoc>();
1653 // Get the result of the conversion function which is a pointer-to-function.
1654 PointerTypeLoc PtrToFunctionTL =
1655 ConvTL.getReturnLoc().getAs<PointerTypeLoc>();
1656 // Do the same for the TypeSourceInfo that is used to name the conversion
1657 // operator.
1658 PointerTypeLoc ConvNamePtrToFunctionTL =
1659 ConvNamePtrToFunctionTSI->getTypeLoc().getAs<PointerTypeLoc>();
1661 // Get the underlying function types that the conversion function will
1662 // be converting to (should match the type of the call operator).
1663 FunctionProtoTypeLoc CallOpConvTL =
1664 PtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>();
1665 FunctionProtoTypeLoc CallOpConvNameTL =
1666 ConvNamePtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>();
1668 // Wire up the FunctionProtoTypeLocs with the call operator's parameters.
1669 // These parameter's are essentially used to transform the name and
1670 // the type of the conversion operator. By using the same parameters
1671 // as the call operator's we don't have to fix any back references that
1672 // the trailing return type of the call operator's uses (such as
1673 // decltype(some_type<decltype(a)>::type{} + decltype(a){}) etc.)
1674 // - we can simply use the return type of the call operator, and
1675 // everything should work.
1676 SmallVector<ParmVarDecl *, 4> InvokerParams;
1677 for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) {
1678 ParmVarDecl *From = CallOperator->getParamDecl(I);
1680 InvokerParams.push_back(ParmVarDecl::Create(
1681 S.Context,
1682 // Temporarily add to the TU. This is set to the invoker below.
1683 S.Context.getTranslationUnitDecl(), From->getBeginLoc(),
1684 From->getLocation(), From->getIdentifier(), From->getType(),
1685 From->getTypeSourceInfo(), From->getStorageClass(),
1686 /*DefArg=*/nullptr));
1687 CallOpConvTL.setParam(I, From);
1688 CallOpConvNameTL.setParam(I, From);
1691 CXXConversionDecl *Conversion = CXXConversionDecl::Create(
1692 S.Context, Class, Loc,
1693 DeclarationNameInfo(ConversionName, Loc, ConvNameLoc), ConvTy, ConvTSI,
1694 S.getCurFPFeatures().isFPConstrained(),
1695 /*isInline=*/true, ExplicitSpecifier(),
1696 S.getLangOpts().CPlusPlus17 ? ConstexprSpecKind::Constexpr
1697 : ConstexprSpecKind::Unspecified,
1698 CallOperator->getBody()->getEndLoc());
1699 Conversion->setAccess(AS_public);
1700 Conversion->setImplicit(true);
1702 // A non-generic lambda may still be a templated entity. We need to preserve
1703 // constraints when converting the lambda to a function pointer. See GH63181.
1704 if (Expr *Requires = CallOperator->getTrailingRequiresClause())
1705 Conversion->setTrailingRequiresClause(Requires);
1707 if (Class->isGenericLambda()) {
1708 // Create a template version of the conversion operator, using the template
1709 // parameter list of the function call operator.
1710 FunctionTemplateDecl *TemplateCallOperator =
1711 CallOperator->getDescribedFunctionTemplate();
1712 FunctionTemplateDecl *ConversionTemplate =
1713 FunctionTemplateDecl::Create(S.Context, Class,
1714 Loc, ConversionName,
1715 TemplateCallOperator->getTemplateParameters(),
1716 Conversion);
1717 ConversionTemplate->setAccess(AS_public);
1718 ConversionTemplate->setImplicit(true);
1719 Conversion->setDescribedFunctionTemplate(ConversionTemplate);
1720 Class->addDecl(ConversionTemplate);
1721 } else
1722 Class->addDecl(Conversion);
1724 // If the lambda is not static, we need to add a static member
1725 // function that will be the result of the conversion with a
1726 // certain unique ID.
1727 // When it is static we just return the static call operator instead.
1728 if (CallOperator->isImplicitObjectMemberFunction()) {
1729 DeclarationName InvokerName =
1730 &S.Context.Idents.get(getLambdaStaticInvokerName());
1731 // FIXME: Instead of passing in the CallOperator->getTypeSourceInfo()
1732 // we should get a prebuilt TrivialTypeSourceInfo from Context
1733 // using FunctionTy & Loc and get its TypeLoc as a FunctionProtoTypeLoc
1734 // then rewire the parameters accordingly, by hoisting up the InvokeParams
1735 // loop below and then use its Params to set Invoke->setParams(...) below.
1736 // This would avoid the 'const' qualifier of the calloperator from
1737 // contaminating the type of the invoker, which is currently adjusted
1738 // in SemaTemplateDeduction.cpp:DeduceTemplateArguments. Fixing the
1739 // trailing return type of the invoker would require a visitor to rebuild
1740 // the trailing return type and adjusting all back DeclRefExpr's to refer
1741 // to the new static invoker parameters - not the call operator's.
1742 CXXMethodDecl *Invoke = CXXMethodDecl::Create(
1743 S.Context, Class, Loc, DeclarationNameInfo(InvokerName, Loc),
1744 InvokerFunctionTy, CallOperator->getTypeSourceInfo(), SC_Static,
1745 S.getCurFPFeatures().isFPConstrained(),
1746 /*isInline=*/true, CallOperator->getConstexprKind(),
1747 CallOperator->getBody()->getEndLoc());
1748 for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I)
1749 InvokerParams[I]->setOwningFunction(Invoke);
1750 Invoke->setParams(InvokerParams);
1751 Invoke->setAccess(AS_private);
1752 Invoke->setImplicit(true);
1753 if (Class->isGenericLambda()) {
1754 FunctionTemplateDecl *TemplateCallOperator =
1755 CallOperator->getDescribedFunctionTemplate();
1756 FunctionTemplateDecl *StaticInvokerTemplate =
1757 FunctionTemplateDecl::Create(
1758 S.Context, Class, Loc, InvokerName,
1759 TemplateCallOperator->getTemplateParameters(), Invoke);
1760 StaticInvokerTemplate->setAccess(AS_private);
1761 StaticInvokerTemplate->setImplicit(true);
1762 Invoke->setDescribedFunctionTemplate(StaticInvokerTemplate);
1763 Class->addDecl(StaticInvokerTemplate);
1764 } else
1765 Class->addDecl(Invoke);
1769 /// Add a lambda's conversion to function pointers, as described in
1770 /// C++11 [expr.prim.lambda]p6. Note that in most cases, this should emit only a
1771 /// single pointer conversion. In the event that the default calling convention
1772 /// for free and member functions is different, it will emit both conventions.
1773 static void addFunctionPointerConversions(Sema &S, SourceRange IntroducerRange,
1774 CXXRecordDecl *Class,
1775 CXXMethodDecl *CallOperator) {
1776 const FunctionProtoType *CallOpProto =
1777 CallOperator->getType()->castAs<FunctionProtoType>();
1779 repeatForLambdaConversionFunctionCallingConvs(
1780 S, *CallOpProto, [&](CallingConv CC) {
1781 QualType InvokerFunctionTy =
1782 S.getLambdaConversionFunctionResultType(CallOpProto, CC);
1783 addFunctionPointerConversion(S, IntroducerRange, Class, CallOperator,
1784 InvokerFunctionTy);
1788 /// Add a lambda's conversion to block pointer.
1789 static void addBlockPointerConversion(Sema &S,
1790 SourceRange IntroducerRange,
1791 CXXRecordDecl *Class,
1792 CXXMethodDecl *CallOperator) {
1793 const FunctionProtoType *CallOpProto =
1794 CallOperator->getType()->castAs<FunctionProtoType>();
1795 QualType FunctionTy = S.getLambdaConversionFunctionResultType(
1796 CallOpProto, getLambdaConversionFunctionCallConv(S, CallOpProto));
1797 QualType BlockPtrTy = S.Context.getBlockPointerType(FunctionTy);
1799 FunctionProtoType::ExtProtoInfo ConversionEPI(
1800 S.Context.getDefaultCallingConvention(
1801 /*IsVariadic=*/false, /*IsCXXMethod=*/true));
1802 ConversionEPI.TypeQuals = Qualifiers();
1803 ConversionEPI.TypeQuals.addConst();
1804 QualType ConvTy =
1805 S.Context.getFunctionType(BlockPtrTy, std::nullopt, ConversionEPI);
1807 SourceLocation Loc = IntroducerRange.getBegin();
1808 DeclarationName Name
1809 = S.Context.DeclarationNames.getCXXConversionFunctionName(
1810 S.Context.getCanonicalType(BlockPtrTy));
1811 DeclarationNameLoc NameLoc = DeclarationNameLoc::makeNamedTypeLoc(
1812 S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc));
1813 CXXConversionDecl *Conversion = CXXConversionDecl::Create(
1814 S.Context, Class, Loc, DeclarationNameInfo(Name, Loc, NameLoc), ConvTy,
1815 S.Context.getTrivialTypeSourceInfo(ConvTy, Loc),
1816 S.getCurFPFeatures().isFPConstrained(),
1817 /*isInline=*/true, ExplicitSpecifier(), ConstexprSpecKind::Unspecified,
1818 CallOperator->getBody()->getEndLoc());
1819 Conversion->setAccess(AS_public);
1820 Conversion->setImplicit(true);
1821 Class->addDecl(Conversion);
1824 ExprResult Sema::BuildCaptureInit(const Capture &Cap,
1825 SourceLocation ImplicitCaptureLoc,
1826 bool IsOpenMPMapping) {
1827 // VLA captures don't have a stored initialization expression.
1828 if (Cap.isVLATypeCapture())
1829 return ExprResult();
1831 // An init-capture is initialized directly from its stored initializer.
1832 if (Cap.isInitCapture())
1833 return cast<VarDecl>(Cap.getVariable())->getInit();
1835 // For anything else, build an initialization expression. For an implicit
1836 // capture, the capture notionally happens at the capture-default, so use
1837 // that location here.
1838 SourceLocation Loc =
1839 ImplicitCaptureLoc.isValid() ? ImplicitCaptureLoc : Cap.getLocation();
1841 // C++11 [expr.prim.lambda]p21:
1842 // When the lambda-expression is evaluated, the entities that
1843 // are captured by copy are used to direct-initialize each
1844 // corresponding non-static data member of the resulting closure
1845 // object. (For array members, the array elements are
1846 // direct-initialized in increasing subscript order.) These
1847 // initializations are performed in the (unspecified) order in
1848 // which the non-static data members are declared.
1850 // C++ [expr.prim.lambda]p12:
1851 // An entity captured by a lambda-expression is odr-used (3.2) in
1852 // the scope containing the lambda-expression.
1853 ExprResult Init;
1854 IdentifierInfo *Name = nullptr;
1855 if (Cap.isThisCapture()) {
1856 QualType ThisTy = getCurrentThisType();
1857 Expr *This = BuildCXXThisExpr(Loc, ThisTy, ImplicitCaptureLoc.isValid());
1858 if (Cap.isCopyCapture())
1859 Init = CreateBuiltinUnaryOp(Loc, UO_Deref, This);
1860 else
1861 Init = This;
1862 } else {
1863 assert(Cap.isVariableCapture() && "unknown kind of capture");
1864 ValueDecl *Var = Cap.getVariable();
1865 Name = Var->getIdentifier();
1866 Init = BuildDeclarationNameExpr(
1867 CXXScopeSpec(), DeclarationNameInfo(Var->getDeclName(), Loc), Var);
1870 // In OpenMP, the capture kind doesn't actually describe how to capture:
1871 // variables are "mapped" onto the device in a process that does not formally
1872 // make a copy, even for a "copy capture".
1873 if (IsOpenMPMapping)
1874 return Init;
1876 if (Init.isInvalid())
1877 return ExprError();
1879 Expr *InitExpr = Init.get();
1880 InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture(
1881 Name, Cap.getCaptureType(), Loc);
1882 InitializationKind InitKind =
1883 InitializationKind::CreateDirect(Loc, Loc, Loc);
1884 InitializationSequence InitSeq(*this, Entity, InitKind, InitExpr);
1885 return InitSeq.Perform(*this, Entity, InitKind, InitExpr);
1888 ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body,
1889 Scope *CurScope) {
1890 LambdaScopeInfo LSI = *cast<LambdaScopeInfo>(FunctionScopes.back());
1891 ActOnFinishFunctionBody(LSI.CallOperator, Body);
1892 return BuildLambdaExpr(StartLoc, Body->getEndLoc(), &LSI);
1895 static LambdaCaptureDefault
1896 mapImplicitCaptureStyle(CapturingScopeInfo::ImplicitCaptureStyle ICS) {
1897 switch (ICS) {
1898 case CapturingScopeInfo::ImpCap_None:
1899 return LCD_None;
1900 case CapturingScopeInfo::ImpCap_LambdaByval:
1901 return LCD_ByCopy;
1902 case CapturingScopeInfo::ImpCap_CapturedRegion:
1903 case CapturingScopeInfo::ImpCap_LambdaByref:
1904 return LCD_ByRef;
1905 case CapturingScopeInfo::ImpCap_Block:
1906 llvm_unreachable("block capture in lambda");
1908 llvm_unreachable("Unknown implicit capture style");
1911 bool Sema::CaptureHasSideEffects(const Capture &From) {
1912 if (From.isInitCapture()) {
1913 Expr *Init = cast<VarDecl>(From.getVariable())->getInit();
1914 if (Init && Init->HasSideEffects(Context))
1915 return true;
1918 if (!From.isCopyCapture())
1919 return false;
1921 const QualType T = From.isThisCapture()
1922 ? getCurrentThisType()->getPointeeType()
1923 : From.getCaptureType();
1925 if (T.isVolatileQualified())
1926 return true;
1928 const Type *BaseT = T->getBaseElementTypeUnsafe();
1929 if (const CXXRecordDecl *RD = BaseT->getAsCXXRecordDecl())
1930 return !RD->isCompleteDefinition() || !RD->hasTrivialCopyConstructor() ||
1931 !RD->hasTrivialDestructor();
1933 return false;
1936 bool Sema::DiagnoseUnusedLambdaCapture(SourceRange CaptureRange,
1937 const Capture &From) {
1938 if (CaptureHasSideEffects(From))
1939 return false;
1941 if (From.isVLATypeCapture())
1942 return false;
1944 // FIXME: maybe we should warn on these if we can find a sensible diagnostic
1945 // message
1946 if (From.isInitCapture() &&
1947 From.getVariable()->isPlaceholderVar(getLangOpts()))
1948 return false;
1950 auto diag = Diag(From.getLocation(), diag::warn_unused_lambda_capture);
1951 if (From.isThisCapture())
1952 diag << "'this'";
1953 else
1954 diag << From.getVariable();
1955 diag << From.isNonODRUsed();
1956 diag << FixItHint::CreateRemoval(CaptureRange);
1957 return true;
1960 /// Create a field within the lambda class or captured statement record for the
1961 /// given capture.
1962 FieldDecl *Sema::BuildCaptureField(RecordDecl *RD,
1963 const sema::Capture &Capture) {
1964 SourceLocation Loc = Capture.getLocation();
1965 QualType FieldType = Capture.getCaptureType();
1967 TypeSourceInfo *TSI = nullptr;
1968 if (Capture.isVariableCapture()) {
1969 const auto *Var = dyn_cast_or_null<VarDecl>(Capture.getVariable());
1970 if (Var && Var->isInitCapture())
1971 TSI = Var->getTypeSourceInfo();
1974 // FIXME: Should we really be doing this? A null TypeSourceInfo seems more
1975 // appropriate, at least for an implicit capture.
1976 if (!TSI)
1977 TSI = Context.getTrivialTypeSourceInfo(FieldType, Loc);
1979 // Build the non-static data member.
1980 FieldDecl *Field =
1981 FieldDecl::Create(Context, RD, /*StartLoc=*/Loc, /*IdLoc=*/Loc,
1982 /*Id=*/nullptr, FieldType, TSI, /*BW=*/nullptr,
1983 /*Mutable=*/false, ICIS_NoInit);
1984 // If the variable being captured has an invalid type, mark the class as
1985 // invalid as well.
1986 if (!FieldType->isDependentType()) {
1987 if (RequireCompleteSizedType(Loc, FieldType,
1988 diag::err_field_incomplete_or_sizeless)) {
1989 RD->setInvalidDecl();
1990 Field->setInvalidDecl();
1991 } else {
1992 NamedDecl *Def;
1993 FieldType->isIncompleteType(&Def);
1994 if (Def && Def->isInvalidDecl()) {
1995 RD->setInvalidDecl();
1996 Field->setInvalidDecl();
2000 Field->setImplicit(true);
2001 Field->setAccess(AS_private);
2002 RD->addDecl(Field);
2004 if (Capture.isVLATypeCapture())
2005 Field->setCapturedVLAType(Capture.getCapturedVLAType());
2007 return Field;
2010 ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc,
2011 LambdaScopeInfo *LSI) {
2012 // Collect information from the lambda scope.
2013 SmallVector<LambdaCapture, 4> Captures;
2014 SmallVector<Expr *, 4> CaptureInits;
2015 SourceLocation CaptureDefaultLoc = LSI->CaptureDefaultLoc;
2016 LambdaCaptureDefault CaptureDefault =
2017 mapImplicitCaptureStyle(LSI->ImpCaptureStyle);
2018 CXXRecordDecl *Class;
2019 CXXMethodDecl *CallOperator;
2020 SourceRange IntroducerRange;
2021 bool ExplicitParams;
2022 bool ExplicitResultType;
2023 CleanupInfo LambdaCleanup;
2024 bool ContainsUnexpandedParameterPack;
2025 bool IsGenericLambda;
2027 CallOperator = LSI->CallOperator;
2028 Class = LSI->Lambda;
2029 IntroducerRange = LSI->IntroducerRange;
2030 ExplicitParams = LSI->ExplicitParams;
2031 ExplicitResultType = !LSI->HasImplicitReturnType;
2032 LambdaCleanup = LSI->Cleanup;
2033 ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack;
2034 IsGenericLambda = Class->isGenericLambda();
2036 CallOperator->setLexicalDeclContext(Class);
2037 Decl *TemplateOrNonTemplateCallOperatorDecl =
2038 CallOperator->getDescribedFunctionTemplate()
2039 ? CallOperator->getDescribedFunctionTemplate()
2040 : cast<Decl>(CallOperator);
2042 // FIXME: Is this really the best choice? Keeping the lexical decl context
2043 // set as CurContext seems more faithful to the source.
2044 TemplateOrNonTemplateCallOperatorDecl->setLexicalDeclContext(Class);
2046 PopExpressionEvaluationContext();
2048 // True if the current capture has a used capture or default before it.
2049 bool CurHasPreviousCapture = CaptureDefault != LCD_None;
2050 SourceLocation PrevCaptureLoc = CurHasPreviousCapture ?
2051 CaptureDefaultLoc : IntroducerRange.getBegin();
2053 for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
2054 const Capture &From = LSI->Captures[I];
2056 if (From.isInvalid())
2057 return ExprError();
2059 assert(!From.isBlockCapture() && "Cannot capture __block variables");
2060 bool IsImplicit = I >= LSI->NumExplicitCaptures;
2061 SourceLocation ImplicitCaptureLoc =
2062 IsImplicit ? CaptureDefaultLoc : SourceLocation();
2064 // Use source ranges of explicit captures for fixits where available.
2065 SourceRange CaptureRange = LSI->ExplicitCaptureRanges[I];
2067 // Warn about unused explicit captures.
2068 bool IsCaptureUsed = true;
2069 if (!CurContext->isDependentContext() && !IsImplicit &&
2070 !From.isODRUsed()) {
2071 // Initialized captures that are non-ODR used may not be eliminated.
2072 // FIXME: Where did the IsGenericLambda here come from?
2073 bool NonODRUsedInitCapture =
2074 IsGenericLambda && From.isNonODRUsed() && From.isInitCapture();
2075 if (!NonODRUsedInitCapture) {
2076 bool IsLast = (I + 1) == LSI->NumExplicitCaptures;
2077 SourceRange FixItRange;
2078 if (CaptureRange.isValid()) {
2079 if (!CurHasPreviousCapture && !IsLast) {
2080 // If there are no captures preceding this capture, remove the
2081 // following comma.
2082 FixItRange = SourceRange(CaptureRange.getBegin(),
2083 getLocForEndOfToken(CaptureRange.getEnd()));
2084 } else {
2085 // Otherwise, remove the comma since the last used capture.
2086 FixItRange = SourceRange(getLocForEndOfToken(PrevCaptureLoc),
2087 CaptureRange.getEnd());
2091 IsCaptureUsed = !DiagnoseUnusedLambdaCapture(FixItRange, From);
2095 if (CaptureRange.isValid()) {
2096 CurHasPreviousCapture |= IsCaptureUsed;
2097 PrevCaptureLoc = CaptureRange.getEnd();
2100 // Map the capture to our AST representation.
2101 LambdaCapture Capture = [&] {
2102 if (From.isThisCapture()) {
2103 // Capturing 'this' implicitly with a default of '[=]' is deprecated,
2104 // because it results in a reference capture. Don't warn prior to
2105 // C++2a; there's nothing that can be done about it before then.
2106 if (getLangOpts().CPlusPlus20 && IsImplicit &&
2107 CaptureDefault == LCD_ByCopy) {
2108 Diag(From.getLocation(), diag::warn_deprecated_this_capture);
2109 Diag(CaptureDefaultLoc, diag::note_deprecated_this_capture)
2110 << FixItHint::CreateInsertion(
2111 getLocForEndOfToken(CaptureDefaultLoc), ", this");
2113 return LambdaCapture(From.getLocation(), IsImplicit,
2114 From.isCopyCapture() ? LCK_StarThis : LCK_This);
2115 } else if (From.isVLATypeCapture()) {
2116 return LambdaCapture(From.getLocation(), IsImplicit, LCK_VLAType);
2117 } else {
2118 assert(From.isVariableCapture() && "unknown kind of capture");
2119 ValueDecl *Var = From.getVariable();
2120 LambdaCaptureKind Kind =
2121 From.isCopyCapture() ? LCK_ByCopy : LCK_ByRef;
2122 return LambdaCapture(From.getLocation(), IsImplicit, Kind, Var,
2123 From.getEllipsisLoc());
2125 }();
2127 // Form the initializer for the capture field.
2128 ExprResult Init = BuildCaptureInit(From, ImplicitCaptureLoc);
2130 // FIXME: Skip this capture if the capture is not used, the initializer
2131 // has no side-effects, the type of the capture is trivial, and the
2132 // lambda is not externally visible.
2134 // Add a FieldDecl for the capture and form its initializer.
2135 BuildCaptureField(Class, From);
2136 Captures.push_back(Capture);
2137 CaptureInits.push_back(Init.get());
2139 if (LangOpts.CUDA)
2140 CUDACheckLambdaCapture(CallOperator, From);
2143 Class->setCaptures(Context, Captures);
2145 // C++11 [expr.prim.lambda]p6:
2146 // The closure type for a lambda-expression with no lambda-capture
2147 // has a public non-virtual non-explicit const conversion function
2148 // to pointer to function having the same parameter and return
2149 // types as the closure type's function call operator.
2150 if (Captures.empty() && CaptureDefault == LCD_None)
2151 addFunctionPointerConversions(*this, IntroducerRange, Class,
2152 CallOperator);
2154 // Objective-C++:
2155 // The closure type for a lambda-expression has a public non-virtual
2156 // non-explicit const conversion function to a block pointer having the
2157 // same parameter and return types as the closure type's function call
2158 // operator.
2159 // FIXME: Fix generic lambda to block conversions.
2160 if (getLangOpts().Blocks && getLangOpts().ObjC && !IsGenericLambda)
2161 addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator);
2163 // Finalize the lambda class.
2164 SmallVector<Decl*, 4> Fields(Class->fields());
2165 ActOnFields(nullptr, Class->getLocation(), Class, Fields, SourceLocation(),
2166 SourceLocation(), ParsedAttributesView());
2167 CheckCompletedCXXClass(nullptr, Class);
2170 Cleanup.mergeFrom(LambdaCleanup);
2172 LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange,
2173 CaptureDefault, CaptureDefaultLoc,
2174 ExplicitParams, ExplicitResultType,
2175 CaptureInits, EndLoc,
2176 ContainsUnexpandedParameterPack);
2177 // If the lambda expression's call operator is not explicitly marked constexpr
2178 // and we are not in a dependent context, analyze the call operator to infer
2179 // its constexpr-ness, suppressing diagnostics while doing so.
2180 if (getLangOpts().CPlusPlus17 && !CallOperator->isInvalidDecl() &&
2181 !CallOperator->isConstexpr() &&
2182 !isa<CoroutineBodyStmt>(CallOperator->getBody()) &&
2183 !Class->getDeclContext()->isDependentContext()) {
2184 CallOperator->setConstexprKind(
2185 CheckConstexprFunctionDefinition(CallOperator,
2186 CheckConstexprKind::CheckValid)
2187 ? ConstexprSpecKind::Constexpr
2188 : ConstexprSpecKind::Unspecified);
2191 // Emit delayed shadowing warnings now that the full capture list is known.
2192 DiagnoseShadowingLambdaDecls(LSI);
2194 if (!CurContext->isDependentContext()) {
2195 switch (ExprEvalContexts.back().Context) {
2196 // C++11 [expr.prim.lambda]p2:
2197 // A lambda-expression shall not appear in an unevaluated operand
2198 // (Clause 5).
2199 case ExpressionEvaluationContext::Unevaluated:
2200 case ExpressionEvaluationContext::UnevaluatedList:
2201 case ExpressionEvaluationContext::UnevaluatedAbstract:
2202 // C++1y [expr.const]p2:
2203 // A conditional-expression e is a core constant expression unless the
2204 // evaluation of e, following the rules of the abstract machine, would
2205 // evaluate [...] a lambda-expression.
2207 // This is technically incorrect, there are some constant evaluated contexts
2208 // where this should be allowed. We should probably fix this when DR1607 is
2209 // ratified, it lays out the exact set of conditions where we shouldn't
2210 // allow a lambda-expression.
2211 case ExpressionEvaluationContext::ConstantEvaluated:
2212 case ExpressionEvaluationContext::ImmediateFunctionContext:
2213 // We don't actually diagnose this case immediately, because we
2214 // could be within a context where we might find out later that
2215 // the expression is potentially evaluated (e.g., for typeid).
2216 ExprEvalContexts.back().Lambdas.push_back(Lambda);
2217 break;
2219 case ExpressionEvaluationContext::DiscardedStatement:
2220 case ExpressionEvaluationContext::PotentiallyEvaluated:
2221 case ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed:
2222 break;
2226 return MaybeBindToTemporary(Lambda);
2229 ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
2230 SourceLocation ConvLocation,
2231 CXXConversionDecl *Conv,
2232 Expr *Src) {
2233 // Make sure that the lambda call operator is marked used.
2234 CXXRecordDecl *Lambda = Conv->getParent();
2235 CXXMethodDecl *CallOperator
2236 = cast<CXXMethodDecl>(
2237 Lambda->lookup(
2238 Context.DeclarationNames.getCXXOperatorName(OO_Call)).front());
2239 CallOperator->setReferenced();
2240 CallOperator->markUsed(Context);
2242 ExprResult Init = PerformCopyInitialization(
2243 InitializedEntity::InitializeLambdaToBlock(ConvLocation, Src->getType()),
2244 CurrentLocation, Src);
2245 if (!Init.isInvalid())
2246 Init = ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
2248 if (Init.isInvalid())
2249 return ExprError();
2251 // Create the new block to be returned.
2252 BlockDecl *Block = BlockDecl::Create(Context, CurContext, ConvLocation);
2254 // Set the type information.
2255 Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo());
2256 Block->setIsVariadic(CallOperator->isVariadic());
2257 Block->setBlockMissingReturnType(false);
2259 // Add parameters.
2260 SmallVector<ParmVarDecl *, 4> BlockParams;
2261 for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) {
2262 ParmVarDecl *From = CallOperator->getParamDecl(I);
2263 BlockParams.push_back(ParmVarDecl::Create(
2264 Context, Block, From->getBeginLoc(), From->getLocation(),
2265 From->getIdentifier(), From->getType(), From->getTypeSourceInfo(),
2266 From->getStorageClass(),
2267 /*DefArg=*/nullptr));
2269 Block->setParams(BlockParams);
2271 Block->setIsConversionFromLambda(true);
2273 // Add capture. The capture uses a fake variable, which doesn't correspond
2274 // to any actual memory location. However, the initializer copy-initializes
2275 // the lambda object.
2276 TypeSourceInfo *CapVarTSI =
2277 Context.getTrivialTypeSourceInfo(Src->getType());
2278 VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation,
2279 ConvLocation, nullptr,
2280 Src->getType(), CapVarTSI,
2281 SC_None);
2282 BlockDecl::Capture Capture(/*variable=*/CapVar, /*byRef=*/false,
2283 /*nested=*/false, /*copy=*/Init.get());
2284 Block->setCaptures(Context, Capture, /*CapturesCXXThis=*/false);
2286 // Add a fake function body to the block. IR generation is responsible
2287 // for filling in the actual body, which cannot be expressed as an AST.
2288 Block->setBody(new (Context) CompoundStmt(ConvLocation));
2290 // Create the block literal expression.
2291 Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType());
2292 ExprCleanupObjects.push_back(Block);
2293 Cleanup.setExprNeedsCleanups(true);
2295 return BuildBlock;
2298 static FunctionDecl *getPatternFunctionDecl(FunctionDecl *FD) {
2299 if (FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization) {
2300 while (FD->getInstantiatedFromMemberFunction())
2301 FD = FD->getInstantiatedFromMemberFunction();
2302 return FD;
2305 if (FD->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate)
2306 return FD->getInstantiatedFromDecl();
2308 FunctionTemplateDecl *FTD = FD->getPrimaryTemplate();
2309 if (!FTD)
2310 return nullptr;
2312 while (FTD->getInstantiatedFromMemberTemplate())
2313 FTD = FTD->getInstantiatedFromMemberTemplate();
2315 return FTD->getTemplatedDecl();
2318 Sema::LambdaScopeForCallOperatorInstantiationRAII::
2319 LambdaScopeForCallOperatorInstantiationRAII(
2320 Sema &SemaRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
2321 LocalInstantiationScope &Scope, bool ShouldAddDeclsFromParentScope)
2322 : FunctionScopeRAII(SemaRef) {
2323 if (!isLambdaCallOperator(FD)) {
2324 FunctionScopeRAII::disable();
2325 return;
2328 SemaRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));
2330 FunctionDecl *Pattern = getPatternFunctionDecl(FD);
2331 if (Pattern) {
2332 SemaRef.addInstantiatedCapturesToScope(FD, Pattern, Scope, MLTAL);
2334 FunctionDecl *ParentFD = FD;
2335 while (ShouldAddDeclsFromParentScope) {
2337 ParentFD =
2338 dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(ParentFD));
2339 Pattern =
2340 dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(Pattern));
2342 if (!FD || !Pattern)
2343 break;
2345 SemaRef.addInstantiatedParametersToScope(ParentFD, Pattern, Scope, MLTAL);
2346 SemaRef.addInstantiatedLocalVarsToScope(ParentFD, Pattern, Scope);