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.
10//
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>
26using namespace clang;
27using namespace sema;
28
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.
63
64static inline std::optional<unsigned>
65getStackIndexOfNearestEnclosingCaptureReadyLambda(
66 ArrayRef<const clang::sema::FunctionScopeInfo *> FunctionScopes,
67 ValueDecl *VarToCapture) {
68 // Label failure to capture.
69 const std::optional<unsigned> NoLambdaIsCaptureReady;
70
71 // Ignore all inner captured regions.
72 unsigned CurScopeIndex = FunctionScopes.size() - 1;
73 while (CurScopeIndex > 0 && isa<clang::sema::CapturedRegionScopeInfo>(
74 Val: 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");
79
80 // If VarToCapture is null, we are attempting to capture 'this'.
81 const bool IsCapturingThis = !VarToCapture;
82 const bool IsCapturingVariable = !IsCapturingThis;
83
84 // Start with the current lambda at the top of the stack (highest index).
85 DeclContext *EnclosingDC =
86 cast<sema::LambdaScopeInfo>(Val: FunctionScopes[CurScopeIndex])->CallOperator;
87
88 do {
89 const clang::sema::LambdaScopeInfo *LSI =
90 cast<sema::LambdaScopeInfo>(Val: 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;
101
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;
123 }
124 EnclosingDC = getLambdaAwareParentOfDeclContext(DC: EnclosingDC);
125
126 assert(CurScopeIndex);
127 --CurScopeIndex;
128 } while (!EnclosingDC->isTranslationUnit() &&
129 EnclosingDC->isDependentContext() &&
130 isLambdaCallOperator(DC: EnclosingDC));
131
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;
138}
139
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.
145///
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')
162///
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.
166///
167/// \param VarToCapture - the variable to capture. If NULL, capture 'this'.
168///
169///
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.
174
175std::optional<unsigned>
176clang::getStackIndexOfNearestEnclosingCaptureCapableLambda(
177 ArrayRef<const sema::FunctionScopeInfo *> FunctionScopes,
178 ValueDecl *VarToCapture, Sema &S) {
179
180 const std::optional<unsigned> NoLambdaIsCaptureCapable;
181
182 const std::optional<unsigned> OptionalStackIndex =
183 getStackIndexOfNearestEnclosingCaptureReadyLambda(FunctionScopes,
184 VarToCapture);
185 if (!OptionalStackIndex)
186 return NoLambdaIsCaptureCapable;
187
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");
193
194 const sema::LambdaScopeInfo *const CaptureReadyLambdaLSI =
195 cast<sema::LambdaScopeInfo>(Val: FunctionScopes[IndexOfCaptureReadyLambda]);
196
197 // If VarToCapture is null, we are attempting to capture 'this'
198 const bool IsCapturingThis = !VarToCapture;
199 const bool IsCapturingVariable = !IsCapturingThis;
200
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(Var: VarToCapture,
208 /*ExprVarIsUsedInLoc*/ Loc: SourceLocation(),
209 Kind: clang::Sema::TryCapture_Implicit,
210 /*EllipsisLoc*/ SourceLocation(),
211 /*BuildAndDiagnose*/ false, CaptureType,
212 DeclRefType, FunctionScopeIndexToStopAt: &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 Loc: CaptureReadyLambdaLSI->PotentialThisCaptureLocation,
222 /*Explicit*/ false, /*BuildAndDiagnose*/ false,
223 FunctionScopeIndexToStopAt: &IndexOfCaptureReadyLambda);
224 if (!CanCaptureThis)
225 return NoLambdaIsCaptureCapable;
226 }
227 return IndexOfCaptureReadyLambda;
228}
229
230static inline TemplateParameterList *
231getGenericLambdaTemplateParameterList(LambdaScopeInfo *LSI, Sema &SemaRef) {
232 if (!LSI->GLTemplateParameterList && !LSI->TemplateParams.empty()) {
233 LSI->GLTemplateParameterList = TemplateParameterList::Create(
234 C: SemaRef.Context,
235 /*Template kw loc*/ TemplateLoc: SourceLocation(),
236 /*L angle loc*/ LAngleLoc: LSI->ExplicitTemplateParamsRange.getBegin(),
237 Params: LSI->TemplateParams,
238 /*R angle loc*/RAngleLoc: LSI->ExplicitTemplateParamsRange.getEnd(),
239 RequiresClause: LSI->RequiresClause.get());
240 }
241 return LSI->GLTemplateParameterList;
242}
243
244CXXRecordDecl *
245Sema::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();
251
252 bool IsGenericLambda =
253 Info && getGenericLambdaTemplateParameterList(LSI: getCurLambda(), SemaRef&: *this);
254 // Start constructing the lambda class.
255 CXXRecordDecl *Class = CXXRecordDecl::CreateLambda(
256 C: Context, DC, Info, Loc: IntroducerRange.getBegin(), DependencyKind: LambdaDependencyKind,
257 IsGeneric: IsGenericLambda, CaptureDefault);
258 DC->addDecl(Class);
259
260 return Class;
261}
262
263/// Determine whether the given context is or is enclosed in an inline
264/// function.
265static bool isInInlineFunction(const DeclContext *DC) {
266 while (!DC->isFileContext()) {
267 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: DC))
268 if (FD->isInlined())
269 return true;
270
271 DC = DC->getLexicalParent();
272 }
273
274 return false;
275}
276
277std::tuple<MangleNumberingContext *, Decl *>
278Sema::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;
282
283 enum ContextKind {
284 Normal,
285 DefaultArgument,
286 DataMember,
287 InlineVariable,
288 TemplatedVariable,
289 Concept
290 } Kind = Normal;
291
292 bool IsInNonspecializedTemplate =
293 inTemplateInstantiation() || CurContext->isDependentContext();
294
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>(Val: 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>(Val: 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>(Val: Var)) {
312 if (!VTS->isExplicitSpecialization())
313 Kind = TemplatedVariable;
314 }
315 } else if (isa<FieldDecl>(Val: ManglingContextDecl)) {
316 Kind = DataMember;
317 } else if (isa<ImplicitConceptSpecializationDecl>(Val: ManglingContextDecl)) {
318 Kind = Concept;
319 }
320 }
321
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>(Val: ManglingContextDecl))) ||
330 isInInlineFunction(DC: CurContext)) {
331 while (auto *CD = dyn_cast<CapturedDecl>(Val: DC))
332 DC = CD->getParent();
333 return std::make_tuple(args: &Context.getManglingNumberContext(DC), args: nullptr);
334 }
335
336 return std::make_tuple(args: nullptr, args: nullptr);
337 }
338
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 args: &Context.getManglingNumberContext(ASTContext::NeedExtraManglingDecl,
353 D: ManglingContextDecl),
354 args&: ManglingContextDecl);
355 }
356
357 llvm_unreachable("unexpected context");
358}
359
360static QualType
361buildTypeForLambdaCallOperator(Sema &S, clang::CXXRecordDecl *Class,
362 TemplateParameterList *TemplateParams,
363 TypeSourceInfo *MethodTypeInfo) {
364 assert(MethodTypeInfo && "expected a non null type");
365
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(TypeWithAuto: Result);
375 MethodType = S.Context.getFunctionType(ResultTy: Result, Args: FPT->getParamTypes(),
376 EPI: FPT->getExtProtoInfo());
377 }
378 }
379 return MethodType;
380}
381
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.
389void 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;
411}
412
413void Sema::handleLambdaNumbering(
414 CXXRecordDecl *Class, CXXMethodDecl *Method,
415 std::optional<CXXRecordDecl::LambdaNumbering> NumberingOverride) {
416 if (NumberingOverride) {
417 Class->setLambdaNumbering(*NumberingOverride);
418 return;
419 }
420
421 ContextRAII ManglingContext(*this, Class->getDeclContext());
422
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, D: 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);
435 };
436
437 CXXRecordDecl::LambdaNumbering Numbering;
438 MangleNumberingContext *MCtx;
439 std::tie(args&: MCtx, args&: Numbering.ContextDecl) =
440 getCurrentMangleNumberContext(DC: 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;
453 }
454 if (MCtx) {
455 Numbering.IndexInContext = MCtx->getNextLambdaIndex();
456 Numbering.ManglingNumber = MCtx->getManglingNumber(CallOperator: Method);
457 Numbering.DeviceManglingNumber = MCtx->getDeviceManglingNumber(Method);
458 Class->setLambdaNumbering(Numbering);
459
460 if (auto *Source =
461 dyn_cast_or_null<ExternalSemaSource>(Val: Context.getExternalSource()))
462 Source->AssignedLambdaNumbering(Lambda: Class);
463 }
464}
465
466static 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;
477 }
478}
479
480void 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;
496}
497
498void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) {
499 LSI->finishedExplicitCaptures();
500}
501
502void 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(in_start: TParams.begin(), in_end: TParams.end());
516 LSI->NumExplicitTemplateParams = TParams.size();
517 LSI->ExplicitTemplateParamsRange = {LAngleLoc, RAngleLoc};
518 LSI->RequiresClause = RequiresClause;
519}
520
521/// If this expression is an enumerator-like expression of some type
522/// T, return the type T; otherwise, return null.
523///
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).
528static 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();
532
533 // - it is an enumerator whose enum type is T or
534 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E)) {
535 if (EnumConstantDecl *D
536 = dyn_cast<EnumConstantDecl>(Val: DRE->getDecl())) {
537 return cast<EnumDecl>(D->getDeclContext());
538 }
539 return nullptr;
540 }
541
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>(Val: E)) {
545 if (BO->getOpcode() == BO_Comma)
546 return findEnumForBlockReturn(E: BO->getRHS());
547 return nullptr;
548 }
549
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>(Val: E)) {
553 if (Expr *last = dyn_cast_or_null<Expr>(Val: SE->getSubStmt()->body_back()))
554 return findEnumForBlockReturn(E: last);
555 return nullptr;
556 }
557
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>(Val: E)) {
562 if (EnumDecl *ED = findEnumForBlockReturn(E: CO->getTrueExpr()))
563 if (ED == findEnumForBlockReturn(E: CO->getFalseExpr()))
564 return ED;
565 return nullptr;
566 }
567
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>(Val: 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());
576
577 // Otherwise, just rely on the type.
578 }
579
580 // - it is an expression of that formal enum type.
581 if (const EnumType *ET = E->getType()->getAs<EnumType>()) {
582 return ET->getDecl();
583 }
584
585 // Otherwise, nope.
586 return nullptr;
587}
588
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.
591static EnumDecl *findEnumForBlockReturn(ReturnStmt *ret) {
592 if (Expr *retValue = ret->getRetValue())
593 return findEnumForBlockReturn(E: retValue);
594 return nullptr;
595}
596
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.
600static EnumDecl *findCommonEnumForBlockReturns(ArrayRef<ReturnStmt*> returns) {
601 ArrayRef<ReturnStmt*>::iterator i = returns.begin(), e = returns.end();
602
603 // Try to find one for the first return.
604 EnumDecl *ED = findEnumForBlockReturn(ret: *i);
605 if (!ED) return nullptr;
606
607 // Check that the rest of the returns have the same enum.
608 for (++i; i != e; ++i) {
609 if (findEnumForBlockReturn(ret: *i) != ED)
610 return nullptr;
611 }
612
613 // Never infer an anonymous enum type.
614 if (!ED->hasNameForLinkage()) return nullptr;
615
616 return ED;
617}
618
619/// Adjust the given return statements so that they formally return
620/// the given type. It should require, at most, an IntegralCast.
621static 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(T1: retValue->getType(), T2: returnType))
628 continue;
629
630 // Right now we only support integral fixup casts.
631 assert(returnType->isIntegralOrUnscopedEnumerationType());
632 assert(retValue->getType()->isIntegralOrUnscopedEnumerationType());
633
634 ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Val: retValue);
635
636 Expr *E = (cleanups ? cleanups->getSubExpr() : retValue);
637 E = ImplicitCastExpr::Create(Context: S.Context, T: returnType, Kind: CK_IntegralCast, Operand: E,
638 /*base path*/ BasePath: nullptr, Cat: VK_PRValue,
639 FPO: FPOptionsOverride());
640 if (cleanups) {
641 cleanups->setSubExpr(E);
642 } else {
643 ret->setRetValue(E);
644 }
645 }
646}
647
648void 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");
654
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.
668 //
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.
672 //
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.
677
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;
686 }
687
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;
693
694 // Try to apply the enum-fuzz rule.
695 if (!getLangOpts().CPlusPlus) {
696 assert(isa<BlockScopeInfo>(CSI));
697 const EnumDecl *ED = findCommonEnumForBlockReturns(returns: CSI.Returns);
698 if (ED) {
699 CSI.ReturnType = Context.getTypeDeclType(ED);
700 adjustBlockReturnsToEnum(*this, CSI.Returns, CSI.ReturnType);
701 return;
702 }
703 }
704
705 // Third case: only one return statement. Don't bother doing extra work!
706 if (CSI.Returns.size() == 1)
707 return;
708
709 // General case: many return statements.
710 // Check that they all have compatible return types.
711
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();
717
718 QualType ReturnType =
719 (RetE ? RetE->getType() : Context.VoidTy).getUnqualifiedType();
720 if (Context.getCanonicalFunctionResultType(ResultType: ReturnType) ==
721 Context.getCanonicalFunctionResultType(ResultType: 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;
730 }
731
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.
738 }
739}
740
741QualType 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>(T: DeductType);
750 TL.setNameLoc(Loc);
751 if (ByRef) {
752 DeductType = BuildReferenceType(T: DeductType, LValueRef: true, Loc, Entity: Id);
753 assert(!DeductType.isNull() && "can't build reference to auto");
754 TLB.push<ReferenceTypeLoc>(T: DeductType).setSigilLoc(Loc);
755 }
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(Pattern: DeductType, NumExpansions,
762 /*ExpectPackInType=*/false);
763 TLB.push<PackExpansionTypeLoc>(T: 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.
767 }
768 }
769 TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, T: DeductType);
770
771 // Deduce the type of the init capture.
772 QualType DeducedType = deduceVarTypeFromInitializer(
773 /*VarDecl*/VDecl: nullptr, Name: DeclarationName(Id), Type: DeductType, TSI,
774 Range: SourceRange(Loc, Loc), DirectInit: IsDirectInit, Init);
775 if (DeducedType.isNull())
776 return QualType();
777
778 // Are we a non-list direct initialization?
779 ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Val: Init);
780
781 // Perform initialization analysis and ensure any implicit conversions
782 // (such as lvalue-to-rvalue) are enforced.
783 InitializedEntity Entity =
784 InitializedEntity::InitializeLambdaCapture(VarID: Id, FieldType: DeducedType, Loc);
785 InitializationKind Kind =
786 IsDirectInit
787 ? (CXXDirectInit ? InitializationKind::CreateDirect(
788 InitLoc: Loc, LParenLoc: Init->getBeginLoc(), RParenLoc: Init->getEndLoc())
789 : InitializationKind::CreateDirectList(InitLoc: Loc))
790 : InitializationKind::CreateCopy(InitLoc: Loc, EqualLoc: Init->getBeginLoc());
791
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(S&: *this, Entity, Kind, Args, ResultType: &DclT);
799
800 if (Result.isInvalid())
801 return QualType();
802
803 Init = Result.getAs<Expr>();
804 return DeducedType;
805}
806
807VarDecl *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(T: InitCaptureType, Loc);
813 if (auto PETL = TSI->getTypeLoc().getAs<PackExpansionTypeLoc>())
814 PETL.setEllipsisLoc(EllipsisLoc);
815
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(C&: Context, DC: DeclCtx, StartLoc: Loc, IdLoc: Loc, Id,
821 T: InitCaptureType, TInfo: TSI, S: 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;
831}
832
833void Sema::addInitCapture(LambdaScopeInfo *LSI, VarDecl *Var, bool ByRef) {
834 assert(Var->isInitCapture() && "init capture flag should be set");
835 LSI->addCapture(Var, /*isBlock=*/false, isByref: ByRef,
836 /*isNested=*/false, Loc: Var->getLocation(), EllipsisLoc: SourceLocation(),
837 CaptureType: Var->getType(), /*Invalid=*/false);
838}
839
840// Unlike getCurLambda, getCurrentLambdaScopeUnsafe doesn't
841// check that the current lambda is in a consistent or fully constructed state.
842static LambdaScopeInfo *getCurrentLambdaScopeUnsafe(Sema &S) {
843 assert(!S.FunctionScopes.empty());
844 return cast<LambdaScopeInfo>(Val: S.FunctionScopes[S.FunctionScopes.size() - 1]);
845}
846
847static TypeSourceInfo *
848getDummyLambdaType(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(space: AS);
859
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(ResultTy: DefaultTypeForNoTrailingReturn,
870 Args: std::nullopt, EPI);
871 return S.Context.getTrivialTypeSourceInfo(T: MethodTy, Loc);
872}
873
874static TypeSourceInfo *getLambdaType(Sema &S, LambdaIntroducer &Intro,
875 Declarator &ParamInfo, Scope *CurScope,
876 SourceLocation Loc,
877 bool &ExplicitResultType) {
878
879 ExplicitResultType = false;
880
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;
888
889 TypeSourceInfo *MethodTyInfo;
890
891 if (ParamInfo.getNumTypeObjects() == 0) {
892 MethodTyInfo = getDummyLambdaType(S, Loc);
893 } else {
894 // Check explicit parameters
895 S.CheckExplicitObjectLambda(D&: ParamInfo);
896
897 DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo();
898
899 bool HasExplicitObjectParameter =
900 ParamInfo.isExplicitObjectMemberFunction();
901
902 ExplicitResultType = FTI.hasTrailingReturnType();
903 if (!FTI.hasMutableQualifier() && !IsLambdaStatic &&
904 !HasExplicitObjectParameter)
905 FTI.getOrCreateMethodQualifiers().SetTypeQual(T: DeclSpec::TQ_const, Loc);
906
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);
916 }
917 }
918
919 MethodTyInfo = S.GetTypeForDeclarator(D&: ParamInfo);
920 assert(MethodTyInfo && "no type from lambda-declarator");
921
922 // Check for unexpanded parameter packs in the method type.
923 if (MethodTyInfo->getType()->containsUnexpandedParameterPack())
924 S.DiagnoseUnexpandedParameterPack(Loc: Intro.Range.getBegin(), T: MethodTyInfo,
925 UPPC: S.UPPC_DeclarationType);
926 }
927 return MethodTyInfo;
928}
929
930CXXMethodDecl *Sema::CreateLambdaCallOperator(SourceRange IntroducerRange,
931 CXXRecordDecl *Class) {
932
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(Op: OO_Call);
941 DeclarationNameLoc MethodNameLoc =
942 DeclarationNameLoc::makeCXXOperatorNameLoc(Range: IntroducerRange.getBegin());
943 CXXMethodDecl *Method = CXXMethodDecl::Create(
944 C&: Context, RD: Class, StartLoc: SourceLocation(),
945 NameInfo: DeclarationNameInfo(MethodName, IntroducerRange.getBegin(),
946 MethodNameLoc),
947 T: QualType(), /*Tinfo=*/TInfo: nullptr, SC: SC_None,
948 UsesFPIntrin: getCurFPFeatures().isFPConstrained(),
949 /*isInline=*/true, ConstexprKind: ConstexprSpecKind::Unspecified, EndLocation: SourceLocation(),
950 /*TrailingRequiresClause=*/nullptr);
951 Method->setAccess(AS_public);
952 return Method;
953}
954
955void Sema::AddTemplateParametersToLambdaCallOperator(
956 CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
957 TemplateParameterList *TemplateParams) {
958 assert(TemplateParams && "no template parameters");
959 FunctionTemplateDecl *TemplateMethod = FunctionTemplateDecl::Create(
960 C&: Context, DC: Class, L: CallOperator->getLocation(), Name: CallOperator->getDeclName(),
961 Params: TemplateParams, Decl: CallOperator);
962 TemplateMethod->setAccess(AS_public);
963 CallOperator->setDescribedFunctionTemplate(TemplateMethod);
964}
965
966void 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) {
972
973 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(S&: *this);
974
975 if (TrailingRequiresClause)
976 Method->setTrailingRequiresClause(TrailingRequiresClause);
977
978 TemplateParameterList *TemplateParams =
979 getGenericLambdaTemplateParameterList(LSI, SemaRef&: *this);
980
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");
988
989 LSI->Lambda->addDecl(TemplateMethod);
990 TemplateMethod->setLexicalDeclContext(DC);
991 } else {
992 LSI->Lambda->addDecl(Method);
993 }
994 LSI->Lambda->setLambdaIsGeneric(TemplateParams);
995 LSI->Lambda->setLambdaTypeInfo(MethodTyInfo);
996
997 Method->setLexicalDeclContext(DC);
998 Method->setLocation(LambdaLoc);
999 Method->setInnerLocStart(CallOperatorLoc);
1000 Method->setTypeSourceInfo(MethodTyInfo);
1001 Method->setType(buildTypeForLambdaCallOperator(S&: *this, Class: LSI->Lambda,
1002 TemplateParams, MethodTypeInfo: MethodTyInfo));
1003 Method->setConstexprKind(ConstexprKind);
1004 Method->setStorageClass(SC);
1005 if (!Params.empty()) {
1006 CheckParmsForFunctionDef(Parameters: 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);
1011 }
1012 }
1013
1014 buildLambdaScopeReturnType(S&: *this, LSI, CallOperator: Method, ExplicitResultType: HasExplicitResultType);
1015}
1016
1017void Sema::ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro,
1018 Scope *CurrentScope) {
1019
1020 LambdaScopeInfo *LSI = getCurLambda();
1021 assert(LSI && "LambdaScopeInfo should be on stack!");
1022
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;
1030
1031 assert(LSI->NumExplicitTemplateParams == 0);
1032
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;
1047 }
1048
1049 CXXRecordDecl *Class = createLambdaClosureType(
1050 IntroducerRange: Intro.Range, /*Info=*/nullptr, LambdaDependencyKind, CaptureDefault: Intro.Default);
1051 LSI->Lambda = Class;
1052
1053 CXXMethodDecl *Method = CreateLambdaCallOperator(IntroducerRange: Intro.Range, Class);
1054 LSI->CallOperator = Method;
1055 Method->setLexicalDeclContext(CurContext);
1056
1057 PushDeclContext(CurScope, Method);
1058
1059 bool ContainsUnexpandedParameterPack = false;
1060
1061 // Distinct capture names, for diagnostics.
1062 llvm::DenseMap<IdentifierInfo *, ValueDecl *> CaptureNames;
1063
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);
1074
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;
1084 }
1085
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);
1095
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;
1103 }
1104
1105 CheckCXXThisCapture(Loc: C->Loc, /*Explicit=*/true, /*BuildAndDiagnose*/ true,
1106 /*FunctionScopeIndexToStopAtPtr*/ FunctionScopeIndexToStopAt: nullptr,
1107 ByCopy: C->Kind == LCK_StarThis);
1108 if (!LSI->Captures.empty())
1109 LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
1110 continue;
1111 }
1112
1113 assert(C->Id && "missing identifier for capture");
1114
1115 if (C->Init.isInvalid())
1116 continue;
1117
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);
1123
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;
1131
1132 if (C->Init.get()->containsUnexpandedParameterPack() &&
1133 !C->InitCaptureType.get()->getAs<PackExpansionType>())
1134 DiagnoseUnexpandedParameterPack(E: C->Init.get(), UPPC: UPPC_Initializer);
1135
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;
1149 }
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>(Val: Var))
1155 CheckShadow(S: CurrentScope, D: V);
1156 PushOnScopeChains(Var, CurrentScope, false);
1157 } else {
1158 assert(C->InitKind == LambdaCaptureInitKind::NoInit &&
1159 "init capture has valid but null init?");
1160
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;
1176 }
1177
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, S: 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(S: CurScope, SS&: ScopeSpec, R, CCC&: Validator))
1191 continue;
1192 }
1193
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;
1200 }
1201
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;
1209 }
1210
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(KV: 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(Loc: 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;
1230 }
1231 }
1232
1233 // Ignore invalid decls; they'll just confuse the code later.
1234 if (Var->isInvalidDecl())
1235 continue;
1236
1237 VarDecl *Underlying = Var->getPotentiallyDecomposedVarDecl();
1238
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;
1243 }
1244
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));
1255
1256 // Just ignore the ellipsis.
1257 }
1258 } else if (Var->isParameterPack()) {
1259 ContainsUnexpandedParameterPack = true;
1260 }
1261
1262 if (C->Init.isUsable()) {
1263 addInitCapture(LSI, Var: cast<VarDecl>(Val: Var), ByRef: 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, Loc: C->Loc, Kind, EllipsisLoc);
1269 }
1270 if (!LSI->Captures.empty())
1271 LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
1272 }
1273 finishLambdaExplicitCaptures(LSI);
1274 LSI->ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
1275 PopDeclContext();
1276}
1277
1278void Sema::ActOnLambdaClosureQualifiers(LambdaIntroducer &Intro,
1279 SourceLocation MutableLoc) {
1280
1281 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(S&: *this);
1282 LSI->Mutable = MutableLoc.isValid();
1283 ContextRAII Context(*this, LSI->CallOperator, /*NewThisContext*/ false);
1284
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.
1289 //
1290 // For simple-captures, this is covered by the check below that any named
1291 // entity is a variable that can be captured.
1292 //
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);
1302}
1303
1304void Sema::ActOnLambdaClosureParameters(
1305 Scope *LambdaScope, MutableArrayRef<DeclaratorChunk::ParamInfo> Params) {
1306 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(S&: *this);
1307 PushDeclContext(LambdaScope, LSI->CallOperator);
1308
1309 for (const DeclaratorChunk::ParamInfo &P : Params) {
1310 auto *Param = cast<ParmVarDecl>(Val: P.Param);
1311 Param->setOwningFunction(LSI->CallOperator);
1312 if (Param->getIdentifier())
1313 PushOnScopeChains(Param, LambdaScope, false);
1314 }
1315
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, SemaRef&: *this);
1322 if (TemplateParams) {
1323 AddTemplateParametersToLambdaCallOperator(CallOperator: LSI->CallOperator, Class: LSI->Lambda,
1324 TemplateParams);
1325 LSI->Lambda->setLambdaIsGeneric(true);
1326 }
1327 LSI->AfterParameterList = true;
1328}
1329
1330void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro,
1331 Declarator &ParamInfo,
1332 const DeclSpec &DS) {
1333
1334 LambdaScopeInfo *LSI = getCurrentLambdaScopeUnsafe(S&: *this);
1335 LSI->CallOperator->setConstexprKind(DS.getConstexprSpecifier());
1336
1337 SmallVector<ParmVarDecl *, 8> Params;
1338 bool ExplicitResultType;
1339
1340 SourceLocation TypeLoc, CallOperatorLoc;
1341 if (ParamInfo.getNumTypeObjects() == 0) {
1342 CallOperatorLoc = TypeLoc = Intro.Range.getEnd();
1343 } else {
1344 unsigned Index;
1345 ParamInfo.isFunctionDeclarator(idx&: Index);
1346 const auto &Object = ParamInfo.getTypeObject(i: Index);
1347 TypeLoc =
1348 Object.Loc.isValid() ? Object.Loc : ParamInfo.getSourceRange().getEnd();
1349 CallOperatorLoc = ParamInfo.getSourceRange().getEnd();
1350 }
1351
1352 CXXRecordDecl *Class = LSI->Lambda;
1353 CXXMethodDecl *Method = LSI->CallOperator;
1354
1355 TypeSourceInfo *MethodTyInfo = getLambdaType(
1356 S&: *this, Intro, ParamInfo, CurScope: getCurScope(), Loc: TypeLoc, ExplicitResultType);
1357
1358 LSI->ExplicitParams = ParamInfo.getNumTypeObjects() != 0;
1359
1360 if (ParamInfo.isFunctionDeclarator() != 0 &&
1361 !FTIHasSingleVoidParameter(FTI: ParamInfo.getFunctionTypeInfo())) {
1362 const auto &FTI = ParamInfo.getFunctionTypeInfo();
1363 Params.reserve(N: Params.size());
1364 for (unsigned I = 0; I < FTI.NumParams; ++I) {
1365 auto *Param = cast<ParmVarDecl>(Val: FTI.Params[I].Param);
1366 Param->setScopeInfo(scopeDepth: 0, parameterIndex: Params.size());
1367 Params.push_back(Elt: Param);
1368 }
1369 }
1370
1371 bool IsLambdaStatic =
1372 ParamInfo.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static;
1373
1374 CompleteLambdaCallOperator(
1375 Method, LambdaLoc: Intro.Range.getBegin(), CallOperatorLoc,
1376 TrailingRequiresClause: ParamInfo.getTrailingRequiresClause(), MethodTyInfo,
1377 ConstexprKind: ParamInfo.getDeclSpec().getConstexprSpecifier(),
1378 SC: IsLambdaStatic ? SC_Static : SC_None, Params, HasExplicitResultType: ExplicitResultType);
1379
1380 CheckCXXDefaultArguments(Method);
1381
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);
1385
1386 // code_seg attribute on lambda apply to the method.
1387 if (Attr *A = getImplicitCodeSegOrSectionAttrForFunction(
1388 Method, /*IsDefinition=*/true))
1389 Method->addAttr(A);
1390
1391 // Attributes on the lambda apply to the method.
1392 ProcessDeclAttributes(CurScope, Method, ParamInfo);
1393
1394 // CUDA lambdas get implicit host and device attributes.
1395 if (getLangOpts().CUDA)
1396 CUDASetLambdaAttrs(Method);
1397
1398 // OpenMP lambdas might get assumumption attributes.
1399 if (LangOpts.OpenMP)
1400 ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Method);
1401
1402 handleLambdaNumbering(Class, Method);
1403
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);
1410 }
1411 }
1412
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;
1420 }
1421 }
1422 return true;
1423 };
1424
1425 for (ParmVarDecl *P : Params) {
1426 if (!P->getIdentifier())
1427 continue;
1428 if (CheckRedefinition(P))
1429 CheckShadow(CurScope, P);
1430 PushOnScopeChains(P, CurScope);
1431 }
1432
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, SemaRef&: *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 NoteTemplateParameterLocation(Decl: *TP);
1448 }
1449 }
1450 }
1451 }
1452
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]
1469 //
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.
1474
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);
1481 }
1482 }
1483
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();
1494}
1495
1496void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope,
1497 bool IsInstantiation) {
1498 LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(Val: FunctionScopes.back());
1499
1500 // Leave the expression-evaluation context.
1501 DiscardCleanupsInEvaluationContext();
1502 PopExpressionEvaluationContext();
1503
1504 // Leave the context of the lambda.
1505 if (!IsInstantiation)
1506 PopDeclContext();
1507
1508 // Finalize the lambda.
1509 CXXRecordDecl *Class = LSI->Lambda;
1510 Class->setInvalidDecl();
1511 SmallVector<Decl*, 4> Fields(Class->fields());
1512 ActOnFields(S: nullptr, RecLoc: Class->getLocation(), TagDecl: Class, Fields, LBrac: SourceLocation(),
1513 RBrac: SourceLocation(), AttrList: ParsedAttributesView());
1514 CheckCompletedCXXClass(S: nullptr, Record: Class);
1515
1516 PopFunctionScopeInfo();
1517}
1518
1519template <typename Func>
1520static void repeatForLambdaConversionFunctionCallingConvs(
1521 Sema &S, const FunctionProtoType &CallOpProto, Func F) {
1522 CallingConv DefaultFree = S.Context.getDefaultCallingConvention(
1523 IsVariadic: CallOpProto.isVariadic(), /*IsCXXMethod=*/false);
1524 CallingConv DefaultMember = S.Context.getDefaultCallingConvention(
1525 IsVariadic: CallOpProto.isVariadic(), /*IsCXXMethod=*/true);
1526 CallingConv CallOpCC = CallOpProto.getCallConv();
1527
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(C&: Convs);
1545 llvm::iterator_range<CallingConv *> Range(
1546 std::begin(arr&: Convs), std::unique(first: std::begin(arr&: Convs), last: std::end(arr&: Convs)));
1547 const TargetInfo &TI = S.getASTContext().getTargetInfo();
1548
1549 for (CallingConv C : Range) {
1550 if (TI.checkCallingConvention(C) == TargetInfo::CCCR_OK)
1551 F(C);
1552 }
1553 return;
1554 }
1555
1556 if (CallOpCC == DefaultMember && DefaultMember != DefaultFree) {
1557 F(DefaultFree);
1558 F(DefaultMember);
1559 } else {
1560 F(CallOpCC);
1561 }
1562}
1563
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.
1567static CallingConv
1568getLambdaConversionFunctionCallConv(Sema &S,
1569 const FunctionProtoType *CallOpProto) {
1570 CallingConv DefaultFree = S.Context.getDefaultCallingConvention(
1571 IsVariadic: CallOpProto->isVariadic(), /*IsCXXMethod=*/false);
1572 CallingConv DefaultMember = S.Context.getDefaultCallingConvention(
1573 IsVariadic: CallOpProto->isVariadic(), /*IsCXXMethod=*/true);
1574 CallingConv CallOpCC = CallOpProto->getCallConv();
1575
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;
1581}
1582
1583QualType 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: 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(ResultTy: CallOpProto->getReturnType(),
1593 Args: CallOpProto->getParamTypes(), EPI: InvokerExtInfo);
1594}
1595
1596/// Add a lambda's conversion to function pointer, as described in
1597/// C++11 [expr.prim.lambda]p6.
1598static 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>();
1606 };
1607 if (llvm::any_of(CallOperator->parameters(), HasPassObjectSizeAttr))
1608 return;
1609
1610 // Add the conversion to function pointer.
1611 QualType PtrToFunctionTy = S.Context.getPointerType(T: InvokerFunctionTy);
1612
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(ResultTy: PtrToFunctionTy, Args: std::nullopt, EPI: ConvExtInfo);
1623
1624 SourceLocation Loc = IntroducerRange.getBegin();
1625 DeclarationName ConversionName
1626 = S.Context.DeclarationNames.getCXXConversionFunctionName(
1627 Ty: S.Context.getCanonicalType(T: 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(T: PtrToFunctionTy, Loc);
1646 DeclarationNameLoc ConvNameLoc =
1647 DeclarationNameLoc::makeNamedTypeLoc(TInfo: ConvNamePtrToFunctionTSI);
1648
1649 // The conversion function is a conversion to a pointer-to-function.
1650 TypeSourceInfo *ConvTSI = S.Context.getTrivialTypeSourceInfo(T: 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>();
1660
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>();
1667
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);
1679
1680 InvokerParams.push_back(Elt: ParmVarDecl::Create(
1681 C&: S.Context,
1682 // Temporarily add to the TU. This is set to the invoker below.
1683 DC: S.Context.getTranslationUnitDecl(), StartLoc: From->getBeginLoc(),
1684 IdLoc: From->getLocation(), Id: From->getIdentifier(), T: From->getType(),
1685 TInfo: From->getTypeSourceInfo(), S: From->getStorageClass(),
1686 /*DefArg=*/nullptr));
1687 CallOpConvTL.setParam(I, From);
1688 CallOpConvNameTL.setParam(I, From);
1689 }
1690
1691 CXXConversionDecl *Conversion = CXXConversionDecl::Create(
1692 C&: S.Context, RD: Class, StartLoc: Loc,
1693 NameInfo: DeclarationNameInfo(ConversionName, Loc, ConvNameLoc), T: ConvTy, TInfo: ConvTSI,
1694 UsesFPIntrin: S.getCurFPFeatures().isFPConstrained(),
1695 /*isInline=*/true, ES: ExplicitSpecifier(),
1696 ConstexprKind: S.getLangOpts().CPlusPlus17 ? ConstexprSpecKind::Constexpr
1697 : ConstexprSpecKind::Unspecified,
1698 EndLocation: CallOperator->getBody()->getEndLoc());
1699 Conversion->setAccess(AS_public);
1700 Conversion->setImplicit(true);
1701
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);
1706
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(C&: S.Context, DC: Class,
1714 L: Loc, Name: ConversionName,
1715 Params: TemplateCallOperator->getTemplateParameters(),
1716 Decl: Conversion);
1717 ConversionTemplate->setAccess(AS_public);
1718 ConversionTemplate->setImplicit(true);
1719 Conversion->setDescribedFunctionTemplate(ConversionTemplate);
1720 Class->addDecl(ConversionTemplate);
1721 } else
1722 Class->addDecl(Conversion);
1723
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(Name: 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 C&: S.Context, RD: Class, StartLoc: Loc, NameInfo: DeclarationNameInfo(InvokerName, Loc),
1744 T: InvokerFunctionTy, TInfo: CallOperator->getTypeSourceInfo(), SC: SC_Static,
1745 UsesFPIntrin: S.getCurFPFeatures().isFPConstrained(),
1746 /*isInline=*/true, ConstexprKind: CallOperator->getConstexprKind(),
1747 EndLocation: 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 C&: S.Context, DC: Class, L: Loc, Name: InvokerName,
1759 Params: TemplateCallOperator->getTemplateParameters(), Decl: Invoke);
1760 StaticInvokerTemplate->setAccess(AS_private);
1761 StaticInvokerTemplate->setImplicit(true);
1762 Invoke->setDescribedFunctionTemplate(StaticInvokerTemplate);
1763 Class->addDecl(StaticInvokerTemplate);
1764 } else
1765 Class->addDecl(Invoke);
1766 }
1767}
1768
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.
1773static void addFunctionPointerConversions(Sema &S, SourceRange IntroducerRange,
1774 CXXRecordDecl *Class,
1775 CXXMethodDecl *CallOperator) {
1776 const FunctionProtoType *CallOpProto =
1777 CallOperator->getType()->castAs<FunctionProtoType>();
1778
1779 repeatForLambdaConversionFunctionCallingConvs(
1780 S, CallOpProto: *CallOpProto, F: [&](CallingConv CC) {
1781 QualType InvokerFunctionTy =
1782 S.getLambdaConversionFunctionResultType(CallOpProto, CC);
1783 addFunctionPointerConversion(S, IntroducerRange, Class, CallOperator,
1784 InvokerFunctionTy);
1785 });
1786}
1787
1788/// Add a lambda's conversion to block pointer.
1789static 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, CC: getLambdaConversionFunctionCallConv(S, CallOpProto));
1797 QualType BlockPtrTy = S.Context.getBlockPointerType(T: FunctionTy);
1798
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(ResultTy: BlockPtrTy, Args: std::nullopt, EPI: ConversionEPI);
1806
1807 SourceLocation Loc = IntroducerRange.getBegin();
1808 DeclarationName Name
1809 = S.Context.DeclarationNames.getCXXConversionFunctionName(
1810 Ty: S.Context.getCanonicalType(T: BlockPtrTy));
1811 DeclarationNameLoc NameLoc = DeclarationNameLoc::makeNamedTypeLoc(
1812 TInfo: S.Context.getTrivialTypeSourceInfo(T: BlockPtrTy, Loc));
1813 CXXConversionDecl *Conversion = CXXConversionDecl::Create(
1814 C&: S.Context, RD: Class, StartLoc: Loc, NameInfo: DeclarationNameInfo(Name, Loc, NameLoc), T: ConvTy,
1815 TInfo: S.Context.getTrivialTypeSourceInfo(T: ConvTy, Loc),
1816 UsesFPIntrin: S.getCurFPFeatures().isFPConstrained(),
1817 /*isInline=*/true, ES: ExplicitSpecifier(), ConstexprKind: ConstexprSpecKind::Unspecified,
1818 EndLocation: CallOperator->getBody()->getEndLoc());
1819 Conversion->setAccess(AS_public);
1820 Conversion->setImplicit(true);
1821 Class->addDecl(Conversion);
1822}
1823
1824ExprResult 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();
1830
1831 // An init-capture is initialized directly from its stored initializer.
1832 if (Cap.isInitCapture())
1833 return cast<VarDecl>(Val: Cap.getVariable())->getInit();
1834
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();
1840
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.
1849
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, Type: ThisTy, IsImplicit: ImplicitCaptureLoc.isValid());
1858 if (Cap.isCopyCapture())
1859 Init = CreateBuiltinUnaryOp(OpLoc: Loc, Opc: UO_Deref, InputExpr: 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);
1868 }
1869
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;
1875
1876 if (Init.isInvalid())
1877 return ExprError();
1878
1879 Expr *InitExpr = Init.get();
1880 InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture(
1881 VarID: Name, FieldType: Cap.getCaptureType(), Loc);
1882 InitializationKind InitKind =
1883 InitializationKind::CreateDirect(InitLoc: Loc, LParenLoc: Loc, RParenLoc: Loc);
1884 InitializationSequence InitSeq(*this, Entity, InitKind, InitExpr);
1885 return InitSeq.Perform(S&: *this, Entity, Kind: InitKind, Args: InitExpr);
1886}
1887
1888ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body) {
1889 LambdaScopeInfo LSI = *cast<LambdaScopeInfo>(Val: FunctionScopes.back());
1890 ActOnFinishFunctionBody(LSI.CallOperator, Body);
1891 return BuildLambdaExpr(StartLoc, EndLoc: Body->getEndLoc(), LSI: &LSI);
1892}
1893
1894static LambdaCaptureDefault
1895mapImplicitCaptureStyle(CapturingScopeInfo::ImplicitCaptureStyle ICS) {
1896 switch (ICS) {
1897 case CapturingScopeInfo::ImpCap_None:
1898 return LCD_None;
1899 case CapturingScopeInfo::ImpCap_LambdaByval:
1900 return LCD_ByCopy;
1901 case CapturingScopeInfo::ImpCap_CapturedRegion:
1902 case CapturingScopeInfo::ImpCap_LambdaByref:
1903 return LCD_ByRef;
1904 case CapturingScopeInfo::ImpCap_Block:
1905 llvm_unreachable("block capture in lambda");
1906 }
1907 llvm_unreachable("Unknown implicit capture style");
1908}
1909
1910bool Sema::CaptureHasSideEffects(const Capture &From) {
1911 if (From.isInitCapture()) {
1912 Expr *Init = cast<VarDecl>(Val: From.getVariable())->getInit();
1913 if (Init && Init->HasSideEffects(Ctx: Context))
1914 return true;
1915 }
1916
1917 if (!From.isCopyCapture())
1918 return false;
1919
1920 const QualType T = From.isThisCapture()
1921 ? getCurrentThisType()->getPointeeType()
1922 : From.getCaptureType();
1923
1924 if (T.isVolatileQualified())
1925 return true;
1926
1927 const Type *BaseT = T->getBaseElementTypeUnsafe();
1928 if (const CXXRecordDecl *RD = BaseT->getAsCXXRecordDecl())
1929 return !RD->isCompleteDefinition() || !RD->hasTrivialCopyConstructor() ||
1930 !RD->hasTrivialDestructor();
1931
1932 return false;
1933}
1934
1935bool Sema::DiagnoseUnusedLambdaCapture(SourceRange CaptureRange,
1936 const Capture &From) {
1937 if (CaptureHasSideEffects(From))
1938 return false;
1939
1940 if (From.isVLATypeCapture())
1941 return false;
1942
1943 // FIXME: maybe we should warn on these if we can find a sensible diagnostic
1944 // message
1945 if (From.isInitCapture() &&
1946 From.getVariable()->isPlaceholderVar(getLangOpts()))
1947 return false;
1948
1949 auto diag = Diag(From.getLocation(), diag::warn_unused_lambda_capture);
1950 if (From.isThisCapture())
1951 diag << "'this'";
1952 else
1953 diag << From.getVariable();
1954 diag << From.isNonODRUsed();
1955 diag << FixItHint::CreateRemoval(RemoveRange: CaptureRange);
1956 return true;
1957}
1958
1959/// Create a field within the lambda class or captured statement record for the
1960/// given capture.
1961FieldDecl *Sema::BuildCaptureField(RecordDecl *RD,
1962 const sema::Capture &Capture) {
1963 SourceLocation Loc = Capture.getLocation();
1964 QualType FieldType = Capture.getCaptureType();
1965
1966 TypeSourceInfo *TSI = nullptr;
1967 if (Capture.isVariableCapture()) {
1968 const auto *Var = dyn_cast_or_null<VarDecl>(Val: Capture.getVariable());
1969 if (Var && Var->isInitCapture())
1970 TSI = Var->getTypeSourceInfo();
1971 }
1972
1973 // FIXME: Should we really be doing this? A null TypeSourceInfo seems more
1974 // appropriate, at least for an implicit capture.
1975 if (!TSI)
1976 TSI = Context.getTrivialTypeSourceInfo(T: FieldType, Loc);
1977
1978 // Build the non-static data member.
1979 FieldDecl *Field =
1980 FieldDecl::Create(Context, RD, /*StartLoc=*/Loc, /*IdLoc=*/Loc,
1981 /*Id=*/nullptr, FieldType, TSI, /*BW=*/nullptr,
1982 /*Mutable=*/false, ICIS_NoInit);
1983 // If the variable being captured has an invalid type, mark the class as
1984 // invalid as well.
1985 if (!FieldType->isDependentType()) {
1986 if (RequireCompleteSizedType(Loc, FieldType,
1987 diag::err_field_incomplete_or_sizeless)) {
1988 RD->setInvalidDecl();
1989 Field->setInvalidDecl();
1990 } else {
1991 NamedDecl *Def;
1992 FieldType->isIncompleteType(Def: &Def);
1993 if (Def && Def->isInvalidDecl()) {
1994 RD->setInvalidDecl();
1995 Field->setInvalidDecl();
1996 }
1997 }
1998 }
1999 Field->setImplicit(true);
2000 Field->setAccess(AS_private);
2001 RD->addDecl(Field);
2002
2003 if (Capture.isVLATypeCapture())
2004 Field->setCapturedVLAType(Capture.getCapturedVLAType());
2005
2006 return Field;
2007}
2008
2009ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc,
2010 LambdaScopeInfo *LSI) {
2011 // Collect information from the lambda scope.
2012 SmallVector<LambdaCapture, 4> Captures;
2013 SmallVector<Expr *, 4> CaptureInits;
2014 SourceLocation CaptureDefaultLoc = LSI->CaptureDefaultLoc;
2015 LambdaCaptureDefault CaptureDefault =
2016 mapImplicitCaptureStyle(ICS: LSI->ImpCaptureStyle);
2017 CXXRecordDecl *Class;
2018 CXXMethodDecl *CallOperator;
2019 SourceRange IntroducerRange;
2020 bool ExplicitParams;
2021 bool ExplicitResultType;
2022 CleanupInfo LambdaCleanup;
2023 bool ContainsUnexpandedParameterPack;
2024 bool IsGenericLambda;
2025 {
2026 CallOperator = LSI->CallOperator;
2027 Class = LSI->Lambda;
2028 IntroducerRange = LSI->IntroducerRange;
2029 ExplicitParams = LSI->ExplicitParams;
2030 ExplicitResultType = !LSI->HasImplicitReturnType;
2031 LambdaCleanup = LSI->Cleanup;
2032 ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack;
2033 IsGenericLambda = Class->isGenericLambda();
2034
2035 CallOperator->setLexicalDeclContext(Class);
2036 Decl *TemplateOrNonTemplateCallOperatorDecl =
2037 CallOperator->getDescribedFunctionTemplate()
2038 ? CallOperator->getDescribedFunctionTemplate()
2039 : cast<Decl>(Val: CallOperator);
2040
2041 // FIXME: Is this really the best choice? Keeping the lexical decl context
2042 // set as CurContext seems more faithful to the source.
2043 TemplateOrNonTemplateCallOperatorDecl->setLexicalDeclContext(Class);
2044
2045 PopExpressionEvaluationContext();
2046
2047 // True if the current capture has a used capture or default before it.
2048 bool CurHasPreviousCapture = CaptureDefault != LCD_None;
2049 SourceLocation PrevCaptureLoc = CurHasPreviousCapture ?
2050 CaptureDefaultLoc : IntroducerRange.getBegin();
2051
2052 for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
2053 const Capture &From = LSI->Captures[I];
2054
2055 if (From.isInvalid())
2056 return ExprError();
2057
2058 assert(!From.isBlockCapture() && "Cannot capture __block variables");
2059 bool IsImplicit = I >= LSI->NumExplicitCaptures;
2060 SourceLocation ImplicitCaptureLoc =
2061 IsImplicit ? CaptureDefaultLoc : SourceLocation();
2062
2063 // Use source ranges of explicit captures for fixits where available.
2064 SourceRange CaptureRange = LSI->ExplicitCaptureRanges[I];
2065
2066 // Warn about unused explicit captures.
2067 bool IsCaptureUsed = true;
2068 if (!CurContext->isDependentContext() && !IsImplicit &&
2069 !From.isODRUsed()) {
2070 // Initialized captures that are non-ODR used may not be eliminated.
2071 // FIXME: Where did the IsGenericLambda here come from?
2072 bool NonODRUsedInitCapture =
2073 IsGenericLambda && From.isNonODRUsed() && From.isInitCapture();
2074 if (!NonODRUsedInitCapture) {
2075 bool IsLast = (I + 1) == LSI->NumExplicitCaptures;
2076 SourceRange FixItRange;
2077 if (CaptureRange.isValid()) {
2078 if (!CurHasPreviousCapture && !IsLast) {
2079 // If there are no captures preceding this capture, remove the
2080 // following comma.
2081 FixItRange = SourceRange(CaptureRange.getBegin(),
2082 getLocForEndOfToken(Loc: CaptureRange.getEnd()));
2083 } else {
2084 // Otherwise, remove the comma since the last used capture.
2085 FixItRange = SourceRange(getLocForEndOfToken(Loc: PrevCaptureLoc),
2086 CaptureRange.getEnd());
2087 }
2088 }
2089
2090 IsCaptureUsed = !DiagnoseUnusedLambdaCapture(CaptureRange: FixItRange, From);
2091 }
2092 }
2093
2094 if (CaptureRange.isValid()) {
2095 CurHasPreviousCapture |= IsCaptureUsed;
2096 PrevCaptureLoc = CaptureRange.getEnd();
2097 }
2098
2099 // Map the capture to our AST representation.
2100 LambdaCapture Capture = [&] {
2101 if (From.isThisCapture()) {
2102 // Capturing 'this' implicitly with a default of '[=]' is deprecated,
2103 // because it results in a reference capture. Don't warn prior to
2104 // C++2a; there's nothing that can be done about it before then.
2105 if (getLangOpts().CPlusPlus20 && IsImplicit &&
2106 CaptureDefault == LCD_ByCopy) {
2107 Diag(From.getLocation(), diag::warn_deprecated_this_capture);
2108 Diag(CaptureDefaultLoc, diag::note_deprecated_this_capture)
2109 << FixItHint::CreateInsertion(
2110 getLocForEndOfToken(CaptureDefaultLoc), ", this");
2111 }
2112 return LambdaCapture(From.getLocation(), IsImplicit,
2113 From.isCopyCapture() ? LCK_StarThis : LCK_This);
2114 } else if (From.isVLATypeCapture()) {
2115 return LambdaCapture(From.getLocation(), IsImplicit, LCK_VLAType);
2116 } else {
2117 assert(From.isVariableCapture() && "unknown kind of capture");
2118 ValueDecl *Var = From.getVariable();
2119 LambdaCaptureKind Kind =
2120 From.isCopyCapture() ? LCK_ByCopy : LCK_ByRef;
2121 return LambdaCapture(From.getLocation(), IsImplicit, Kind, Var,
2122 From.getEllipsisLoc());
2123 }
2124 }();
2125
2126 // Form the initializer for the capture field.
2127 ExprResult Init = BuildCaptureInit(Cap: From, ImplicitCaptureLoc);
2128
2129 // FIXME: Skip this capture if the capture is not used, the initializer
2130 // has no side-effects, the type of the capture is trivial, and the
2131 // lambda is not externally visible.
2132
2133 // Add a FieldDecl for the capture and form its initializer.
2134 BuildCaptureField(Class, From);
2135 Captures.push_back(Elt: Capture);
2136 CaptureInits.push_back(Elt: Init.get());
2137
2138 if (LangOpts.CUDA)
2139 CUDACheckLambdaCapture(D: CallOperator, Capture: From);
2140 }
2141
2142 Class->setCaptures(Context, Captures);
2143
2144 // C++11 [expr.prim.lambda]p6:
2145 // The closure type for a lambda-expression with no lambda-capture
2146 // has a public non-virtual non-explicit const conversion function
2147 // to pointer to function having the same parameter and return
2148 // types as the closure type's function call operator.
2149 if (Captures.empty() && CaptureDefault == LCD_None)
2150 addFunctionPointerConversions(S&: *this, IntroducerRange, Class,
2151 CallOperator);
2152
2153 // Objective-C++:
2154 // The closure type for a lambda-expression has a public non-virtual
2155 // non-explicit const conversion function to a block pointer having the
2156 // same parameter and return types as the closure type's function call
2157 // operator.
2158 // FIXME: Fix generic lambda to block conversions.
2159 if (getLangOpts().Blocks && getLangOpts().ObjC && !IsGenericLambda)
2160 addBlockPointerConversion(S&: *this, IntroducerRange, Class, CallOperator);
2161
2162 // Finalize the lambda class.
2163 SmallVector<Decl*, 4> Fields(Class->fields());
2164 ActOnFields(S: nullptr, RecLoc: Class->getLocation(), TagDecl: Class, Fields, LBrac: SourceLocation(),
2165 RBrac: SourceLocation(), AttrList: ParsedAttributesView());
2166 CheckCompletedCXXClass(S: nullptr, Record: Class);
2167 }
2168
2169 Cleanup.mergeFrom(Rhs: LambdaCleanup);
2170
2171 LambdaExpr *Lambda = LambdaExpr::Create(C: Context, Class, IntroducerRange,
2172 CaptureDefault, CaptureDefaultLoc,
2173 ExplicitParams, ExplicitResultType,
2174 CaptureInits, ClosingBrace: EndLoc,
2175 ContainsUnexpandedParameterPack);
2176 // If the lambda expression's call operator is not explicitly marked constexpr
2177 // and we are not in a dependent context, analyze the call operator to infer
2178 // its constexpr-ness, suppressing diagnostics while doing so.
2179 if (getLangOpts().CPlusPlus17 && !CallOperator->isInvalidDecl() &&
2180 !CallOperator->isConstexpr() &&
2181 !isa<CoroutineBodyStmt>(Val: CallOperator->getBody()) &&
2182 !Class->getDeclContext()->isDependentContext()) {
2183 CallOperator->setConstexprKind(
2184 CheckConstexprFunctionDefinition(CallOperator,
2185 CheckConstexprKind::CheckValid)
2186 ? ConstexprSpecKind::Constexpr
2187 : ConstexprSpecKind::Unspecified);
2188 }
2189
2190 // Emit delayed shadowing warnings now that the full capture list is known.
2191 DiagnoseShadowingLambdaDecls(LSI);
2192
2193 if (!CurContext->isDependentContext()) {
2194 switch (ExprEvalContexts.back().Context) {
2195 // C++11 [expr.prim.lambda]p2:
2196 // A lambda-expression shall not appear in an unevaluated operand
2197 // (Clause 5).
2198 case ExpressionEvaluationContext::Unevaluated:
2199 case ExpressionEvaluationContext::UnevaluatedList:
2200 case ExpressionEvaluationContext::UnevaluatedAbstract:
2201 // C++1y [expr.const]p2:
2202 // A conditional-expression e is a core constant expression unless the
2203 // evaluation of e, following the rules of the abstract machine, would
2204 // evaluate [...] a lambda-expression.
2205 //
2206 // This is technically incorrect, there are some constant evaluated contexts
2207 // where this should be allowed. We should probably fix this when DR1607 is
2208 // ratified, it lays out the exact set of conditions where we shouldn't
2209 // allow a lambda-expression.
2210 case ExpressionEvaluationContext::ConstantEvaluated:
2211 case ExpressionEvaluationContext::ImmediateFunctionContext:
2212 // We don't actually diagnose this case immediately, because we
2213 // could be within a context where we might find out later that
2214 // the expression is potentially evaluated (e.g., for typeid).
2215 ExprEvalContexts.back().Lambdas.push_back(Elt: Lambda);
2216 break;
2217
2218 case ExpressionEvaluationContext::DiscardedStatement:
2219 case ExpressionEvaluationContext::PotentiallyEvaluated:
2220 case ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed:
2221 break;
2222 }
2223 }
2224
2225 return MaybeBindToTemporary(Lambda);
2226}
2227
2228ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation,
2229 SourceLocation ConvLocation,
2230 CXXConversionDecl *Conv,
2231 Expr *Src) {
2232 // Make sure that the lambda call operator is marked used.
2233 CXXRecordDecl *Lambda = Conv->getParent();
2234 CXXMethodDecl *CallOperator
2235 = cast<CXXMethodDecl>(
2236 Lambda->lookup(
2237 Context.DeclarationNames.getCXXOperatorName(Op: OO_Call)).front());
2238 CallOperator->setReferenced();
2239 CallOperator->markUsed(Context);
2240
2241 ExprResult Init = PerformCopyInitialization(
2242 Entity: InitializedEntity::InitializeLambdaToBlock(BlockVarLoc: ConvLocation, Type: Src->getType()),
2243 EqualLoc: CurrentLocation, Init: Src);
2244 if (!Init.isInvalid())
2245 Init = ActOnFinishFullExpr(Expr: Init.get(), /*DiscardedValue*/ false);
2246
2247 if (Init.isInvalid())
2248 return ExprError();
2249
2250 // Create the new block to be returned.
2251 BlockDecl *Block = BlockDecl::Create(C&: Context, DC: CurContext, L: ConvLocation);
2252
2253 // Set the type information.
2254 Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo());
2255 Block->setIsVariadic(CallOperator->isVariadic());
2256 Block->setBlockMissingReturnType(false);
2257
2258 // Add parameters.
2259 SmallVector<ParmVarDecl *, 4> BlockParams;
2260 for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) {
2261 ParmVarDecl *From = CallOperator->getParamDecl(I);
2262 BlockParams.push_back(Elt: ParmVarDecl::Create(
2263 C&: Context, DC: Block, StartLoc: From->getBeginLoc(), IdLoc: From->getLocation(),
2264 Id: From->getIdentifier(), T: From->getType(), TInfo: From->getTypeSourceInfo(),
2265 S: From->getStorageClass(),
2266 /*DefArg=*/nullptr));
2267 }
2268 Block->setParams(BlockParams);
2269
2270 Block->setIsConversionFromLambda(true);
2271
2272 // Add capture. The capture uses a fake variable, which doesn't correspond
2273 // to any actual memory location. However, the initializer copy-initializes
2274 // the lambda object.
2275 TypeSourceInfo *CapVarTSI =
2276 Context.getTrivialTypeSourceInfo(T: Src->getType());
2277 VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation,
2278 ConvLocation, nullptr,
2279 Src->getType(), CapVarTSI,
2280 SC_None);
2281 BlockDecl::Capture Capture(/*variable=*/CapVar, /*byRef=*/false,
2282 /*nested=*/false, /*copy=*/Init.get());
2283 Block->setCaptures(Context, Captures: Capture, /*CapturesCXXThis=*/false);
2284
2285 // Add a fake function body to the block. IR generation is responsible
2286 // for filling in the actual body, which cannot be expressed as an AST.
2287 Block->setBody(new (Context) CompoundStmt(ConvLocation));
2288
2289 // Create the block literal expression.
2290 Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType());
2291 ExprCleanupObjects.push_back(Elt: Block);
2292 Cleanup.setExprNeedsCleanups(true);
2293
2294 return BuildBlock;
2295}
2296
2297static FunctionDecl *getPatternFunctionDecl(FunctionDecl *FD) {
2298 if (FD->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization) {
2299 while (FD->getInstantiatedFromMemberFunction())
2300 FD = FD->getInstantiatedFromMemberFunction();
2301 return FD;
2302 }
2303
2304 if (FD->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate)
2305 return FD->getInstantiatedFromDecl();
2306
2307 FunctionTemplateDecl *FTD = FD->getPrimaryTemplate();
2308 if (!FTD)
2309 return nullptr;
2310
2311 while (FTD->getInstantiatedFromMemberTemplate())
2312 FTD = FTD->getInstantiatedFromMemberTemplate();
2313
2314 return FTD->getTemplatedDecl();
2315}
2316
2317Sema::LambdaScopeForCallOperatorInstantiationRAII::
2318 LambdaScopeForCallOperatorInstantiationRAII(
2319 Sema &SemaRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL,
2320 LocalInstantiationScope &Scope, bool ShouldAddDeclsFromParentScope)
2321 : FunctionScopeRAII(SemaRef) {
2322 if (!isLambdaCallOperator(FD)) {
2323 FunctionScopeRAII::disable();
2324 return;
2325 }
2326
2327 SemaRef.RebuildLambdaScopeInfo(CallOperator: cast<CXXMethodDecl>(Val: FD));
2328
2329 FunctionDecl *Pattern = getPatternFunctionDecl(FD);
2330 if (Pattern) {
2331 SemaRef.addInstantiatedCapturesToScope(Function: FD, PatternDecl: Pattern, Scope, TemplateArgs: MLTAL);
2332
2333 FunctionDecl *ParentFD = FD;
2334 while (ShouldAddDeclsFromParentScope) {
2335
2336 ParentFD =
2337 dyn_cast<FunctionDecl>(Val: getLambdaAwareParentOfDeclContext(ParentFD));
2338 Pattern =
2339 dyn_cast<FunctionDecl>(Val: getLambdaAwareParentOfDeclContext(Pattern));
2340
2341 if (!FD || !Pattern)
2342 break;
2343
2344 SemaRef.addInstantiatedParametersToScope(Function: ParentFD, PatternDecl: Pattern, Scope, TemplateArgs: MLTAL);
2345 SemaRef.addInstantiatedLocalVarsToScope(Function: ParentFD, PatternDecl: Pattern, Scope);
2346 }
2347 }
2348}
2349

source code of clang/lib/Sema/SemaLambda.cpp