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