clang 19.0.0git
SemaLambda.cpp
Go to the documentation of this file.
1//===--- SemaLambda.cpp - Semantic Analysis for C++11 Lambdas -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements semantic analysis for C++ lambda expressions.
10//
11//===----------------------------------------------------------------------===//
12#include "clang/Sema/DeclSpec.h"
13#include "TypeLocBuilder.h"
14#include "clang/AST/ASTLambda.h"
15#include "clang/AST/ExprCXX.h"
18#include "clang/Sema/Lookup.h"
19#include "clang/Sema/Scope.h"
23#include "clang/Sema/Template.h"
24#include "llvm/ADT/STLExtras.h"
25#include <optional>
26using namespace clang;
27using namespace sema;
28
29/// Examines the FunctionScopeInfo stack to determine the nearest
30/// enclosing lambda (to the current lambda) that is 'capture-ready' for
31/// the variable referenced in the current lambda (i.e. \p VarToCapture).
32/// If successful, returns the index into Sema's FunctionScopeInfo stack
33/// of the capture-ready lambda's LambdaScopeInfo.
34///
35/// Climbs down the stack of lambdas (deepest nested lambda - i.e. current
36/// lambda - is on top) to determine the index of the nearest enclosing/outer
37/// lambda that is ready to capture the \p VarToCapture being referenced in
38/// the current lambda.
39/// As we climb down the stack, we want the index of the first such lambda -
40/// that is the lambda with the highest index that is 'capture-ready'.
41///
42/// A lambda 'L' is capture-ready for 'V' (var or this) if:
43/// - its enclosing context is non-dependent
44/// - and if the chain of lambdas between L and the lambda in which
45/// V is potentially used (i.e. the lambda at the top of the scope info
46/// stack), can all capture or have already captured V.
47/// If \p VarToCapture is 'null' then we are trying to capture 'this'.
48///
49/// Note that a lambda that is deemed 'capture-ready' still needs to be checked
50/// for whether it is 'capture-capable' (see
51/// getStackIndexOfNearestEnclosingCaptureCapableLambda), before it can truly
52/// capture.
53///
54/// \param FunctionScopes - Sema's stack of nested FunctionScopeInfo's (which a
55/// LambdaScopeInfo inherits from). The current/deepest/innermost lambda
56/// is at the top of the stack and has the highest index.
57/// \param VarToCapture - the variable to capture. If NULL, capture 'this'.
58///
59/// \returns An std::optional<unsigned> Index that if evaluates to 'true'
60/// contains the index (into Sema's FunctionScopeInfo stack) of the innermost
61/// lambda which is capture-ready. If the return value evaluates to 'false'
62/// then no lambda is capture-ready for \p VarToCapture.
63
64static inline std::optional<unsigned>
67 ValueDecl *VarToCapture) {
68 // Label failure to capture.
69 const std::optional<unsigned> NoLambdaIsCaptureReady;
70
71 // Ignore all inner captured regions.
72 unsigned CurScopeIndex = FunctionScopes.size() - 1;
73 while (CurScopeIndex > 0 && isa<clang::sema::CapturedRegionScopeInfo>(
74 FunctionScopes[CurScopeIndex]))
75 --CurScopeIndex;
76 assert(
77 isa<clang::sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex]) &&
78 "The function on the top of sema's function-info stack must be a lambda");
79
80 // If VarToCapture is null, we are attempting to capture 'this'.
81 const bool IsCapturingThis = !VarToCapture;
82 const bool IsCapturingVariable = !IsCapturingThis;
83
84 // Start with the current lambda at the top of the stack (highest index).
85 DeclContext *EnclosingDC =
86 cast<sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex])->CallOperator;
87
88 do {
90 cast<sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex]);
91 // IF we have climbed down to an intervening enclosing lambda that contains
92 // the variable declaration - it obviously can/must not capture the
93 // variable.
94 // Since its enclosing DC is dependent, all the lambdas between it and the
95 // innermost nested lambda are dependent (otherwise we wouldn't have
96 // arrived here) - so we don't yet have a lambda that can capture the
97 // variable.
98 if (IsCapturingVariable &&
99 VarToCapture->getDeclContext()->Equals(EnclosingDC))
100 return NoLambdaIsCaptureReady;
101
102 // For an enclosing lambda to be capture ready for an entity, all
103 // intervening lambda's have to be able to capture that entity. If even
104 // one of the intervening lambda's is not capable of capturing the entity
105 // then no enclosing lambda can ever capture that entity.
106 // For e.g.
107 // const int x = 10;
108 // [=](auto a) { #1
109 // [](auto b) { #2 <-- an intervening lambda that can never capture 'x'
110 // [=](auto c) { #3
111 // f(x, c); <-- can not lead to x's speculative capture by #1 or #2
112 // }; }; };
113 // If they do not have a default implicit capture, check to see
114 // if the entity has already been explicitly captured.
115 // If even a single dependent enclosing lambda lacks the capability
116 // to ever capture this variable, there is no further enclosing
117 // non-dependent lambda that can capture this variable.
118 if (LSI->ImpCaptureStyle == sema::LambdaScopeInfo::ImpCap_None) {
119 if (IsCapturingVariable && !LSI->isCaptured(VarToCapture))
120 return NoLambdaIsCaptureReady;
121 if (IsCapturingThis && !LSI->isCXXThisCaptured())
122 return NoLambdaIsCaptureReady;
123 }
124 EnclosingDC = getLambdaAwareParentOfDeclContext(EnclosingDC);
125
126 assert(CurScopeIndex);
127 --CurScopeIndex;
128 } while (!EnclosingDC->isTranslationUnit() &&
129 EnclosingDC->isDependentContext() &&
130 isLambdaCallOperator(EnclosingDC));
131
132 assert(CurScopeIndex < (FunctionScopes.size() - 1));
133 // If the enclosingDC is not dependent, then the immediately nested lambda
134 // (one index above) is capture-ready.
135 if (!EnclosingDC->isDependentContext())
136 return CurScopeIndex + 1;
137 return NoLambdaIsCaptureReady;
138}
139
140/// Examines the FunctionScopeInfo stack to determine the nearest
141/// enclosing lambda (to the current lambda) that is 'capture-capable' for
142/// the variable referenced in the current lambda (i.e. \p VarToCapture).
143/// If successful, returns the index into Sema's FunctionScopeInfo stack
144/// of the capture-capable lambda's LambdaScopeInfo.
145///
146/// Given the current stack of lambdas being processed by Sema and
147/// the variable of interest, to identify the nearest enclosing lambda (to the
148/// current lambda at the top of the stack) that can truly capture
149/// a variable, it has to have the following two properties:
150/// a) 'capture-ready' - be the innermost lambda that is 'capture-ready':
151/// - climb down the stack (i.e. starting from the innermost and examining
152/// each outer lambda step by step) checking if each enclosing
153/// lambda can either implicitly or explicitly capture the variable.
154/// Record the first such lambda that is enclosed in a non-dependent
155/// context. If no such lambda currently exists return failure.
156/// b) 'capture-capable' - make sure the 'capture-ready' lambda can truly
157/// capture the variable by checking all its enclosing lambdas:
158/// - check if all outer lambdas enclosing the 'capture-ready' lambda
159/// identified above in 'a' can also capture the variable (this is done
160/// via tryCaptureVariable for variables and CheckCXXThisCapture for
161/// 'this' by passing in the index of the Lambda identified in step 'a')
162///
163/// \param FunctionScopes - Sema's stack of nested FunctionScopeInfo's (which a
164/// LambdaScopeInfo inherits from). The current/deepest/innermost lambda
165/// is at the top of the stack.
166///
167/// \param VarToCapture - the variable to capture. If NULL, capture 'this'.
168///
169///
170/// \returns An std::optional<unsigned> Index that if evaluates to 'true'
171/// contains the index (into Sema's FunctionScopeInfo stack) of the innermost
172/// lambda which is capture-capable. If the return value evaluates to 'false'
173/// then no lambda is capture-capable for \p VarToCapture.
174
175std::optional<unsigned>
178 ValueDecl *VarToCapture, Sema &S) {
179
180 const std::optional<unsigned> NoLambdaIsCaptureCapable;
181
182 const std::optional<unsigned> OptionalStackIndex =
184 VarToCapture);
185 if (!OptionalStackIndex)
186 return NoLambdaIsCaptureCapable;
187
188 const unsigned IndexOfCaptureReadyLambda = *OptionalStackIndex;
189 assert(((IndexOfCaptureReadyLambda != (FunctionScopes.size() - 1)) ||
190 S.getCurGenericLambda()) &&
191 "The capture ready lambda for a potential capture can only be the "
192 "current lambda if it is a generic lambda");
193
194 const sema::LambdaScopeInfo *const CaptureReadyLambdaLSI =
195 cast<sema::LambdaScopeInfo>(FunctionScopes[IndexOfCaptureReadyLambda]);
196
197 // If VarToCapture is null, we are attempting to capture 'this'
198 const bool IsCapturingThis = !VarToCapture;
199 const bool IsCapturingVariable = !IsCapturingThis;
200
201 if (IsCapturingVariable) {
202 // Check if the capture-ready lambda can truly capture the variable, by
203 // checking whether all enclosing lambdas of the capture-ready lambda allow
204 // the capture - i.e. make sure it is capture-capable.
205 QualType CaptureType, DeclRefType;
206 const bool CanCaptureVariable =
207 !S.tryCaptureVariable(VarToCapture,
208 /*ExprVarIsUsedInLoc*/ SourceLocation(),
210 /*EllipsisLoc*/ SourceLocation(),
211 /*BuildAndDiagnose*/ false, CaptureType,
212 DeclRefType, &IndexOfCaptureReadyLambda);
213 if (!CanCaptureVariable)
214 return NoLambdaIsCaptureCapable;
215 } else {
216 // Check if the capture-ready lambda can truly capture 'this' by checking
217 // whether all enclosing lambdas of the capture-ready lambda can capture
218 // 'this'.
219 const bool CanCaptureThis =
221 CaptureReadyLambdaLSI->PotentialThisCaptureLocation,
222 /*Explicit*/ false, /*BuildAndDiagnose*/ false,
223 &IndexOfCaptureReadyLambda);
224 if (!CanCaptureThis)
225 return NoLambdaIsCaptureCapable;
226 }
227 return IndexOfCaptureReadyLambda;
228}
229
230static inline TemplateParameterList *
232 if (!LSI->GLTemplateParameterList && !LSI->TemplateParams.empty()) {
233 LSI->GLTemplateParameterList = TemplateParameterList::Create(
234 SemaRef.Context,
235 /*Template kw loc*/ SourceLocation(),
236 /*L angle loc*/ LSI->ExplicitTemplateParamsRange.getBegin(),
237 LSI->TemplateParams,
238 /*R angle loc*/LSI->ExplicitTemplateParamsRange.getEnd(),
239 LSI->RequiresClause.get());
240 }
241 return LSI->GLTemplateParameterList;
242}
243
246 unsigned LambdaDependencyKind,
247 LambdaCaptureDefault CaptureDefault) {
249 while (!(DC->isFunctionOrMethod() || DC->isRecord() || DC->isFileContext()))
250 DC = DC->getParent();
251
252 bool IsGenericLambda =
254 // Start constructing the lambda class.
256 Context, DC, Info, IntroducerRange.getBegin(), LambdaDependencyKind,
257 IsGenericLambda, CaptureDefault);
258 DC->addDecl(Class);
259
260 return Class;
261}
262
263/// Determine whether the given context is or is enclosed in an inline
264/// function.
265static bool isInInlineFunction(const DeclContext *DC) {
266 while (!DC->isFileContext()) {
267 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC))
268 if (FD->isInlined())
269 return true;
270
271 DC = DC->getLexicalParent();
272 }
273
274 return false;
275}
276
277std::tuple<MangleNumberingContext *, Decl *>
279 // Compute the context for allocating mangling numbers in the current
280 // expression, if the ABI requires them.
281 Decl *ManglingContextDecl = ExprEvalContexts.back().ManglingContextDecl;
282
283 enum ContextKind {
284 Normal,
286 DataMember,
287 InlineVariable,
288 TemplatedVariable,
289 Concept
290 } Kind = Normal;
291
292 bool IsInNonspecializedTemplate =
294
295 // Default arguments of member function parameters that appear in a class
296 // definition, as well as the initializers of data members, receive special
297 // treatment. Identify them.
298 if (ManglingContextDecl) {
299 if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) {
300 if (const DeclContext *LexicalDC
301 = Param->getDeclContext()->getLexicalParent())
302 if (LexicalDC->isRecord())
303 Kind = DefaultArgument;
304 } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) {
305 if (Var->getMostRecentDecl()->isInline())
306 Kind = InlineVariable;
307 else if (Var->getDeclContext()->isRecord() && IsInNonspecializedTemplate)
308 Kind = TemplatedVariable;
309 else if (Var->getDescribedVarTemplate())
310 Kind = TemplatedVariable;
311 else if (auto *VTS = dyn_cast<VarTemplateSpecializationDecl>(Var)) {
312 if (!VTS->isExplicitSpecialization())
313 Kind = TemplatedVariable;
314 }
315 } else if (isa<FieldDecl>(ManglingContextDecl)) {
316 Kind = DataMember;
317 } else if (isa<ImplicitConceptSpecializationDecl>(ManglingContextDecl)) {
318 Kind = Concept;
319 }
320 }
321
322 // Itanium ABI [5.1.7]:
323 // In the following contexts [...] the one-definition rule requires closure
324 // types in different translation units to "correspond":
325 switch (Kind) {
326 case Normal: {
327 // -- the bodies of inline or templated functions
328 if ((IsInNonspecializedTemplate &&
329 !(ManglingContextDecl && isa<ParmVarDecl>(ManglingContextDecl))) ||
331 while (auto *CD = dyn_cast<CapturedDecl>(DC))
332 DC = CD->getParent();
333 return std::make_tuple(&Context.getManglingNumberContext(DC), nullptr);
334 }
335
336 return std::make_tuple(nullptr, nullptr);
337 }
338
339 case Concept:
340 // Concept definitions aren't code generated and thus aren't mangled,
341 // however the ManglingContextDecl is important for the purposes of
342 // re-forming the template argument list of the lambda for constraint
343 // evaluation.
344 case DataMember:
345 // -- default member initializers
346 case DefaultArgument:
347 // -- default arguments appearing in class definitions
348 case InlineVariable:
349 case TemplatedVariable:
350 // -- the initializers of inline or templated variables
351 return std::make_tuple(
353 ManglingContextDecl),
354 ManglingContextDecl);
355 }
356
357 llvm_unreachable("unexpected context");
358}
359
360static QualType
362 TemplateParameterList *TemplateParams,
363 TypeSourceInfo *MethodTypeInfo) {
364 assert(MethodTypeInfo && "expected a non null type");
365
366 QualType MethodType = MethodTypeInfo->getType();
367 // If a lambda appears in a dependent context or is a generic lambda (has
368 // template parameters) and has an 'auto' return type, deduce it to a
369 // dependent type.
370 if (Class->isDependentContext() || TemplateParams) {
371 const FunctionProtoType *FPT = MethodType->castAs<FunctionProtoType>();
373 if (Result->isUndeducedType()) {
375 MethodType = S.Context.getFunctionType(Result, FPT->getParamTypes(),
376 FPT->getExtProtoInfo());
377 }
378 }
379 return MethodType;
380}
381
382// [C++2b] [expr.prim.lambda.closure] p4
383// Given a lambda with a lambda-capture, the type of the explicit object
384// parameter, if any, of the lambda's function call operator (possibly
385// instantiated from a function call operator template) shall be either:
386// - the closure type,
387// - class type derived from the closure type, or
388// - a reference to a possibly cv-qualified such type.
390 CXXMethodDecl *Method) {
392 return;
393 CXXRecordDecl *RD = Method->getParent();
394 if (Method->getType()->isDependentType())
395 return;
396 if (RD->isCapturelessLambda())
397 return;
398 QualType ExplicitObjectParameterType = Method->getParamDecl(0)
399 ->getType()
403 QualType LambdaType = getASTContext().getRecordType(RD);
404 if (LambdaType == ExplicitObjectParameterType)
405 return;
406 if (IsDerivedFrom(RD->getLocation(), ExplicitObjectParameterType, LambdaType))
407 return;
408 Diag(Method->getParamDecl(0)->getLocation(),
409 diag::err_invalid_explicit_object_type_in_lambda)
410 << ExplicitObjectParameterType;
411}
412
415 std::optional<CXXRecordDecl::LambdaNumbering> NumberingOverride) {
416 if (NumberingOverride) {
417 Class->setLambdaNumbering(*NumberingOverride);
418 return;
419 }
420
421 ContextRAII ManglingContext(*this, Class->getDeclContext());
422
423 auto getMangleNumberingContext =
424 [this](CXXRecordDecl *Class,
425 Decl *ManglingContextDecl) -> MangleNumberingContext * {
426 // Get mangle numbering context if there's any extra decl context.
427 if (ManglingContextDecl)
429 ASTContext::NeedExtraManglingDecl, ManglingContextDecl);
430 // Otherwise, from that lambda's decl context.
431 auto DC = Class->getDeclContext();
432 while (auto *CD = dyn_cast<CapturedDecl>(DC))
433 DC = CD->getParent();
435 };
436
439 std::tie(MCtx, Numbering.ContextDecl) =
440 getCurrentMangleNumberContext(Class->getDeclContext());
441 if (!MCtx && (getLangOpts().CUDA || getLangOpts().SYCLIsDevice ||
442 getLangOpts().SYCLIsHost)) {
443 // Force lambda numbering in CUDA/HIP as we need to name lambdas following
444 // ODR. Both device- and host-compilation need to have a consistent naming
445 // on kernel functions. As lambdas are potential part of these `__global__`
446 // function names, they needs numbering following ODR.
447 // Also force for SYCL, since we need this for the
448 // __builtin_sycl_unique_stable_name implementation, which depends on lambda
449 // mangling.
450 MCtx = getMangleNumberingContext(Class, Numbering.ContextDecl);
451 assert(MCtx && "Retrieving mangle numbering context failed!");
452 Numbering.HasKnownInternalLinkage = true;
453 }
454 if (MCtx) {
455 Numbering.IndexInContext = MCtx->getNextLambdaIndex();
456 Numbering.ManglingNumber = MCtx->getManglingNumber(Method);
457 Numbering.DeviceManglingNumber = MCtx->getDeviceManglingNumber(Method);
458 Class->setLambdaNumbering(Numbering);
459
460 if (auto *Source =
461 dyn_cast_or_null<ExternalSemaSource>(Context.getExternalSource()))
462 Source->AssignedLambdaNumbering(Class);
463 }
464}
465
467 CXXMethodDecl *CallOperator,
468 bool ExplicitResultType) {
469 if (ExplicitResultType) {
470 LSI->HasImplicitReturnType = false;
471 LSI->ReturnType = CallOperator->getReturnType();
472 if (!LSI->ReturnType->isDependentType() && !LSI->ReturnType->isVoidType())
473 S.RequireCompleteType(CallOperator->getBeginLoc(), LSI->ReturnType,
474 diag::err_lambda_incomplete_result);
475 } else {
476 LSI->HasImplicitReturnType = true;
477 }
478}
479
481 SourceRange IntroducerRange,
482 LambdaCaptureDefault CaptureDefault,
483 SourceLocation CaptureDefaultLoc,
484 bool ExplicitParams, bool Mutable) {
485 LSI->CallOperator = CallOperator;
486 CXXRecordDecl *LambdaClass = CallOperator->getParent();
487 LSI->Lambda = LambdaClass;
488 if (CaptureDefault == LCD_ByCopy)
489 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval;
490 else if (CaptureDefault == LCD_ByRef)
491 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref;
492 LSI->CaptureDefaultLoc = CaptureDefaultLoc;
493 LSI->IntroducerRange = IntroducerRange;
494 LSI->ExplicitParams = ExplicitParams;
495 LSI->Mutable = Mutable;
496}
497
500}
501
503 LambdaIntroducer &Intro, SourceLocation LAngleLoc,
504 ArrayRef<NamedDecl *> TParams, SourceLocation RAngleLoc,
505 ExprResult RequiresClause) {
507 assert(LSI && "Expected a lambda scope");
508 assert(LSI->NumExplicitTemplateParams == 0 &&
509 "Already acted on explicit template parameters");
510 assert(LSI->TemplateParams.empty() &&
511 "Explicit template parameters should come "
512 "before invented (auto) ones");
513 assert(!TParams.empty() &&
514 "No template parameters to act on");
515 LSI->TemplateParams.append(TParams.begin(), TParams.end());
516 LSI->NumExplicitTemplateParams = TParams.size();
517 LSI->ExplicitTemplateParamsRange = {LAngleLoc, RAngleLoc};
518 LSI->RequiresClause = RequiresClause;
519}
520
521/// If this expression is an enumerator-like expression of some type
522/// T, return the type T; otherwise, return null.
523///
524/// Pointer comparisons on the result here should always work because
525/// it's derived from either the parent of an EnumConstantDecl
526/// (i.e. the definition) or the declaration returned by
527/// EnumType::getDecl() (i.e. the definition).
529 // An expression is an enumerator-like expression of type T if,
530 // ignoring parens and parens-like expressions:
531 E = E->IgnoreParens();
532
533 // - it is an enumerator whose enum type is T or
534 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
535 if (EnumConstantDecl *D
536 = dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
537 return cast<EnumDecl>(D->getDeclContext());
538 }
539 return nullptr;
540 }
541
542 // - it is a comma expression whose RHS is an enumerator-like
543 // expression of type T or
544 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
545 if (BO->getOpcode() == BO_Comma)
546 return findEnumForBlockReturn(BO->getRHS());
547 return nullptr;
548 }
549
550 // - it is a statement-expression whose value expression is an
551 // enumerator-like expression of type T or
552 if (StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
553 if (Expr *last = dyn_cast_or_null<Expr>(SE->getSubStmt()->body_back()))
554 return findEnumForBlockReturn(last);
555 return nullptr;
556 }
557
558 // - it is a ternary conditional operator (not the GNU ?:
559 // extension) whose second and third operands are
560 // enumerator-like expressions of type T or
561 if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) {
562 if (EnumDecl *ED = findEnumForBlockReturn(CO->getTrueExpr()))
563 if (ED == findEnumForBlockReturn(CO->getFalseExpr()))
564 return ED;
565 return nullptr;
566 }
567
568 // (implicitly:)
569 // - it is an implicit integral conversion applied to an
570 // enumerator-like expression of type T or
571 if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
572 // We can sometimes see integral conversions in valid
573 // enumerator-like expressions.
574 if (ICE->getCastKind() == CK_IntegralCast)
575 return findEnumForBlockReturn(ICE->getSubExpr());
576
577 // Otherwise, just rely on the type.
578 }
579
580 // - it is an expression of that formal enum type.
581 if (const EnumType *ET = E->getType()->getAs<EnumType>()) {
582 return ET->getDecl();
583 }
584
585 // Otherwise, nope.
586 return nullptr;
587}
588
589/// Attempt to find a type T for which the returned expression of the
590/// given statement is an enumerator-like expression of that type.
592 if (Expr *retValue = ret->getRetValue())
593 return findEnumForBlockReturn(retValue);
594 return nullptr;
595}
596
597/// Attempt to find a common type T for which all of the returned
598/// expressions in a block are enumerator-like expressions of that
599/// type.
601 ArrayRef<ReturnStmt*>::iterator i = returns.begin(), e = returns.end();
602
603 // Try to find one for the first return.
605 if (!ED) return nullptr;
606
607 // Check that the rest of the returns have the same enum.
608 for (++i; i != e; ++i) {
609 if (findEnumForBlockReturn(*i) != ED)
610 return nullptr;
611 }
612
613 // Never infer an anonymous enum type.
614 if (!ED->hasNameForLinkage()) return nullptr;
615
616 return ED;
617}
618
619/// Adjust the given return statements so that they formally return
620/// the given type. It should require, at most, an IntegralCast.
622 QualType returnType) {
624 i = returns.begin(), e = returns.end(); i != e; ++i) {
625 ReturnStmt *ret = *i;
626 Expr *retValue = ret->getRetValue();
627 if (S.Context.hasSameType(retValue->getType(), returnType))
628 continue;
629
630 // Right now we only support integral fixup casts.
631 assert(returnType->isIntegralOrUnscopedEnumerationType());
632 assert(retValue->getType()->isIntegralOrUnscopedEnumerationType());
633
634 ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(retValue);
635
636 Expr *E = (cleanups ? cleanups->getSubExpr() : retValue);
637 E = ImplicitCastExpr::Create(S.Context, returnType, CK_IntegralCast, E,
638 /*base path*/ nullptr, VK_PRValue,
640 if (cleanups) {
641 cleanups->setSubExpr(E);
642 } else {
643 ret->setRetValue(E);
644 }
645 }
646}
647
649 assert(CSI.HasImplicitReturnType);
650 // If it was ever a placeholder, it had to been deduced to DependentTy.
651 assert(CSI.ReturnType.isNull() || !CSI.ReturnType->isUndeducedType());
652 assert((!isa<LambdaScopeInfo>(CSI) || !getLangOpts().CPlusPlus14) &&
653 "lambda expressions use auto deduction in C++14 onwards");
654
655 // C++ core issue 975:
656 // If a lambda-expression does not include a trailing-return-type,
657 // it is as if the trailing-return-type denotes the following type:
658 // - if there are no return statements in the compound-statement,
659 // or all return statements return either an expression of type
660 // void or no expression or braced-init-list, the type void;
661 // - otherwise, if all return statements return an expression
662 // and the types of the returned expressions after
663 // lvalue-to-rvalue conversion (4.1 [conv.lval]),
664 // array-to-pointer conversion (4.2 [conv.array]), and
665 // function-to-pointer conversion (4.3 [conv.func]) are the
666 // same, that common type;
667 // - otherwise, the program is ill-formed.
668 //
669 // C++ core issue 1048 additionally removes top-level cv-qualifiers
670 // from the types of returned expressions to match the C++14 auto
671 // deduction rules.
672 //
673 // In addition, in blocks in non-C++ modes, if all of the return
674 // statements are enumerator-like expressions of some type T, where
675 // T has a name for linkage, then we infer the return type of the
676 // block to be that type.
677
678 // First case: no return statements, implicit void return type.
679 ASTContext &Ctx = getASTContext();
680 if (CSI.Returns.empty()) {
681 // It's possible there were simply no /valid/ return statements.
682 // In this case, the first one we found may have at least given us a type.
683 if (CSI.ReturnType.isNull())
684 CSI.ReturnType = Ctx.VoidTy;
685 return;
686 }
687
688 // Second case: at least one return statement has dependent type.
689 // Delay type checking until instantiation.
690 assert(!CSI.ReturnType.isNull() && "We should have a tentative return type.");
691 if (CSI.ReturnType->isDependentType())
692 return;
693
694 // Try to apply the enum-fuzz rule.
695 if (!getLangOpts().CPlusPlus) {
696 assert(isa<BlockScopeInfo>(CSI));
698 if (ED) {
701 return;
702 }
703 }
704
705 // Third case: only one return statement. Don't bother doing extra work!
706 if (CSI.Returns.size() == 1)
707 return;
708
709 // General case: many return statements.
710 // Check that they all have compatible return types.
711
712 // We require the return types to strictly match here.
713 // Note that we've already done the required promotions as part of
714 // processing the return statement.
715 for (const ReturnStmt *RS : CSI.Returns) {
716 const Expr *RetE = RS->getRetValue();
717
718 QualType ReturnType =
719 (RetE ? RetE->getType() : Context.VoidTy).getUnqualifiedType();
720 if (Context.getCanonicalFunctionResultType(ReturnType) ==
722 // Use the return type with the strictest possible nullability annotation.
723 auto RetTyNullability = ReturnType->getNullability();
724 auto BlockNullability = CSI.ReturnType->getNullability();
725 if (BlockNullability &&
726 (!RetTyNullability ||
727 hasWeakerNullability(*RetTyNullability, *BlockNullability)))
728 CSI.ReturnType = ReturnType;
729 continue;
730 }
731
732 // FIXME: This is a poor diagnostic for ReturnStmts without expressions.
733 // TODO: It's possible that the *first* return is the divergent one.
734 Diag(RS->getBeginLoc(),
735 diag::err_typecheck_missing_return_type_incompatible)
736 << ReturnType << CSI.ReturnType << isa<LambdaScopeInfo>(CSI);
737 // Continue iterating so that we keep emitting diagnostics.
738 }
739}
740
742 SourceLocation Loc, bool ByRef, SourceLocation EllipsisLoc,
743 std::optional<unsigned> NumExpansions, IdentifierInfo *Id,
744 bool IsDirectInit, Expr *&Init) {
745 // Create an 'auto' or 'auto&' TypeSourceInfo that we can use to
746 // deduce against.
747 QualType DeductType = Context.getAutoDeductType();
748 TypeLocBuilder TLB;
749 AutoTypeLoc TL = TLB.push<AutoTypeLoc>(DeductType);
750 TL.setNameLoc(Loc);
751 if (ByRef) {
752 DeductType = BuildReferenceType(DeductType, true, Loc, Id);
753 assert(!DeductType.isNull() && "can't build reference to auto");
754 TLB.push<ReferenceTypeLoc>(DeductType).setSigilLoc(Loc);
755 }
756 if (EllipsisLoc.isValid()) {
757 if (Init->containsUnexpandedParameterPack()) {
758 Diag(EllipsisLoc, getLangOpts().CPlusPlus20
759 ? diag::warn_cxx17_compat_init_capture_pack
760 : diag::ext_init_capture_pack);
761 DeductType = Context.getPackExpansionType(DeductType, NumExpansions,
762 /*ExpectPackInType=*/false);
763 TLB.push<PackExpansionTypeLoc>(DeductType).setEllipsisLoc(EllipsisLoc);
764 } else {
765 // Just ignore the ellipsis for now and form a non-pack variable. We'll
766 // diagnose this later when we try to capture it.
767 }
768 }
769 TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType);
770
771 // Deduce the type of the init capture.
773 /*VarDecl*/nullptr, DeclarationName(Id), DeductType, TSI,
774 SourceRange(Loc, Loc), IsDirectInit, Init);
775 if (DeducedType.isNull())
776 return QualType();
777
778 // Are we a non-list direct initialization?
779 ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init);
780
781 // Perform initialization analysis and ensure any implicit conversions
782 // (such as lvalue-to-rvalue) are enforced.
783 InitializedEntity Entity =
785 InitializationKind Kind =
786 IsDirectInit
787 ? (CXXDirectInit ? InitializationKind::CreateDirect(
788 Loc, Init->getBeginLoc(), Init->getEndLoc())
790 : InitializationKind::CreateCopy(Loc, Init->getBeginLoc());
791
792 MultiExprArg Args = Init;
793 if (CXXDirectInit)
794 Args =
795 MultiExprArg(CXXDirectInit->getExprs(), CXXDirectInit->getNumExprs());
796 QualType DclT;
797 InitializationSequence InitSeq(*this, Entity, Kind, Args);
798 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT);
799
800 if (Result.isInvalid())
801 return QualType();
802
803 Init = Result.getAs<Expr>();
804 return DeducedType;
805}
806
808 SourceLocation Loc, QualType InitCaptureType, SourceLocation EllipsisLoc,
809 IdentifierInfo *Id, unsigned InitStyle, Expr *Init, DeclContext *DeclCtx) {
810 // FIXME: Retain the TypeSourceInfo from buildLambdaInitCaptureInitialization
811 // rather than reconstructing it here.
812 TypeSourceInfo *TSI = Context.getTrivialTypeSourceInfo(InitCaptureType, Loc);
813 if (auto PETL = TSI->getTypeLoc().getAs<PackExpansionTypeLoc>())
814 PETL.setEllipsisLoc(EllipsisLoc);
815
816 // Create a dummy variable representing the init-capture. This is not actually
817 // used as a variable, and only exists as a way to name and refer to the
818 // init-capture.
819 // FIXME: Pass in separate source locations for '&' and identifier.
820 VarDecl *NewVD = VarDecl::Create(Context, DeclCtx, Loc, Loc, Id,
821 InitCaptureType, TSI, SC_Auto);
822 NewVD->setInitCapture(true);
823 NewVD->setReferenced(true);
824 // FIXME: Pass in a VarDecl::InitializationStyle.
825 NewVD->setInitStyle(static_cast<VarDecl::InitializationStyle>(InitStyle));
826 NewVD->markUsed(Context);
827 NewVD->setInit(Init);
828 if (NewVD->isParameterPack())
829 getCurLambda()->LocalPacks.push_back(NewVD);
830 return NewVD;
831}
832
833void Sema::addInitCapture(LambdaScopeInfo *LSI, VarDecl *Var, bool ByRef) {
834 assert(Var->isInitCapture() && "init capture flag should be set");
835 LSI->addCapture(Var, /*isBlock=*/false, ByRef,
836 /*isNested=*/false, Var->getLocation(), SourceLocation(),
837 Var->getType(), /*Invalid=*/false);
838}
839
840// Unlike getCurLambda, getCurrentLambdaScopeUnsafe doesn't
841// check that the current lambda is in a consistent or fully constructed state.
843 assert(!S.FunctionScopes.empty());
844 return cast<LambdaScopeInfo>(S.FunctionScopes[S.FunctionScopes.size() - 1]);
845}
846
847static TypeSourceInfo *
849 // C++11 [expr.prim.lambda]p4:
850 // If a lambda-expression does not include a lambda-declarator, it is as
851 // if the lambda-declarator were ().
853 /*IsVariadic=*/false, /*IsCXXMethod=*/true));
854 EPI.HasTrailingReturn = true;
855 EPI.TypeQuals.addConst();
857 if (AS != LangAS::Default)
859
860 // C++1y [expr.prim.lambda]:
861 // The lambda return type is 'auto', which is replaced by the
862 // trailing-return type if provided and/or deduced from 'return'
863 // statements
864 // We don't do this before C++1y, because we don't support deduced return
865 // types there.
866 QualType DefaultTypeForNoTrailingReturn = S.getLangOpts().CPlusPlus14
869 QualType MethodTy = S.Context.getFunctionType(DefaultTypeForNoTrailingReturn,
870 std::nullopt, EPI);
871 return S.Context.getTrivialTypeSourceInfo(MethodTy, Loc);
872}
873
875 Declarator &ParamInfo, Scope *CurScope,
876 SourceLocation Loc,
877 bool &ExplicitResultType) {
878
879 ExplicitResultType = false;
880
881 assert(
882 (ParamInfo.getDeclSpec().getStorageClassSpec() ==
885 "Unexpected storage specifier");
886 bool IsLambdaStatic =
888
889 TypeSourceInfo *MethodTyInfo;
890
891 if (ParamInfo.getNumTypeObjects() == 0) {
892 MethodTyInfo = getDummyLambdaType(S, Loc);
893 } else {
894 // Check explicit parameters
895 S.CheckExplicitObjectLambda(ParamInfo);
896
898
899 bool HasExplicitObjectParameter =
901
902 ExplicitResultType = FTI.hasTrailingReturnType();
903 if (!FTI.hasMutableQualifier() && !IsLambdaStatic &&
904 !HasExplicitObjectParameter)
906
907 if (ExplicitResultType && S.getLangOpts().HLSL) {
908 QualType RetTy = FTI.getTrailingReturnType().get();
909 if (!RetTy.isNull()) {
910 // HLSL does not support specifying an address space on a lambda return
911 // type.
912 LangAS AddressSpace = RetTy.getAddressSpace();
913 if (AddressSpace != LangAS::Default)
915 diag::err_return_value_with_address_space);
916 }
917 }
918
919 MethodTyInfo = S.GetTypeForDeclarator(ParamInfo);
920 assert(MethodTyInfo && "no type from lambda-declarator");
921
922 // Check for unexpanded parameter packs in the method type.
923 if (MethodTyInfo->getType()->containsUnexpandedParameterPack())
924 S.DiagnoseUnexpandedParameterPack(Intro.Range.getBegin(), MethodTyInfo,
926 }
927 return MethodTyInfo;
928}
929
932
933 // C++20 [expr.prim.lambda.closure]p3:
934 // The closure type for a lambda-expression has a public inline function
935 // call operator (for a non-generic lambda) or function call operator
936 // template (for a generic lambda) whose parameters and return type are
937 // described by the lambda-expression's parameter-declaration-clause
938 // and trailing-return-type respectively.
939 DeclarationName MethodName =
941 DeclarationNameLoc MethodNameLoc =
945 DeclarationNameInfo(MethodName, IntroducerRange.getBegin(),
946 MethodNameLoc),
947 QualType(), /*Tinfo=*/nullptr, SC_None,
948 getCurFPFeatures().isFPConstrained(),
950 /*TrailingRequiresClause=*/nullptr);
951 Method->setAccess(AS_public);
952 return Method;
953}
954
956 CXXMethodDecl *CallOperator, CXXRecordDecl *Class,
957 TemplateParameterList *TemplateParams) {
958 assert(TemplateParams && "no template parameters");
960 Context, Class, CallOperator->getLocation(), CallOperator->getDeclName(),
961 TemplateParams, CallOperator);
962 TemplateMethod->setAccess(AS_public);
963 CallOperator->setDescribedFunctionTemplate(TemplateMethod);
964}
965
967 CXXMethodDecl *Method, SourceLocation LambdaLoc,
968 SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause,
969 TypeSourceInfo *MethodTyInfo, ConstexprSpecKind ConstexprKind,
971 bool HasExplicitResultType) {
972
974
975 if (TrailingRequiresClause)
976 Method->setTrailingRequiresClause(TrailingRequiresClause);
977
978 TemplateParameterList *TemplateParams =
980
981 DeclContext *DC = Method->getLexicalDeclContext();
982 Method->setLexicalDeclContext(LSI->Lambda);
983 if (TemplateParams) {
984 FunctionTemplateDecl *TemplateMethod =
986 assert(TemplateMethod &&
987 "AddTemplateParametersToLambdaCallOperator should have been called");
988
989 LSI->Lambda->addDecl(TemplateMethod);
990 TemplateMethod->setLexicalDeclContext(DC);
991 } else {
992 LSI->Lambda->addDecl(Method);
993 }
994 LSI->Lambda->setLambdaIsGeneric(TemplateParams);
995 LSI->Lambda->setLambdaTypeInfo(MethodTyInfo);
996
997 Method->setLexicalDeclContext(DC);
998 Method->setLocation(LambdaLoc);
999 Method->setInnerLocStart(CallOperatorLoc);
1000 Method->setTypeSourceInfo(MethodTyInfo);
1001 Method->setType(buildTypeForLambdaCallOperator(*this, LSI->Lambda,
1002 TemplateParams, MethodTyInfo));
1003 Method->setConstexprKind(ConstexprKind);
1004 Method->setStorageClass(SC);
1005 if (!Params.empty()) {
1006 CheckParmsForFunctionDef(Params, /*CheckParameterNames=*/false);
1007 Method->setParams(Params);
1008 for (auto P : Method->parameters()) {
1009 assert(P && "null in a parameter list");
1010 P->setOwningFunction(Method);
1011 }
1012 }
1013
1014 buildLambdaScopeReturnType(*this, LSI, Method, HasExplicitResultType);
1015}
1016
1018 Scope *CurrentScope) {
1019
1021 assert(LSI && "LambdaScopeInfo should be on stack!");
1022
1023 if (Intro.Default == LCD_ByCopy)
1024 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval;
1025 else if (Intro.Default == LCD_ByRef)
1026 LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref;
1027 LSI->CaptureDefaultLoc = Intro.DefaultLoc;
1028 LSI->IntroducerRange = Intro.Range;
1029 LSI->AfterParameterList = false;
1030
1031 assert(LSI->NumExplicitTemplateParams == 0);
1032
1033 // Determine if we're within a context where we know that the lambda will
1034 // be dependent, because there are template parameters in scope.
1035 CXXRecordDecl::LambdaDependencyKind LambdaDependencyKind =
1037 if (LSI->NumExplicitTemplateParams > 0) {
1038 Scope *TemplateParamScope = CurScope->getTemplateParamParent();
1039 assert(TemplateParamScope &&
1040 "Lambda with explicit template param list should establish a "
1041 "template param scope");
1042 assert(TemplateParamScope->getParent());
1043 if (TemplateParamScope->getParent()->getTemplateParamParent() != nullptr)
1044 LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent;
1045 } else if (CurScope->getTemplateParamParent() != nullptr) {
1046 LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent;
1047 }
1048
1050 Intro.Range, /*Info=*/nullptr, LambdaDependencyKind, Intro.Default);
1051 LSI->Lambda = Class;
1052
1054 LSI->CallOperator = Method;
1056
1057 PushDeclContext(CurScope, Method);
1058
1059 bool ContainsUnexpandedParameterPack = false;
1060
1061 // Distinct capture names, for diagnostics.
1062 llvm::DenseMap<IdentifierInfo *, ValueDecl *> CaptureNames;
1063
1064 // Handle explicit captures.
1065 SourceLocation PrevCaptureLoc =
1066 Intro.Default == LCD_None ? Intro.Range.getBegin() : Intro.DefaultLoc;
1067 for (auto C = Intro.Captures.begin(), E = Intro.Captures.end(); C != E;
1068 PrevCaptureLoc = C->Loc, ++C) {
1069 if (C->Kind == LCK_This || C->Kind == LCK_StarThis) {
1070 if (C->Kind == LCK_StarThis)
1071 Diag(C->Loc, !getLangOpts().CPlusPlus17
1072 ? diag::ext_star_this_lambda_capture_cxx17
1073 : diag::warn_cxx14_compat_star_this_lambda_capture);
1074
1075 // C++11 [expr.prim.lambda]p8:
1076 // An identifier or this shall not appear more than once in a
1077 // lambda-capture.
1078 if (LSI->isCXXThisCaptured()) {
1079 Diag(C->Loc, diag::err_capture_more_than_once)
1080 << "'this'" << SourceRange(LSI->getCXXThisCapture().getLocation())
1082 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1083 continue;
1084 }
1085
1086 // C++20 [expr.prim.lambda]p8:
1087 // If a lambda-capture includes a capture-default that is =,
1088 // each simple-capture of that lambda-capture shall be of the form
1089 // "&identifier", "this", or "* this". [ Note: The form [&,this] is
1090 // redundant but accepted for compatibility with ISO C++14. --end note ]
1091 if (Intro.Default == LCD_ByCopy && C->Kind != LCK_StarThis)
1092 Diag(C->Loc, !getLangOpts().CPlusPlus20
1093 ? diag::ext_equals_this_lambda_capture_cxx20
1094 : diag::warn_cxx17_compat_equals_this_lambda_capture);
1095
1096 // C++11 [expr.prim.lambda]p12:
1097 // If this is captured by a local lambda expression, its nearest
1098 // enclosing function shall be a non-static member function.
1099 QualType ThisCaptureType = getCurrentThisType();
1100 if (ThisCaptureType.isNull()) {
1101 Diag(C->Loc, diag::err_this_capture) << true;
1102 continue;
1103 }
1104
1105 CheckCXXThisCapture(C->Loc, /*Explicit=*/true, /*BuildAndDiagnose*/ true,
1106 /*FunctionScopeIndexToStopAtPtr*/ nullptr,
1107 C->Kind == LCK_StarThis);
1108 if (!LSI->Captures.empty())
1109 LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
1110 continue;
1111 }
1112
1113 assert(C->Id && "missing identifier for capture");
1114
1115 if (C->Init.isInvalid())
1116 continue;
1117
1118 ValueDecl *Var = nullptr;
1119 if (C->Init.isUsable()) {
1121 ? diag::warn_cxx11_compat_init_capture
1122 : diag::ext_init_capture);
1123
1124 // If the initializer expression is usable, but the InitCaptureType
1125 // is not, then an error has occurred - so ignore the capture for now.
1126 // for e.g., [n{0}] { }; <-- if no <initializer_list> is included.
1127 // FIXME: we should create the init capture variable and mark it invalid
1128 // in this case.
1129 if (C->InitCaptureType.get().isNull())
1130 continue;
1131
1132 if (C->Init.get()->containsUnexpandedParameterPack() &&
1133 !C->InitCaptureType.get()->getAs<PackExpansionType>())
1135
1136 unsigned InitStyle;
1137 switch (C->InitKind) {
1139 llvm_unreachable("not an init-capture?");
1141 InitStyle = VarDecl::CInit;
1142 break;
1144 InitStyle = VarDecl::CallInit;
1145 break;
1147 InitStyle = VarDecl::ListInit;
1148 break;
1149 }
1150 Var = createLambdaInitCaptureVarDecl(C->Loc, C->InitCaptureType.get(),
1151 C->EllipsisLoc, C->Id, InitStyle,
1152 C->Init.get(), Method);
1153 assert(Var && "createLambdaInitCaptureVarDecl returned a null VarDecl?");
1154 if (auto *V = dyn_cast<VarDecl>(Var))
1155 CheckShadow(CurrentScope, V);
1156 PushOnScopeChains(Var, CurrentScope, false);
1157 } else {
1158 assert(C->InitKind == LambdaCaptureInitKind::NoInit &&
1159 "init capture has valid but null init?");
1160
1161 // C++11 [expr.prim.lambda]p8:
1162 // If a lambda-capture includes a capture-default that is &, the
1163 // identifiers in the lambda-capture shall not be preceded by &.
1164 // If a lambda-capture includes a capture-default that is =, [...]
1165 // each identifier it contains shall be preceded by &.
1166 if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef) {
1167 Diag(C->Loc, diag::err_reference_capture_with_reference_default)
1169 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1170 continue;
1171 } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy) {
1172 Diag(C->Loc, diag::err_copy_capture_with_copy_default)
1174 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1175 continue;
1176 }
1177
1178 // C++11 [expr.prim.lambda]p10:
1179 // The identifiers in a capture-list are looked up using the usual
1180 // rules for unqualified name lookup (3.4.1)
1181 DeclarationNameInfo Name(C->Id, C->Loc);
1182 LookupResult R(*this, Name, LookupOrdinaryName);
1183 LookupName(R, CurScope);
1184 if (R.isAmbiguous())
1185 continue;
1186 if (R.empty()) {
1187 // FIXME: Disable corrections that would add qualification?
1188 CXXScopeSpec ScopeSpec;
1189 DeclFilterCCC<VarDecl> Validator{};
1190 if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator))
1191 continue;
1192 }
1193
1194 if (auto *BD = R.getAsSingle<BindingDecl>())
1195 Var = BD;
1196 else
1197 Var = R.getAsSingle<VarDecl>();
1198 if (Var && DiagnoseUseOfDecl(Var, C->Loc))
1199 continue;
1200 }
1201
1202 // C++11 [expr.prim.lambda]p10:
1203 // [...] each such lookup shall find a variable with automatic storage
1204 // duration declared in the reaching scope of the local lambda expression.
1205 // Note that the 'reaching scope' check happens in tryCaptureVariable().
1206 if (!Var) {
1207 Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id;
1208 continue;
1209 }
1210
1211 // C++11 [expr.prim.lambda]p8:
1212 // An identifier or this shall not appear more than once in a
1213 // lambda-capture.
1214 if (auto [It, Inserted] = CaptureNames.insert(std::pair{C->Id, Var});
1215 !Inserted) {
1216 if (C->InitKind == LambdaCaptureInitKind::NoInit &&
1217 !Var->isInitCapture()) {
1218 Diag(C->Loc, diag::err_capture_more_than_once)
1219 << C->Id << It->second->getBeginLoc()
1221 SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc));
1222 Var->setInvalidDecl();
1223 } else if (Var && Var->isPlaceholderVar(getLangOpts())) {
1225 } else {
1226 // Previous capture captured something different (one or both was
1227 // an init-capture): no fixit.
1228 Diag(C->Loc, diag::err_capture_more_than_once) << C->Id;
1229 continue;
1230 }
1231 }
1232
1233 // Ignore invalid decls; they'll just confuse the code later.
1234 if (Var->isInvalidDecl())
1235 continue;
1236
1237 VarDecl *Underlying = Var->getPotentiallyDecomposedVarDecl();
1238
1239 if (!Underlying->hasLocalStorage()) {
1240 Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id;
1241 Diag(Var->getLocation(), diag::note_previous_decl) << C->Id;
1242 continue;
1243 }
1244
1245 // C++11 [expr.prim.lambda]p23:
1246 // A capture followed by an ellipsis is a pack expansion (14.5.3).
1247 SourceLocation EllipsisLoc;
1248 if (C->EllipsisLoc.isValid()) {
1249 if (Var->isParameterPack()) {
1250 EllipsisLoc = C->EllipsisLoc;
1251 } else {
1252 Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs)
1253 << (C->Init.isUsable() ? C->Init.get()->getSourceRange()
1254 : SourceRange(C->Loc));
1255
1256 // Just ignore the ellipsis.
1257 }
1258 } else if (Var->isParameterPack()) {
1259 ContainsUnexpandedParameterPack = true;
1260 }
1261
1262 if (C->Init.isUsable()) {
1263 addInitCapture(LSI, cast<VarDecl>(Var), C->Kind == LCK_ByRef);
1264 PushOnScopeChains(Var, CurScope, false);
1265 } else {
1268 tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc);
1269 }
1270 if (!LSI->Captures.empty())
1271 LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange;
1272 }
1274 LSI->ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack;
1276}
1277
1279 SourceLocation MutableLoc) {
1280
1282 LSI->Mutable = MutableLoc.isValid();
1283 ContextRAII Context(*this, LSI->CallOperator, /*NewThisContext*/ false);
1284
1285 // C++11 [expr.prim.lambda]p9:
1286 // A lambda-expression whose smallest enclosing scope is a block scope is a
1287 // local lambda expression; any other lambda expression shall not have a
1288 // capture-default or simple-capture in its lambda-introducer.
1289 //
1290 // For simple-captures, this is covered by the check below that any named
1291 // entity is a variable that can be captured.
1292 //
1293 // For DR1632, we also allow a capture-default in any context where we can
1294 // odr-use 'this' (in particular, in a default initializer for a non-static
1295 // data member).
1296 if (Intro.Default != LCD_None &&
1297 !LSI->Lambda->getParent()->isFunctionOrMethod() &&
1298 (getCurrentThisType().isNull() ||
1299 CheckCXXThisCapture(SourceLocation(), /*Explicit=*/true,
1300 /*BuildAndDiagnose=*/false)))
1301 Diag(Intro.DefaultLoc, diag::err_capture_default_non_local);
1302}
1303
1307 PushDeclContext(LambdaScope, LSI->CallOperator);
1308
1309 for (const DeclaratorChunk::ParamInfo &P : Params) {
1310 auto *Param = cast<ParmVarDecl>(P.Param);
1311 Param->setOwningFunction(LSI->CallOperator);
1312 if (Param->getIdentifier())
1313 PushOnScopeChains(Param, LambdaScope, false);
1314 }
1315
1316 // After the parameter list, we may parse a noexcept/requires/trailing return
1317 // type which need to know whether the call operator constiture a dependent
1318 // context, so we need to setup the FunctionTemplateDecl of generic lambdas
1319 // now.
1320 TemplateParameterList *TemplateParams =
1322 if (TemplateParams) {
1324 TemplateParams);
1325 LSI->Lambda->setLambdaIsGeneric(true);
1326 }
1327 LSI->AfterParameterList = true;
1328}
1329
1331 Declarator &ParamInfo,
1332 const DeclSpec &DS) {
1333
1336
1338 bool ExplicitResultType;
1339
1340 SourceLocation TypeLoc, CallOperatorLoc;
1341 if (ParamInfo.getNumTypeObjects() == 0) {
1342 CallOperatorLoc = TypeLoc = Intro.Range.getEnd();
1343 } else {
1344 unsigned Index;
1345 ParamInfo.isFunctionDeclarator(Index);
1346 const auto &Object = ParamInfo.getTypeObject(Index);
1347 TypeLoc =
1348 Object.Loc.isValid() ? Object.Loc : ParamInfo.getSourceRange().getEnd();
1349 CallOperatorLoc = ParamInfo.getSourceRange().getEnd();
1350 }
1351
1352 CXXRecordDecl *Class = LSI->Lambda;
1353 CXXMethodDecl *Method = LSI->CallOperator;
1354
1355 TypeSourceInfo *MethodTyInfo = getLambdaType(
1356 *this, Intro, ParamInfo, getCurScope(), TypeLoc, ExplicitResultType);
1357
1358 LSI->ExplicitParams = ParamInfo.getNumTypeObjects() != 0;
1359
1360 if (ParamInfo.isFunctionDeclarator() != 0 &&
1362 const auto &FTI = ParamInfo.getFunctionTypeInfo();
1363 Params.reserve(Params.size());
1364 for (unsigned I = 0; I < FTI.NumParams; ++I) {
1365 auto *Param = cast<ParmVarDecl>(FTI.Params[I].Param);
1366 Param->setScopeInfo(0, Params.size());
1367 Params.push_back(Param);
1368 }
1369 }
1370
1371 bool IsLambdaStatic =
1373
1375 Method, Intro.Range.getBegin(), CallOperatorLoc,
1376 ParamInfo.getTrailingRequiresClause(), MethodTyInfo,
1377 ParamInfo.getDeclSpec().getConstexprSpecifier(),
1378 IsLambdaStatic ? SC_Static : SC_None, Params, ExplicitResultType);
1379
1381
1382 // This represents the function body for the lambda function, check if we
1383 // have to apply optnone due to a pragma.
1384 AddRangeBasedOptnone(Method);
1385
1386 // code_seg attribute on lambda apply to the method.
1388 Method, /*IsDefinition=*/true))
1389 Method->addAttr(A);
1390
1391 // Attributes on the lambda apply to the method.
1392 ProcessDeclAttributes(CurScope, Method, ParamInfo);
1393
1394 // CUDA lambdas get implicit host and device attributes.
1395 if (getLangOpts().CUDA)
1396 CUDASetLambdaAttrs(Method);
1397
1398 // OpenMP lambdas might get assumumption attributes.
1399 if (LangOpts.OpenMP)
1401
1403
1404 for (auto &&C : LSI->Captures) {
1405 if (!C.isVariableCapture())
1406 continue;
1407 ValueDecl *Var = C.getVariable();
1408 if (Var && Var->isInitCapture()) {
1409 PushOnScopeChains(Var, CurScope, false);
1410 }
1411 }
1412
1413 auto CheckRedefinition = [&](ParmVarDecl *Param) {
1414 for (const auto &Capture : Intro.Captures) {
1415 if (Capture.Id == Param->getIdentifier()) {
1416 Diag(Param->getLocation(), diag::err_parameter_shadow_capture);
1417 Diag(Capture.Loc, diag::note_var_explicitly_captured_here)
1418 << Capture.Id << true;
1419 return false;
1420 }
1421 }
1422 return true;
1423 };
1424
1425 for (ParmVarDecl *P : Params) {
1426 if (!P->getIdentifier())
1427 continue;
1428 if (CheckRedefinition(P))
1429 CheckShadow(CurScope, P);
1430 PushOnScopeChains(P, CurScope);
1431 }
1432
1433 // C++23 [expr.prim.lambda.capture]p5:
1434 // If an identifier in a capture appears as the declarator-id of a parameter
1435 // of the lambda-declarator's parameter-declaration-clause or as the name of a
1436 // template parameter of the lambda-expression's template-parameter-list, the
1437 // program is ill-formed.
1438 TemplateParameterList *TemplateParams =
1440 if (TemplateParams) {
1441 for (const auto *TP : TemplateParams->asArray()) {
1442 if (!TP->getIdentifier())
1443 continue;
1444 for (const auto &Capture : Intro.Captures) {
1445 if (Capture.Id == TP->getIdentifier()) {
1446 Diag(Capture.Loc, diag::err_template_param_shadow) << Capture.Id;
1448 }
1449 }
1450 }
1451 }
1452
1453 // C++20: dcl.decl.general p4:
1454 // The optional requires-clause ([temp.pre]) in an init-declarator or
1455 // member-declarator shall be present only if the declarator declares a
1456 // templated function ([dcl.fct]).
1457 if (Expr *TRC = Method->getTrailingRequiresClause()) {
1458 // [temp.pre]/8:
1459 // An entity is templated if it is
1460 // - a template,
1461 // - an entity defined ([basic.def]) or created ([class.temporary]) in a
1462 // templated entity,
1463 // - a member of a templated entity,
1464 // - an enumerator for an enumeration that is a templated entity, or
1465 // - the closure type of a lambda-expression ([expr.prim.lambda.closure])
1466 // appearing in the declaration of a templated entity. [Note 6: A local
1467 // class, a local or block variable, or a friend function defined in a
1468 // templated entity is a templated entity. — end note]
1469 //
1470 // A templated function is a function template or a function that is
1471 // templated. A templated class is a class template or a class that is
1472 // templated. A templated variable is a variable template or a variable
1473 // that is templated.
1474
1475 // Note: we only have to check if this is defined in a template entity, OR
1476 // if we are a template, since the rest don't apply. The requires clause
1477 // applies to the call operator, which we already know is a member function,
1478 // AND defined.
1479 if (!Method->getDescribedFunctionTemplate() && !Method->isTemplated()) {
1480 Diag(TRC->getBeginLoc(), diag::err_constrained_non_templated_function);
1481 }
1482 }
1483
1484 // Enter a new evaluation context to insulate the lambda from any
1485 // cleanups from the enclosing full-expression.
1490 ExprEvalContexts.back().InImmediateFunctionContext =
1491 LSI->CallOperator->isConsteval();
1492 ExprEvalContexts.back().InImmediateEscalatingFunctionContext =
1493 getLangOpts().CPlusPlus20 && LSI->CallOperator->isImmediateEscalating();
1494}
1495
1497 bool IsInstantiation) {
1498 LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(FunctionScopes.back());
1499
1500 // Leave the expression-evaluation context.
1503
1504 // Leave the context of the lambda.
1505 if (!IsInstantiation)
1507
1508 // Finalize the lambda.
1509 CXXRecordDecl *Class = LSI->Lambda;
1510 Class->setInvalidDecl();
1511 SmallVector<Decl*, 4> Fields(Class->fields());
1512 ActOnFields(nullptr, Class->getLocation(), Class, Fields, SourceLocation(),
1514 CheckCompletedCXXClass(nullptr, Class);
1515
1517}
1518
1519template <typename Func>
1521 Sema &S, const FunctionProtoType &CallOpProto, Func F) {
1523 CallOpProto.isVariadic(), /*IsCXXMethod=*/false);
1525 CallOpProto.isVariadic(), /*IsCXXMethod=*/true);
1526 CallingConv CallOpCC = CallOpProto.getCallConv();
1527
1528 /// Implement emitting a version of the operator for many of the calling
1529 /// conventions for MSVC, as described here:
1530 /// https://devblogs.microsoft.com/oldnewthing/20150220-00/?p=44623.
1531 /// Experimentally, we determined that cdecl, stdcall, fastcall, and
1532 /// vectorcall are generated by MSVC when it is supported by the target.
1533 /// Additionally, we are ensuring that the default-free/default-member and
1534 /// call-operator calling convention are generated as well.
1535 /// NOTE: We intentionally generate a 'thiscall' on Win32 implicitly from the
1536 /// 'member default', despite MSVC not doing so. We do this in order to ensure
1537 /// that someone who intentionally places 'thiscall' on the lambda call
1538 /// operator will still get that overload, since we don't have the a way of
1539 /// detecting the attribute by the time we get here.
1540 if (S.getLangOpts().MSVCCompat) {
1541 CallingConv Convs[] = {
1543 DefaultFree, DefaultMember, CallOpCC};
1544 llvm::sort(Convs);
1545 llvm::iterator_range<CallingConv *> Range(
1546 std::begin(Convs), std::unique(std::begin(Convs), std::end(Convs)));
1547 const TargetInfo &TI = S.getASTContext().getTargetInfo();
1548
1549 for (CallingConv C : Range) {
1551 F(C);
1552 }
1553 return;
1554 }
1555
1556 if (CallOpCC == DefaultMember && DefaultMember != DefaultFree) {
1557 F(DefaultFree);
1558 F(DefaultMember);
1559 } else {
1560 F(CallOpCC);
1561 }
1562}
1563
1564// Returns the 'standard' calling convention to be used for the lambda
1565// conversion function, that is, the 'free' function calling convention unless
1566// it is overridden by a non-default calling convention attribute.
1567static CallingConv
1569 const FunctionProtoType *CallOpProto) {
1571 CallOpProto->isVariadic(), /*IsCXXMethod=*/false);
1573 CallOpProto->isVariadic(), /*IsCXXMethod=*/true);
1574 CallingConv CallOpCC = CallOpProto->getCallConv();
1575
1576 // If the call-operator hasn't been changed, return both the 'free' and
1577 // 'member' function calling convention.
1578 if (CallOpCC == DefaultMember && DefaultMember != DefaultFree)
1579 return DefaultFree;
1580 return CallOpCC;
1581}
1582
1584 const FunctionProtoType *CallOpProto, CallingConv CC) {
1585 const FunctionProtoType::ExtProtoInfo CallOpExtInfo =
1586 CallOpProto->getExtProtoInfo();
1587 FunctionProtoType::ExtProtoInfo InvokerExtInfo = CallOpExtInfo;
1588 InvokerExtInfo.ExtInfo = InvokerExtInfo.ExtInfo.withCallingConv(CC);
1589 InvokerExtInfo.TypeQuals = Qualifiers();
1590 assert(InvokerExtInfo.RefQualifier == RQ_None &&
1591 "Lambda's call operator should not have a reference qualifier");
1592 return Context.getFunctionType(CallOpProto->getReturnType(),
1593 CallOpProto->getParamTypes(), InvokerExtInfo);
1594}
1595
1596/// Add a lambda's conversion to function pointer, as described in
1597/// C++11 [expr.prim.lambda]p6.
1598static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange,
1600 CXXMethodDecl *CallOperator,
1601 QualType InvokerFunctionTy) {
1602 // This conversion is explicitly disabled if the lambda's function has
1603 // pass_object_size attributes on any of its parameters.
1604 auto HasPassObjectSizeAttr = [](const ParmVarDecl *P) {
1605 return P->hasAttr<PassObjectSizeAttr>();
1606 };
1607 if (llvm::any_of(CallOperator->parameters(), HasPassObjectSizeAttr))
1608 return;
1609
1610 // Add the conversion to function pointer.
1611 QualType PtrToFunctionTy = S.Context.getPointerType(InvokerFunctionTy);
1612
1613 // Create the type of the conversion function.
1616 /*IsVariadic=*/false, /*IsCXXMethod=*/true));
1617 // The conversion function is always const and noexcept.
1618 ConvExtInfo.TypeQuals = Qualifiers();
1619 ConvExtInfo.TypeQuals.addConst();
1620 ConvExtInfo.ExceptionSpec.Type = EST_BasicNoexcept;
1621 QualType ConvTy =
1622 S.Context.getFunctionType(PtrToFunctionTy, std::nullopt, ConvExtInfo);
1623
1624 SourceLocation Loc = IntroducerRange.getBegin();
1625 DeclarationName ConversionName
1627 S.Context.getCanonicalType(PtrToFunctionTy));
1628 // Construct a TypeSourceInfo for the conversion function, and wire
1629 // all the parameters appropriately for the FunctionProtoTypeLoc
1630 // so that everything works during transformation/instantiation of
1631 // generic lambdas.
1632 // The main reason for wiring up the parameters of the conversion
1633 // function with that of the call operator is so that constructs
1634 // like the following work:
1635 // auto L = [](auto b) { <-- 1
1636 // return [](auto a) -> decltype(a) { <-- 2
1637 // return a;
1638 // };
1639 // };
1640 // int (*fp)(int) = L(5);
1641 // Because the trailing return type can contain DeclRefExprs that refer
1642 // to the original call operator's variables, we hijack the call
1643 // operators ParmVarDecls below.
1644 TypeSourceInfo *ConvNamePtrToFunctionTSI =
1645 S.Context.getTrivialTypeSourceInfo(PtrToFunctionTy, Loc);
1646 DeclarationNameLoc ConvNameLoc =
1647 DeclarationNameLoc::makeNamedTypeLoc(ConvNamePtrToFunctionTSI);
1648
1649 // The conversion function is a conversion to a pointer-to-function.
1650 TypeSourceInfo *ConvTSI = S.Context.getTrivialTypeSourceInfo(ConvTy, Loc);
1651 FunctionProtoTypeLoc ConvTL =
1653 // Get the result of the conversion function which is a pointer-to-function.
1654 PointerTypeLoc PtrToFunctionTL =
1655 ConvTL.getReturnLoc().getAs<PointerTypeLoc>();
1656 // Do the same for the TypeSourceInfo that is used to name the conversion
1657 // operator.
1658 PointerTypeLoc ConvNamePtrToFunctionTL =
1659 ConvNamePtrToFunctionTSI->getTypeLoc().getAs<PointerTypeLoc>();
1660
1661 // Get the underlying function types that the conversion function will
1662 // be converting to (should match the type of the call operator).
1663 FunctionProtoTypeLoc CallOpConvTL =
1664 PtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>();
1665 FunctionProtoTypeLoc CallOpConvNameTL =
1666 ConvNamePtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>();
1667
1668 // Wire up the FunctionProtoTypeLocs with the call operator's parameters.
1669 // These parameter's are essentially used to transform the name and
1670 // the type of the conversion operator. By using the same parameters
1671 // as the call operator's we don't have to fix any back references that
1672 // the trailing return type of the call operator's uses (such as
1673 // decltype(some_type<decltype(a)>::type{} + decltype(a){}) etc.)
1674 // - we can simply use the return type of the call operator, and
1675 // everything should work.
1676 SmallVector<ParmVarDecl *, 4> InvokerParams;
1677 for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) {
1678 ParmVarDecl *From = CallOperator->getParamDecl(I);
1679
1680 InvokerParams.push_back(ParmVarDecl::Create(
1681 S.Context,
1682 // Temporarily add to the TU. This is set to the invoker below.
1684 From->getLocation(), From->getIdentifier(), From->getType(),
1685 From->getTypeSourceInfo(), From->getStorageClass(),
1686 /*DefArg=*/nullptr));
1687 CallOpConvTL.setParam(I, From);
1688 CallOpConvNameTL.setParam(I, From);
1689 }
1690
1692 S.Context, Class, Loc,
1693 DeclarationNameInfo(ConversionName, Loc, ConvNameLoc), ConvTy, ConvTSI,
1695 /*isInline=*/true, ExplicitSpecifier(),
1698 CallOperator->getBody()->getEndLoc());
1699 Conversion->setAccess(AS_public);
1700 Conversion->setImplicit(true);
1701
1702 // A non-generic lambda may still be a templated entity. We need to preserve
1703 // constraints when converting the lambda to a function pointer. See GH63181.
1704 if (Expr *Requires = CallOperator->getTrailingRequiresClause())
1705 Conversion->setTrailingRequiresClause(Requires);
1706
1707 if (Class->isGenericLambda()) {
1708 // Create a template version of the conversion operator, using the template
1709 // parameter list of the function call operator.
1710 FunctionTemplateDecl *TemplateCallOperator =
1711 CallOperator->getDescribedFunctionTemplate();
1712 FunctionTemplateDecl *ConversionTemplate =
1714 Loc, ConversionName,
1715 TemplateCallOperator->getTemplateParameters(),
1716 Conversion);
1717 ConversionTemplate->setAccess(AS_public);
1718 ConversionTemplate->setImplicit(true);
1719 Conversion->setDescribedFunctionTemplate(ConversionTemplate);
1720 Class->addDecl(ConversionTemplate);
1721 } else
1722 Class->addDecl(Conversion);
1723
1724 // If the lambda is not static, we need to add a static member
1725 // function that will be the result of the conversion with a
1726 // certain unique ID.
1727 // When it is static we just return the static call operator instead.
1728 if (CallOperator->isImplicitObjectMemberFunction()) {
1729 DeclarationName InvokerName =
1731 // FIXME: Instead of passing in the CallOperator->getTypeSourceInfo()
1732 // we should get a prebuilt TrivialTypeSourceInfo from Context
1733 // using FunctionTy & Loc and get its TypeLoc as a FunctionProtoTypeLoc
1734 // then rewire the parameters accordingly, by hoisting up the InvokeParams
1735 // loop below and then use its Params to set Invoke->setParams(...) below.
1736 // This would avoid the 'const' qualifier of the calloperator from
1737 // contaminating the type of the invoker, which is currently adjusted
1738 // in SemaTemplateDeduction.cpp:DeduceTemplateArguments. Fixing the
1739 // trailing return type of the invoker would require a visitor to rebuild
1740 // the trailing return type and adjusting all back DeclRefExpr's to refer
1741 // to the new static invoker parameters - not the call operator's.
1743 S.Context, Class, Loc, DeclarationNameInfo(InvokerName, Loc),
1744 InvokerFunctionTy, CallOperator->getTypeSourceInfo(), SC_Static,
1746 /*isInline=*/true, CallOperator->getConstexprKind(),
1747 CallOperator->getBody()->getEndLoc());
1748 for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I)
1749 InvokerParams[I]->setOwningFunction(Invoke);
1750 Invoke->setParams(InvokerParams);
1751 Invoke->setAccess(AS_private);
1752 Invoke->setImplicit(true);
1753 if (Class->isGenericLambda()) {
1754 FunctionTemplateDecl *TemplateCallOperator =
1755 CallOperator->getDescribedFunctionTemplate();
1756 FunctionTemplateDecl *StaticInvokerTemplate =
1758 S.Context, Class, Loc, InvokerName,
1759 TemplateCallOperator->getTemplateParameters(), Invoke);
1760 StaticInvokerTemplate->setAccess(AS_private);
1761 StaticInvokerTemplate->setImplicit(true);
1762 Invoke->setDescribedFunctionTemplate(StaticInvokerTemplate);
1763 Class->addDecl(StaticInvokerTemplate);
1764 } else
1765 Class->addDecl(Invoke);
1766 }
1767}
1768
1769/// Add a lambda's conversion to function pointers, as described in
1770/// C++11 [expr.prim.lambda]p6. Note that in most cases, this should emit only a
1771/// single pointer conversion. In the event that the default calling convention
1772/// for free and member functions is different, it will emit both conventions.
1773static void addFunctionPointerConversions(Sema &S, SourceRange IntroducerRange,
1775 CXXMethodDecl *CallOperator) {
1776 const FunctionProtoType *CallOpProto =
1777 CallOperator->getType()->castAs<FunctionProtoType>();
1778
1780 S, *CallOpProto, [&](CallingConv CC) {
1781 QualType InvokerFunctionTy =
1782 S.getLambdaConversionFunctionResultType(CallOpProto, CC);
1783 addFunctionPointerConversion(S, IntroducerRange, Class, CallOperator,
1784 InvokerFunctionTy);
1785 });
1786}
1787
1788/// Add a lambda's conversion to block pointer.
1790 SourceRange IntroducerRange,
1792 CXXMethodDecl *CallOperator) {
1793 const FunctionProtoType *CallOpProto =
1794 CallOperator->getType()->castAs<FunctionProtoType>();
1796 CallOpProto, getLambdaConversionFunctionCallConv(S, CallOpProto));
1797 QualType BlockPtrTy = S.Context.getBlockPointerType(FunctionTy);
1798
1799 FunctionProtoType::ExtProtoInfo ConversionEPI(
1801 /*IsVariadic=*/false, /*IsCXXMethod=*/true));
1802 ConversionEPI.TypeQuals = Qualifiers();
1803 ConversionEPI.TypeQuals.addConst();
1804 QualType ConvTy =
1805 S.Context.getFunctionType(BlockPtrTy, std::nullopt, ConversionEPI);
1806
1807 SourceLocation Loc = IntroducerRange.getBegin();
1808 DeclarationName Name
1810 S.Context.getCanonicalType(BlockPtrTy));
1812 S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc));
1814 S.Context, Class, Loc, DeclarationNameInfo(Name, Loc, NameLoc), ConvTy,
1815 S.Context.getTrivialTypeSourceInfo(ConvTy, Loc),
1818 CallOperator->getBody()->getEndLoc());
1819 Conversion->setAccess(AS_public);
1820 Conversion->setImplicit(true);
1821 Class->addDecl(Conversion);
1822}
1823
1825 SourceLocation ImplicitCaptureLoc,
1826 bool IsOpenMPMapping) {
1827 // VLA captures don't have a stored initialization expression.
1828 if (Cap.isVLATypeCapture())
1829 return ExprResult();
1830
1831 // An init-capture is initialized directly from its stored initializer.
1832 if (Cap.isInitCapture())
1833 return cast<VarDecl>(Cap.getVariable())->getInit();
1834
1835 // For anything else, build an initialization expression. For an implicit
1836 // capture, the capture notionally happens at the capture-default, so use
1837 // that location here.
1838 SourceLocation Loc =
1839 ImplicitCaptureLoc.isValid() ? ImplicitCaptureLoc : Cap.getLocation();
1840
1841 // C++11 [expr.prim.lambda]p21:
1842 // When the lambda-expression is evaluated, the entities that
1843 // are captured by copy are used to direct-initialize each
1844 // corresponding non-static data member of the resulting closure
1845 // object. (For array members, the array elements are
1846 // direct-initialized in increasing subscript order.) These
1847 // initializations are performed in the (unspecified) order in
1848 // which the non-static data members are declared.
1849
1850 // C++ [expr.prim.lambda]p12:
1851 // An entity captured by a lambda-expression is odr-used (3.2) in
1852 // the scope containing the lambda-expression.
1854 IdentifierInfo *Name = nullptr;
1855 if (Cap.isThisCapture()) {
1856 QualType ThisTy = getCurrentThisType();
1857 Expr *This = BuildCXXThisExpr(Loc, ThisTy, ImplicitCaptureLoc.isValid());
1858 if (Cap.isCopyCapture())
1859 Init = CreateBuiltinUnaryOp(Loc, UO_Deref, This);
1860 else
1861 Init = This;
1862 } else {
1863 assert(Cap.isVariableCapture() && "unknown kind of capture");
1864 ValueDecl *Var = Cap.getVariable();
1865 Name = Var->getIdentifier();
1867 CXXScopeSpec(), DeclarationNameInfo(Var->getDeclName(), Loc), Var);
1868 }
1869
1870 // In OpenMP, the capture kind doesn't actually describe how to capture:
1871 // variables are "mapped" onto the device in a process that does not formally
1872 // make a copy, even for a "copy capture".
1873 if (IsOpenMPMapping)
1874 return Init;
1875
1876 if (Init.isInvalid())
1877 return ExprError();
1878
1879 Expr *InitExpr = Init.get();
1881 Name, Cap.getCaptureType(), Loc);
1882 InitializationKind InitKind =
1883 InitializationKind::CreateDirect(Loc, Loc, Loc);
1884 InitializationSequence InitSeq(*this, Entity, InitKind, InitExpr);
1885 return InitSeq.Perform(*this, Entity, InitKind, InitExpr);
1886}
1887
1889 LambdaScopeInfo LSI = *cast<LambdaScopeInfo>(FunctionScopes.back());
1891 return BuildLambdaExpr(StartLoc, Body->getEndLoc(), &LSI);
1892}
1893
1896 switch (ICS) {
1898 return LCD_None;
1900 return LCD_ByCopy;
1903 return LCD_ByRef;
1905 llvm_unreachable("block capture in lambda");
1906 }
1907 llvm_unreachable("Unknown implicit capture style");
1908}
1909
1911 if (From.isInitCapture()) {
1912 Expr *Init = cast<VarDecl>(From.getVariable())->getInit();
1913 if (Init && Init->HasSideEffects(Context))
1914 return true;
1915 }
1916
1917 if (!From.isCopyCapture())
1918 return false;
1919
1920 const QualType T = From.isThisCapture()
1922 : From.getCaptureType();
1923
1924 if (T.isVolatileQualified())
1925 return true;
1926
1927 const Type *BaseT = T->getBaseElementTypeUnsafe();
1928 if (const CXXRecordDecl *RD = BaseT->getAsCXXRecordDecl())
1929 return !RD->isCompleteDefinition() || !RD->hasTrivialCopyConstructor() ||
1930 !RD->hasTrivialDestructor();
1931
1932 return false;
1933}
1934
1936 const Capture &From) {
1937 if (CaptureHasSideEffects(From))
1938 return false;
1939
1940 if (From.isVLATypeCapture())
1941 return false;
1942
1943 // FIXME: maybe we should warn on these if we can find a sensible diagnostic
1944 // message
1945 if (From.isInitCapture() &&
1947 return false;
1948
1949 auto diag = Diag(From.getLocation(), diag::warn_unused_lambda_capture);
1950 if (From.isThisCapture())
1951 diag << "'this'";
1952 else
1953 diag << From.getVariable();
1954 diag << From.isNonODRUsed();
1955 diag << FixItHint::CreateRemoval(CaptureRange);
1956 return true;
1957}
1958
1959/// Create a field within the lambda class or captured statement record for the
1960/// given capture.
1962 const sema::Capture &Capture) {
1964 QualType FieldType = Capture.getCaptureType();
1965
1966 TypeSourceInfo *TSI = nullptr;
1967 if (Capture.isVariableCapture()) {
1968 const auto *Var = dyn_cast_or_null<VarDecl>(Capture.getVariable());
1969 if (Var && Var->isInitCapture())
1970 TSI = Var->getTypeSourceInfo();
1971 }
1972
1973 // FIXME: Should we really be doing this? A null TypeSourceInfo seems more
1974 // appropriate, at least for an implicit capture.
1975 if (!TSI)
1976 TSI = Context.getTrivialTypeSourceInfo(FieldType, Loc);
1977
1978 // Build the non-static data member.
1979 FieldDecl *Field =
1980 FieldDecl::Create(Context, RD, /*StartLoc=*/Loc, /*IdLoc=*/Loc,
1981 /*Id=*/nullptr, FieldType, TSI, /*BW=*/nullptr,
1982 /*Mutable=*/false, ICIS_NoInit);
1983 // If the variable being captured has an invalid type, mark the class as
1984 // invalid as well.
1985 if (!FieldType->isDependentType()) {
1986 if (RequireCompleteSizedType(Loc, FieldType,
1987 diag::err_field_incomplete_or_sizeless)) {
1988 RD->setInvalidDecl();
1989 Field->setInvalidDecl();
1990 } else {
1991 NamedDecl *Def;
1992 FieldType->isIncompleteType(&Def);
1993 if (Def && Def->isInvalidDecl()) {
1994 RD->setInvalidDecl();
1995 Field->setInvalidDecl();
1996 }
1997 }
1998 }
1999 Field->setImplicit(true);
2000 Field->setAccess(AS_private);
2001 RD->addDecl(Field);
2002
2004 Field->setCapturedVLAType(Capture.getCapturedVLAType());
2005
2006 return Field;
2007}
2008
2010 LambdaScopeInfo *LSI) {
2011 // Collect information from the lambda scope.
2013 SmallVector<Expr *, 4> CaptureInits;
2014 SourceLocation CaptureDefaultLoc = LSI->CaptureDefaultLoc;
2015 LambdaCaptureDefault CaptureDefault =
2018 CXXMethodDecl *CallOperator;
2019 SourceRange IntroducerRange;
2020 bool ExplicitParams;
2021 bool ExplicitResultType;
2022 CleanupInfo LambdaCleanup;
2023 bool ContainsUnexpandedParameterPack;
2024 bool IsGenericLambda;
2025 {
2026 CallOperator = LSI->CallOperator;
2027 Class = LSI->Lambda;
2028 IntroducerRange = LSI->IntroducerRange;
2029 ExplicitParams = LSI->ExplicitParams;
2030 ExplicitResultType = !LSI->HasImplicitReturnType;
2031 LambdaCleanup = LSI->Cleanup;
2032 ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack;
2033 IsGenericLambda = Class->isGenericLambda();
2034
2035 CallOperator->setLexicalDeclContext(Class);
2036 Decl *TemplateOrNonTemplateCallOperatorDecl =
2037 CallOperator->getDescribedFunctionTemplate()
2038 ? CallOperator->getDescribedFunctionTemplate()
2039 : cast<Decl>(CallOperator);
2040
2041 // FIXME: Is this really the best choice? Keeping the lexical decl context
2042 // set as CurContext seems more faithful to the source.
2043 TemplateOrNonTemplateCallOperatorDecl->setLexicalDeclContext(Class);
2044
2046
2047 // True if the current capture has a used capture or default before it.
2048 bool CurHasPreviousCapture = CaptureDefault != LCD_None;
2049 SourceLocation PrevCaptureLoc = CurHasPreviousCapture ?
2050 CaptureDefaultLoc : IntroducerRange.getBegin();
2051
2052 for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I) {
2053 const Capture &From = LSI->Captures[I];
2054
2055 if (From.isInvalid())
2056 return ExprError();
2057
2058 assert(!From.isBlockCapture() && "Cannot capture __block variables");
2059 bool IsImplicit = I >= LSI->NumExplicitCaptures;
2060 SourceLocation ImplicitCaptureLoc =
2061 IsImplicit ? CaptureDefaultLoc : SourceLocation();
2062
2063 // Use source ranges of explicit captures for fixits where available.
2064 SourceRange CaptureRange = LSI->ExplicitCaptureRanges[I];
2065
2066 // Warn about unused explicit captures.
2067 bool IsCaptureUsed = true;
2068 if (!CurContext->isDependentContext() && !IsImplicit &&
2069 !From.isODRUsed()) {
2070 // Initialized captures that are non-ODR used may not be eliminated.
2071 // FIXME: Where did the IsGenericLambda here come from?
2072 bool NonODRUsedInitCapture =
2073 IsGenericLambda && From.isNonODRUsed() && From.isInitCapture();
2074 if (!NonODRUsedInitCapture) {
2075 bool IsLast = (I + 1) == LSI->NumExplicitCaptures;
2076 SourceRange FixItRange;
2077 if (CaptureRange.isValid()) {
2078 if (!CurHasPreviousCapture && !IsLast) {
2079 // If there are no captures preceding this capture, remove the
2080 // following comma.
2081 FixItRange = SourceRange(CaptureRange.getBegin(),
2082 getLocForEndOfToken(CaptureRange.getEnd()));
2083 } else {
2084 // Otherwise, remove the comma since the last used capture.
2085 FixItRange = SourceRange(getLocForEndOfToken(PrevCaptureLoc),
2086 CaptureRange.getEnd());
2087 }
2088 }
2089
2090 IsCaptureUsed = !DiagnoseUnusedLambdaCapture(FixItRange, From);
2091 }
2092 }
2093
2094 if (CaptureRange.isValid()) {
2095 CurHasPreviousCapture |= IsCaptureUsed;
2096 PrevCaptureLoc = CaptureRange.getEnd();
2097 }
2098
2099 // Map the capture to our AST representation.
2100 LambdaCapture Capture = [&] {
2101 if (From.isThisCapture()) {
2102 // Capturing 'this' implicitly with a default of '[=]' is deprecated,
2103 // because it results in a reference capture. Don't warn prior to
2104 // C++2a; there's nothing that can be done about it before then.
2105 if (getLangOpts().CPlusPlus20 && IsImplicit &&
2106 CaptureDefault == LCD_ByCopy) {
2107 Diag(From.getLocation(), diag::warn_deprecated_this_capture);
2108 Diag(CaptureDefaultLoc, diag::note_deprecated_this_capture)
2110 getLocForEndOfToken(CaptureDefaultLoc), ", this");
2111 }
2112 return LambdaCapture(From.getLocation(), IsImplicit,
2114 } else if (From.isVLATypeCapture()) {
2115 return LambdaCapture(From.getLocation(), IsImplicit, LCK_VLAType);
2116 } else {
2117 assert(From.isVariableCapture() && "unknown kind of capture");
2118 ValueDecl *Var = From.getVariable();
2119 LambdaCaptureKind Kind =
2121 return LambdaCapture(From.getLocation(), IsImplicit, Kind, Var,
2122 From.getEllipsisLoc());
2123 }
2124 }();
2125
2126 // Form the initializer for the capture field.
2127 ExprResult Init = BuildCaptureInit(From, ImplicitCaptureLoc);
2128
2129 // FIXME: Skip this capture if the capture is not used, the initializer
2130 // has no side-effects, the type of the capture is trivial, and the
2131 // lambda is not externally visible.
2132
2133 // Add a FieldDecl for the capture and form its initializer.
2134 BuildCaptureField(Class, From);
2135 Captures.push_back(Capture);
2136 CaptureInits.push_back(Init.get());
2137
2138 if (LangOpts.CUDA)
2139 CUDACheckLambdaCapture(CallOperator, From);
2140 }
2141
2142 Class->setCaptures(Context, Captures);
2143
2144 // C++11 [expr.prim.lambda]p6:
2145 // The closure type for a lambda-expression with no lambda-capture
2146 // has a public non-virtual non-explicit const conversion function
2147 // to pointer to function having the same parameter and return
2148 // types as the closure type's function call operator.
2149 if (Captures.empty() && CaptureDefault == LCD_None)
2150 addFunctionPointerConversions(*this, IntroducerRange, Class,
2151 CallOperator);
2152
2153 // Objective-C++:
2154 // The closure type for a lambda-expression has a public non-virtual
2155 // non-explicit const conversion function to a block pointer having the
2156 // same parameter and return types as the closure type's function call
2157 // operator.
2158 // FIXME: Fix generic lambda to block conversions.
2159 if (getLangOpts().Blocks && getLangOpts().ObjC && !IsGenericLambda)
2160 addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator);
2161
2162 // Finalize the lambda class.
2163 SmallVector<Decl*, 4> Fields(Class->fields());
2164 ActOnFields(nullptr, Class->getLocation(), Class, Fields, SourceLocation(),
2166 CheckCompletedCXXClass(nullptr, Class);
2167 }
2168
2169 Cleanup.mergeFrom(LambdaCleanup);
2170
2171 LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange,
2172 CaptureDefault, CaptureDefaultLoc,
2173 ExplicitParams, ExplicitResultType,
2174 CaptureInits, EndLoc,
2175 ContainsUnexpandedParameterPack);
2176 // If the lambda expression's call operator is not explicitly marked constexpr
2177 // and we are not in a dependent context, analyze the call operator to infer
2178 // its constexpr-ness, suppressing diagnostics while doing so.
2179 if (getLangOpts().CPlusPlus17 && !CallOperator->isInvalidDecl() &&
2180 !CallOperator->isConstexpr() &&
2181 !isa<CoroutineBodyStmt>(CallOperator->getBody()) &&
2182 !Class->getDeclContext()->isDependentContext()) {
2183 CallOperator->setConstexprKind(
2188 }
2189
2190 // Emit delayed shadowing warnings now that the full capture list is known.
2192
2194 switch (ExprEvalContexts.back().Context) {
2195 // C++11 [expr.prim.lambda]p2:
2196 // A lambda-expression shall not appear in an unevaluated operand
2197 // (Clause 5).
2201 // C++1y [expr.const]p2:
2202 // A conditional-expression e is a core constant expression unless the
2203 // evaluation of e, following the rules of the abstract machine, would
2204 // evaluate [...] a lambda-expression.
2205 //
2206 // This is technically incorrect, there are some constant evaluated contexts
2207 // where this should be allowed. We should probably fix this when DR1607 is
2208 // ratified, it lays out the exact set of conditions where we shouldn't
2209 // allow a lambda-expression.
2212 // We don't actually diagnose this case immediately, because we
2213 // could be within a context where we might find out later that
2214 // the expression is potentially evaluated (e.g., for typeid).
2215 ExprEvalContexts.back().Lambdas.push_back(Lambda);
2216 break;
2217
2221 break;
2222 }
2223 }
2224
2225 return MaybeBindToTemporary(Lambda);
2226}
2227
2229 SourceLocation ConvLocation,
2230 CXXConversionDecl *Conv,
2231 Expr *Src) {
2232 // Make sure that the lambda call operator is marked used.
2233 CXXRecordDecl *Lambda = Conv->getParent();
2234 CXXMethodDecl *CallOperator
2235 = cast<CXXMethodDecl>(
2236 Lambda->lookup(
2238 CallOperator->setReferenced();
2239 CallOperator->markUsed(Context);
2240
2243 CurrentLocation, Src);
2244 if (!Init.isInvalid())
2245 Init = ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
2246
2247 if (Init.isInvalid())
2248 return ExprError();
2249
2250 // Create the new block to be returned.
2252
2253 // Set the type information.
2254 Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo());
2255 Block->setIsVariadic(CallOperator->isVariadic());
2256 Block->setBlockMissingReturnType(false);
2257
2258 // Add parameters.
2260 for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I) {
2261 ParmVarDecl *From = CallOperator->getParamDecl(I);
2262 BlockParams.push_back(ParmVarDecl::Create(
2263 Context, Block, From->getBeginLoc(), From->getLocation(),
2264 From->getIdentifier(), From->getType(), From->getTypeSourceInfo(),
2265 From->getStorageClass(),
2266 /*DefArg=*/nullptr));
2267 }
2268 Block->setParams(BlockParams);
2269
2270 Block->setIsConversionFromLambda(true);
2271
2272 // Add capture. The capture uses a fake variable, which doesn't correspond
2273 // to any actual memory location. However, the initializer copy-initializes
2274 // the lambda object.
2275 TypeSourceInfo *CapVarTSI =
2277 VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation,
2278 ConvLocation, nullptr,
2279 Src->getType(), CapVarTSI,
2280 SC_None);
2281 BlockDecl::Capture Capture(/*variable=*/CapVar, /*byRef=*/false,
2282 /*nested=*/false, /*copy=*/Init.get());
2283 Block->setCaptures(Context, Capture, /*CapturesCXXThis=*/false);
2284
2285 // Add a fake function body to the block. IR generation is responsible
2286 // for filling in the actual body, which cannot be expressed as an AST.
2287 Block->setBody(new (Context) CompoundStmt(ConvLocation));
2288
2289 // Create the block literal expression.
2290 Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType());
2291 ExprCleanupObjects.push_back(Block);
2293
2294 return BuildBlock;
2295}
2296
2301 return FD;
2302 }
2303
2305 return FD->getInstantiatedFromDecl();
2306
2308 if (!FTD)
2309 return nullptr;
2310
2313
2314 return FTD->getTemplatedDecl();
2315}
2316
2320 LocalInstantiationScope &Scope, bool ShouldAddDeclsFromParentScope)
2321 : FunctionScopeRAII(SemaRef) {
2322 if (!isLambdaCallOperator(FD)) {
2324 return;
2325 }
2326
2327 SemaRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));
2328
2329 FunctionDecl *Pattern = getPatternFunctionDecl(FD);
2330 if (Pattern) {
2331 SemaRef.addInstantiatedCapturesToScope(FD, Pattern, Scope, MLTAL);
2332
2333 FunctionDecl *ParentFD = FD;
2334 while (ShouldAddDeclsFromParentScope) {
2335
2336 ParentFD =
2337 dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(ParentFD));
2338 Pattern =
2339 dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(Pattern));
2340
2341 if (!FD || !Pattern)
2342 break;
2343
2344 SemaRef.addInstantiatedParametersToScope(ParentFD, Pattern, Scope, MLTAL);
2345 SemaRef.addInstantiatedLocalVarsToScope(ParentFD, Pattern, Scope);
2346 }
2347 }
2348}
#define V(N, I)
Definition: ASTContext.h:3259
int Id
Definition: ASTDiff.cpp:190
This file provides some common utility functions for processing Lambda related AST Constructs.
StringRef P
Defines the clang::Expr interface and subclasses for C++ expressions.
static LambdaCaptureDefault mapImplicitCaptureStyle(CapturingScopeInfo::ImplicitCaptureStyle ICS)
static CallingConv getLambdaConversionFunctionCallConv(Sema &S, const FunctionProtoType *CallOpProto)
static EnumDecl * findEnumForBlockReturn(Expr *E)
If this expression is an enumerator-like expression of some type T, return the type T; otherwise,...
Definition: SemaLambda.cpp:528
static EnumDecl * findCommonEnumForBlockReturns(ArrayRef< ReturnStmt * > returns)
Attempt to find a common type T for which all of the returned expressions in a block are enumerator-l...
Definition: SemaLambda.cpp:600
static TypeSourceInfo * getLambdaType(Sema &S, LambdaIntroducer &Intro, Declarator &ParamInfo, Scope *CurScope, SourceLocation Loc, bool &ExplicitResultType)
Definition: SemaLambda.cpp:874
static std::optional< unsigned > getStackIndexOfNearestEnclosingCaptureReadyLambda(ArrayRef< const clang::sema::FunctionScopeInfo * > FunctionScopes, ValueDecl *VarToCapture)
Examines the FunctionScopeInfo stack to determine the nearest enclosing lambda (to the current lambda...
Definition: SemaLambda.cpp:65
static FunctionDecl * getPatternFunctionDecl(FunctionDecl *FD)
static LambdaScopeInfo * getCurrentLambdaScopeUnsafe(Sema &S)
Definition: SemaLambda.cpp:842
static void adjustBlockReturnsToEnum(Sema &S, ArrayRef< ReturnStmt * > returns, QualType returnType)
Adjust the given return statements so that they formally return the given type.
Definition: SemaLambda.cpp:621
static TemplateParameterList * getGenericLambdaTemplateParameterList(LambdaScopeInfo *LSI, Sema &SemaRef)
Definition: SemaLambda.cpp:231
static void addBlockPointerConversion(Sema &S, SourceRange IntroducerRange, CXXRecordDecl *Class, CXXMethodDecl *CallOperator)
Add a lambda's conversion to block pointer.
static void buildLambdaScopeReturnType(Sema &S, LambdaScopeInfo *LSI, CXXMethodDecl *CallOperator, bool ExplicitResultType)
Definition: SemaLambda.cpp:466
static TypeSourceInfo * getDummyLambdaType(Sema &S, SourceLocation Loc=SourceLocation())
Definition: SemaLambda.cpp:848
static QualType buildTypeForLambdaCallOperator(Sema &S, clang::CXXRecordDecl *Class, TemplateParameterList *TemplateParams, TypeSourceInfo *MethodTypeInfo)
Definition: SemaLambda.cpp:361
static bool isInInlineFunction(const DeclContext *DC)
Determine whether the given context is or is enclosed in an inline function.
Definition: SemaLambda.cpp:265
static void addFunctionPointerConversions(Sema &S, SourceRange IntroducerRange, CXXRecordDecl *Class, CXXMethodDecl *CallOperator)
Add a lambda's conversion to function pointers, as described in C++11 [expr.prim.lambda]p6.
static void repeatForLambdaConversionFunctionCallingConvs(Sema &S, const FunctionProtoType &CallOpProto, Func F)
static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange, CXXRecordDecl *Class, CXXMethodDecl *CallOperator, QualType InvokerFunctionTy)
Add a lambda's conversion to function pointer, as described in C++11 [expr.prim.lambda]p6.
This file provides some common utility functions for processing Lambdas.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1068
CanQualType getCanonicalFunctionResultType(QualType ResultType) const
Adjust the given function result type.
QualType getBlockPointerType(QualType T) const
Return the uniqued reference to the type for a block of the specified type.
DeclarationNameTable DeclarationNames
Definition: ASTContext.h:643
QualType getRecordType(const RecordDecl *Decl) const
QualType getPackExpansionType(QualType Pattern, std::optional< unsigned > NumExpansions, bool ExpectPackInType=true)
Form a pack expansion type with the given pattern.
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2549
bool hasSameType(QualType T1, QualType T2) const
Determine whether the given types T1 and T2 are equivalent.
Definition: ASTContext.h:2565
CallingConv getDefaultCallingConvention(bool IsVariadic, bool IsCXXMethod, bool IsBuiltin=false) const
Retrieves the default calling convention for the current target.
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
CanQualType DependentTy
Definition: ASTContext.h:1114
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
Definition: ASTContext.h:1575
IdentifierTable & Idents
Definition: ASTContext.h:639
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
MangleNumberingContext & getManglingNumberContext(const DeclContext *DC)
Retrieve the context for computing mangling numbers in the given DeclContext.
CanQualType VoidTy
Definition: ASTContext.h:1086
QualType getFunctionType(QualType ResultTy, ArrayRef< QualType > Args, const FunctionProtoType::ExtProtoInfo &EPI) const
Return a normal function type with a typed argument list.
Definition: ASTContext.h:1553
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:752
QualType getAutoDeductType() const
C++11 deduction pattern for 'auto' type.
ExternalASTSource * getExternalSource() const
Retrieve a pointer to the external AST source associated with this AST context, if any.
Definition: ASTContext.h:1182
PtrTy get() const
Definition: Ownership.h:170
Attr - This represents one attribute.
Definition: Attr.h:42
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3834
A binding in a decomposition declaration.
Definition: DeclCXX.h:4100
A class which contains all the information about a particular captured value.
Definition: Decl.h:4465
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4459
static BlockDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L)
Definition: Decl.cpp:5375
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6167
Represents a C++ conversion function within a class.
Definition: DeclCXX.h:2855
static CXXConversionDecl * Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, bool UsesFPIntrin, bool isInline, ExplicitSpecifier ES, ConstexprSpecKind ConstexprKind, SourceLocation EndLocation, Expr *TrailingRequiresClause=nullptr)
Definition: DeclCXX.cpp:2888
QualType getConversionType() const
Returns the type that this conversion function is converting to.
Definition: DeclCXX.h:2895
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2053
bool isImplicitObjectMemberFunction() const
[C++2b][dcl.fct]/p7 An implicit object member function is a non-static member function without an exp...
Definition: DeclCXX.cpp:2460
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2179
static CXXMethodDecl * Create(ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin, bool isInline, ConstexprSpecKind ConstexprKind, SourceLocation EndLocation, Expr *TrailingRequiresClause=nullptr)
Definition: DeclCXX.cpp:2273
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
void setLambdaTypeInfo(TypeSourceInfo *TS)
Definition: DeclCXX.h:1865
void setLambdaIsGeneric(bool IsGeneric)
Definition: DeclCXX.h:1872
static CXXRecordDecl * CreateLambda(const ASTContext &C, DeclContext *DC, TypeSourceInfo *Info, SourceLocation Loc, unsigned DependencyKind, bool IsGeneric, LambdaCaptureDefault CaptureDefault)
Definition: DeclCXX.cpp:147
bool isCapturelessLambda() const
Definition: DeclCXX.h:1067
Represents a C++ nested-name-specifier or a global scope specifier.
Definition: DeclSpec.h:73
void mergeFrom(CleanupInfo Rhs)
Definition: CleanupInfo.h:38
void setExprNeedsCleanups(bool SideEffects)
Definition: CleanupInfo.h:28
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1604
ConditionalOperator - The ?: ternary operator.
Definition: Expr.h:4173
reference front() const
Definition: DeclBase.h:1402
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1446
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition: DeclBase.h:2076
bool Equals(const DeclContext *DC) const
Determine whether this declaration context is equivalent to the declaration context DC.
Definition: DeclBase.h:2201
bool isFileContext() const
Definition: DeclBase.h:2147
bool isDependentContext() const
Determines whether this context is dependent on a template parameter.
Definition: DeclBase.cpp:1265
DeclContext * getLexicalParent()
getLexicalParent - Returns the containing lexical DeclContext.
Definition: DeclBase.h:2092
lookup_result lookup(DeclarationName Name) const
lookup - Find the declarations (if any) with the given Name in this context.
Definition: DeclBase.cpp:1785
bool isTranslationUnit() const
Definition: DeclBase.h:2152
bool isRecord() const
Definition: DeclBase.h:2156
void addDecl(Decl *D)
Add the declaration D into this context.
Definition: DeclBase.cpp:1699
bool isFunctionOrMethod() const
Definition: DeclBase.h:2128
Simple template class for restricting typo correction candidates to ones having a single Decl* of the...
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
Captures information about "declaration specifiers".
Definition: DeclSpec.h:246
SCS getStorageClassSpec() const
Definition: DeclSpec.h:497
bool SetTypeQual(TQ T, SourceLocation Loc)
Definition: DeclSpec.cpp:1013
ConstexprSpecKind getConstexprSpecifier() const
Definition: DeclSpec.h:828
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:85
void addAttr(Attr *A)
Definition: DeclBase.cpp:975
bool isParameterPack() const
Whether this declaration is a parameter pack.
Definition: DeclBase.cpp:220
void setInvalidDecl(bool Invalid=true)
setInvalidDecl - Indicates the Decl had a semantic error.
Definition: DeclBase.cpp:132
void markUsed(ASTContext &C)
Mark the declaration used, in the sense of odr-use.
Definition: DeclBase.cpp:545
bool isTemplated() const
Determine whether this declaration is a templated entity (whether it is.
Definition: DeclBase.cpp:262
bool isInvalidDecl() const
Definition: DeclBase.h:593
void setAccess(AccessSpecifier AS)
Definition: DeclBase.h:507
SourceLocation getLocation() const
Definition: DeclBase.h:444
void setImplicit(bool I=true)
Definition: DeclBase.h:599
void setReferenced(bool R=true)
Definition: DeclBase.h:628
void setLocation(SourceLocation L)
Definition: DeclBase.h:445
DeclContext * getDeclContext()
Definition: DeclBase.h:453
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition: DeclBase.h:918
void setLexicalDeclContext(DeclContext *DC)
Definition: DeclBase.cpp:340
DeclarationNameLoc - Additional source/type location info for a declaration name.
static DeclarationNameLoc makeNamedTypeLoc(TypeSourceInfo *TInfo)
Construct location information for a constructor, destructor or conversion operator.
static DeclarationNameLoc makeCXXOperatorNameLoc(SourceLocation BeginLoc, SourceLocation EndLoc)
Construct location information for a non-literal C++ operator.
DeclarationName getCXXConversionFunctionName(CanQualType Ty)
Returns the name of a C++ conversion function for the given Type.
DeclarationName getCXXOperatorName(OverloadedOperatorKind Op)
Get the name of the overloadable C++ operator corresponding to Op.
The name of a declaration.
void setInnerLocStart(SourceLocation L)
Definition: Decl.h:814
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Decl.h:822
void setTypeSourceInfo(TypeSourceInfo *TI)
Definition: Decl.h:805
void setTrailingRequiresClause(Expr *TrailingRequiresClause)
Definition: Decl.cpp:2016
Expr * getTrailingRequiresClause()
Get the constraint-expression introduced by the trailing requires-clause in the function/member decla...
Definition: Decl.h:846
TypeSourceInfo * getTypeSourceInfo() const
Definition: Decl.h:799
Information about one declarator, including the parsed type information and the identifier.
Definition: DeclSpec.h:1899
bool isFunctionDeclarator(unsigned &idx) const
isFunctionDeclarator - This method returns true if the declarator is a function declarator (looking t...
Definition: DeclSpec.h:2455
const DeclaratorChunk & getTypeObject(unsigned i) const
Return the specified TypeInfo from this declarator.
Definition: DeclSpec.h:2397
const DeclSpec & getDeclSpec() const
getDeclSpec - Return the declaration-specifier that this declarator was declared with.
Definition: DeclSpec.h:2046
Expr * getTrailingRequiresClause()
Sets a trailing requires clause for this declarator.
Definition: DeclSpec.h:2632
unsigned getNumTypeObjects() const
Return the number of types applied to this declarator.
Definition: DeclSpec.h:2393
bool isExplicitObjectMemberFunction()
Definition: DeclSpec.cpp:424
SourceRange getSourceRange() const LLVM_READONLY
Get the source range that spans this declarator.
Definition: DeclSpec.h:2081
DeclaratorChunk::FunctionTypeInfo & getFunctionTypeInfo()
getFunctionTypeInfo - Retrieves the function type info object (looking through parentheses).
Definition: DeclSpec.h:2486
Common base class for placeholders for types that get replaced by placeholder type deduction: C++11 a...
Definition: Type.h:5490
An instance of this object exists for each enum constant that is defined.
Definition: Decl.h:3265
Represents an enum.
Definition: Decl.h:3832
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
Definition: Type.h:5118
Store information needed for an explicit specifier.
Definition: DeclCXX.h:1892
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:3436
This represents one expression.
Definition: Expr.h:110
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3041
QualType getType() const
Definition: Expr.h:142
Represents difference between two FPOptions values.
Definition: LangOptions.h:870
bool isFPConstrained() const
Definition: LangOptions.h:800
Represents a member of a struct/union/class.
Definition: Decl.h:3025
static FieldDecl * Create(const ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, InClassInitStyle InitStyle)
Definition: Decl.cpp:4512
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition: Diagnostic.h:123
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:97
void setSubExpr(Expr *E)
As with any mutator of the AST, be very careful when modifying an existing AST to preserve its invari...
Definition: Expr.h:1057
const Expr * getSubExpr() const
Definition: Expr.h:1052
Represents a function declaration or definition.
Definition: Decl.h:1959
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2674
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition: Decl.cpp:3201
ConstexprSpecKind getConstexprKind() const
Definition: Decl.h:2413
void setDescribedFunctionTemplate(FunctionTemplateDecl *Template)
Definition: Decl.cpp:4012
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition: Decl.cpp:4007
QualType getReturnType() const
Definition: Decl.h:2722
ArrayRef< ParmVarDecl * > parameters() const
Definition: Decl.h:2651
FunctionTemplateDecl * getPrimaryTemplate() const
Retrieve the primary template that this function template specialization either specializes or was in...
Definition: Decl.cpp:4127
bool isVariadic() const
Whether this function is variadic.
Definition: Decl.cpp:3089
@ TK_MemberSpecialization
Definition: Decl.h:1971
@ TK_DependentNonTemplate
Definition: Decl.h:1980
TemplatedKind getTemplatedKind() const
What kind of templated function this is.
Definition: Decl.cpp:3958
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
Definition: Decl.h:2407
bool isImmediateEscalating() const
Definition: Decl.cpp:3233
FunctionDecl * getInstantiatedFromDecl() const
Definition: Decl.cpp:4031
void setConstexprKind(ConstexprSpecKind CSK)
Definition: Decl.h:2410
bool isConsteval() const
Definition: Decl.h:2419
void setStorageClass(StorageClass SClass)
Sets the storage class as written in the source.
Definition: Decl.h:2770
FunctionDecl * getInstantiatedFromMemberFunction() const
If this function is an instantiation of a member function of a class template specialization,...
Definition: Decl.cpp:3979
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3657
void setParams(ArrayRef< ParmVarDecl * > NewParamInfo)
Definition: Decl.h:2682
Represents a prototype with parameter type info, e.g.
Definition: Type.h:4199
bool isVariadic() const
Whether this function prototype is variadic.
Definition: Type.h:4555
ExtProtoInfo getExtProtoInfo() const
Definition: Type.h:4443
ArrayRef< QualType > getParamTypes() const
Definition: Type.h:4439
Declaration of a template function.
Definition: DeclTemplate.h:958
FunctionDecl * getTemplatedDecl() const
Get the underlying function declaration of the template.
FunctionTemplateDecl * getInstantiatedFromMemberTemplate() const
static FunctionTemplateDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name, TemplateParameterList *Params, NamedDecl *Decl)
Create a function template node.
TypeLoc getReturnLoc() const
Definition: TypeLoc.h:1483
ExtInfo withCallingConv(CallingConv cc) const
Definition: Type.h:4025
CallingConv getCallConv() const
Definition: Type.h:4127
QualType getReturnType() const
Definition: Type.h:4116
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition: Expr.h:3649
static ImplicitCastExpr * Create(const ASTContext &Context, QualType T, CastKind Kind, Expr *Operand, const CXXCastPath *BasePath, ExprValueKind Cat, FPOptionsOverride FPO)
Definition: Expr.cpp:2060
Describes the kind of initialization being performed, along with location information for tokens rela...
static InitializationKind CreateDirect(SourceLocation InitLoc, SourceLocation LParenLoc, SourceLocation RParenLoc)
Create a direct initialization.
static InitializationKind CreateCopy(SourceLocation InitLoc, SourceLocation EqualLoc, bool AllowExplicitConvs=false)
Create a copy initialization.
static InitializationKind CreateDirectList(SourceLocation InitLoc)
Describes the sequence of initializations required to initialize a given object or reference with a s...
ExprResult Perform(Sema &S, const InitializedEntity &Entity, const InitializationKind &Kind, MultiExprArg Args, QualType *ResultType=nullptr)
Perform the actual initialization of the given entity based on the computed initialization sequence.
Definition: SemaInit.cpp:8578
Describes an entity that is being initialized.
static InitializedEntity InitializeLambdaToBlock(SourceLocation BlockVarLoc, QualType Type)
static InitializedEntity InitializeLambdaCapture(IdentifierInfo *VarID, QualType FieldType, SourceLocation Loc)
Create the initialization entity for a lambda capture.
Describes the capture of a variable or of this, or of a C++1y init-capture.
Definition: LambdaCapture.h:25
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1938
static LambdaExpr * Create(const ASTContext &C, CXXRecordDecl *Class, SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc, bool ExplicitParams, bool ExplicitResultType, ArrayRef< Expr * > CaptureInits, SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack)
Construct a new lambda expression.
Definition: ExprCXX.cpp:1244
A stack-allocated class that identifies which local variable declaration instantiations are present i...
Definition: Template.h:365
Represents the results of name lookup.
Definition: Lookup.h:46
DeclClass * getAsSingle() const
Definition: Lookup.h:553
bool empty() const
Return true if no decls were found.
Definition: Lookup.h:359
bool isAmbiguous() const
Definition: Lookup.h:321
Keeps track of the mangled names of lambda expressions and block literals within a particular context...
virtual unsigned getManglingNumber(const CXXMethodDecl *CallOperator)=0
Retrieve the mangling number of a new lambda expression with the given call operator within this cont...
virtual unsigned getDeviceManglingNumber(const CXXMethodDecl *)
Retrieve the mangling number of a new lambda expression with the given call operator within the devic...
Data structure that captures multiple levels of template argument lists for use in template instantia...
Definition: Template.h:76
This represents a decl that may have a name.
Definition: Decl.h:249
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:270
bool isPlaceholderVar(const LangOptions &LangOpts) const
Definition: Decl.cpp:1082
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:315
PtrTy get() const
Definition: Ownership.h:80
Represents a pack expansion of types.
Definition: Type.h:6112
Expr ** getExprs()
Definition: Expr.h:5658
unsigned getNumExprs() const
Return the number of expressions in this paren list.
Definition: Expr.h:5647
Represents a parameter to a function.
Definition: Decl.h:1749
static ParmVarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg)
Definition: Decl.cpp:2915
Wrapper for source info for pointers.
Definition: TypeLoc.h:1275
A (possibly-)qualified type.
Definition: Type.h:737
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition: Type.h:6985
QualType getDesugaredType(const ASTContext &Context) const
Return the specified type with any "sugar" removed from the type.
Definition: Type.h:1088
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:804
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:7027
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:7102
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6995
The collection of all-type qualifiers we support.
Definition: Type.h:147
void addAddressSpace(LangAS space)
Definition: Type.h:404
void addConst()
Definition: Type.h:267
Represents a struct/union/class.
Definition: Decl.h:4133
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition: Stmt.h:3017
void setRetValue(Expr *E)
Definition: Stmt.h:3050
SourceLocation getBeginLoc() const
Definition: Stmt.h:3074
Expr * getRetValue()
Definition: Stmt.h:3048
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
const Scope * getParent() const
getParent - Return the scope that this is nested in.
Definition: Scope.h:264
Scope * getTemplateParamParent()
Definition: Scope.h:309
A RAII object to temporarily push a declaration context.
Definition: Sema.h:2671
LambdaScopeForCallOperatorInstantiationRAII(Sema &SemasRef, FunctionDecl *FD, MultiLevelTemplateArgumentList MLTAL, LocalInstantiationScope &Scope, bool ShouldAddDeclsFromParentScope=true)
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:426
Attr * getImplicitCodeSegOrSectionAttrForFunction(const FunctionDecl *FD, bool IsDefinition)
Returns an implicit CodeSegAttr if a __declspec(code_seg) is found on a containing class.
Definition: SemaDecl.cpp:11095
QualType getCurrentThisType()
Try to retrieve the type of the 'this' pointer.
ExprResult BuildBlockForLambdaConversion(SourceLocation CurrentLocation, SourceLocation ConvLocation, CXXConversionDecl *Conv, Expr *Src)
Scope * getCurScope() const
Retrieve the parser's current scope.
Definition: Sema.h:869
bool DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, CorrectionCandidateCallback &CCC, TemplateArgumentListInfo *ExplicitTemplateArgs=nullptr, ArrayRef< Expr * > Args=std::nullopt, DeclContext *LookupCtx=nullptr, TypoExpr **Out=nullptr)
Diagnose an empty lookup.
Definition: SemaExpr.cpp:2469
ExprResult CreateBuiltinUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc, Expr *InputExpr, bool IsAfterAmp=false)
Definition: SemaExpr.cpp:16292
bool RequireCompleteSizedType(SourceLocation Loc, QualType T, unsigned DiagID, const Ts &...Args)
Definition: Sema.h:6596
@ LookupOrdinaryName
Ordinary name lookup, which finds ordinary names (functions, variables, typedefs, etc....
Definition: Sema.h:7607
QualType deduceVarTypeFromInitializer(VarDecl *VDecl, DeclarationName Name, QualType Type, TypeSourceInfo *TSI, SourceRange Range, bool DirectInit, Expr *Init)
Definition: SemaDecl.cpp:13045
VarDecl * createLambdaInitCaptureVarDecl(SourceLocation Loc, QualType InitCaptureType, SourceLocation EllipsisLoc, IdentifierInfo *Id, unsigned InitStyle, Expr *Init, DeclContext *DeclCtx)
Create a dummy variable within the declcontext of the lambda's call operator, for name lookup purpose...
Definition: SemaLambda.cpp:807
ExprResult ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body)
ActOnLambdaExpr - This is called when the body of a lambda expression was successfully completed.
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: Sema.cpp:1911
ExprResult BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc, sema::LambdaScopeInfo *LSI)
Complete a lambda-expression having processed and attached the lambda body.
CXXRecordDecl * createLambdaClosureType(SourceRange IntroducerRange, TypeSourceInfo *Info, unsigned LambdaDependencyKind, LambdaCaptureDefault CaptureDefault)
Create a new lambda closure type.
Definition: SemaLambda.cpp:245
void PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, Decl *LambdaContextDecl=nullptr, ExpressionEvaluationContextRecord::ExpressionKind Type=ExpressionEvaluationContextRecord::EK_Other)
Definition: SemaExpr.cpp:18205
void CUDACheckLambdaCapture(CXXMethodDecl *D, const sema::Capture &Capture)
Definition: SemaCUDA.cpp:929
SmallVector< sema::FunctionScopeInfo *, 4 > FunctionScopes
Stack containing information about each of the nested function, block, and method scopes that are cur...
Definition: Sema.h:979
PoppedFunctionScopePtr PopFunctionScopeInfo(const sema::AnalysisBasedWarnings::Policy *WP=nullptr, const Decl *D=nullptr, QualType BlockType=QualType())
Pop a function (or block or lambda or captured region) scope from the stack.
Definition: Sema.cpp:2260
bool CheckCXXThisCapture(SourceLocation Loc, bool Explicit=false, bool BuildAndDiagnose=true, const unsigned *const FunctionScopeIndexToStopAt=nullptr, bool ByCopy=false)
Make sure the value of 'this' is actually available in the current context, if it is a potentially ev...
void ActOnLambdaExpressionAfterIntroducer(LambdaIntroducer &Intro, Scope *CurContext)
Once the Lambdas capture are known, we can start to create the closure, call operator method,...
void AddTemplateParametersToLambdaCallOperator(CXXMethodDecl *CallOperator, CXXRecordDecl *Class, TemplateParameterList *TemplateParams)
Definition: SemaLambda.cpp:955
ExprResult MaybeBindToTemporary(Expr *E)
MaybeBindToTemporary - If the passed in expression has a record type with a non-trivial destructor,...
void AddRangeBasedOptnone(FunctionDecl *FD)
Only called on function definitions; if there is a pragma in scope with the effect of a range-based o...
Definition: SemaAttr.cpp:1165
void addInitCapture(sema::LambdaScopeInfo *LSI, VarDecl *Var, bool ByRef)
Add an init-capture to a lambda scope.
Definition: SemaLambda.cpp:833
FieldDecl * BuildCaptureField(RecordDecl *RD, const sema::Capture &Capture)
Build a FieldDecl suitable to hold the given capture.
sema::LambdaScopeInfo * RebuildLambdaScopeInfo(CXXMethodDecl *CallOperator)
Definition: SemaDecl.cpp:15648
ASTContext & Context
Definition: Sema.h:1030
void PushOnScopeChains(NamedDecl *D, Scope *S, bool AddToContext=true)
Add this decl to the scope shadowed decl chains.
Definition: SemaDecl.cpp:1517
ASTContext & getASTContext() const
Definition: Sema.h:501
bool tryCaptureVariable(ValueDecl *Var, SourceLocation Loc, TryCaptureKind Kind, SourceLocation EllipsisLoc, bool BuildAndDiagnose, QualType &CaptureType, QualType &DeclRefType, const unsigned *const FunctionScopeIndexToStopAt)
Try to capture the given variable.
Definition: SemaExpr.cpp:19722
void PopExpressionEvaluationContext()
Definition: SemaExpr.cpp:18631
sema::LambdaScopeInfo * getCurGenericLambda()
Retrieve the current generic lambda info, if any.
Definition: Sema.cpp:2400
void handleLambdaNumbering(CXXRecordDecl *Class, CXXMethodDecl *Method, std::optional< CXXRecordDecl::LambdaNumbering > NumberingOverride=std::nullopt)
Number lambda for linkage purposes if necessary.
Definition: SemaLambda.cpp:413
LangAS getDefaultCXXMethodAddrSpace() const
Returns default addr space for method qualifiers.
Definition: Sema.cpp:1508
ExprResult BuildCaptureInit(const sema::Capture &Capture, SourceLocation ImplicitCaptureLoc, bool IsOpenMPMapping=false)
Initialize the given capture with a suitable expression.
FPOptions & getCurFPFeatures()
Definition: Sema.h:496
SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset=0)
Calls Lexer::getLocForEndOfToken()
Definition: Sema.cpp:58
@ UPPC_Initializer
An initializer.
Definition: Sema.h:11062
@ UPPC_DeclarationType
The type of an arbitrary declaration.
Definition: Sema.h:11035
void buildLambdaScope(sema::LambdaScopeInfo *LSI, CXXMethodDecl *CallOperator, SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault, SourceLocation CaptureDefaultLoc, bool ExplicitParams, bool Mutable)
Endow the lambda scope info with the relevant properties.
Definition: SemaLambda.cpp:480
const LangOptions & getLangOpts() const
Definition: Sema.h:494
bool CaptureHasSideEffects(const sema::Capture &From)
Does copying/destroying the captured variable have side effects?
void ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, Declarator &ParamInfo, const DeclSpec &DS)
ActOnStartOfLambdaDefinition - This is called just before we start parsing the body of a lambda; it a...
void ActOnLambdaClosureParameters(Scope *LambdaScope, MutableArrayRef< DeclaratorChunk::ParamInfo > ParamInfo)
bool CheckConstexprFunctionDefinition(const FunctionDecl *FD, CheckConstexprKind Kind)
bool DiagnoseUnexpandedParameterPack(SourceLocation Loc, TypeSourceInfo *T, UnexpandedParameterPackContext UPPC)
If the given type contains an unexpanded parameter pack, diagnose the error.
void DiagnoseInvalidExplicitObjectParameterInLambda(CXXMethodDecl *Method)
Definition: SemaLambda.cpp:389
const LangOptions & LangOpts
Definition: Sema.h:1028
sema::LambdaScopeInfo * getCurLambda(bool IgnoreNonLambdaCapturingScope=false)
Retrieve the current lambda scope info, if any.
Definition: Sema.cpp:2375
bool DiagnoseUnusedLambdaCapture(SourceRange CaptureRange, const sema::Capture &From)
Diagnose if an explicit lambda capture is unused.
QualType buildLambdaInitCaptureInitialization(SourceLocation Loc, bool ByRef, SourceLocation EllipsisLoc, std::optional< unsigned > NumExpansions, IdentifierInfo *Id, bool DirectInit, Expr *&Init)
Definition: SemaLambda.cpp:741
void CheckCXXDefaultArguments(FunctionDecl *FD)
Helpers for dealing with blocks and functions.
CleanupInfo Cleanup
Used to control the generation of ExprWithCleanups.
Definition: Sema.h:5371
void DiagnoseShadowingLambdaDecls(const sema::LambdaScopeInfo *LSI)
Diagnose shadowing for variables shadowed in the lambda record LambdaRD when these variables are capt...
Definition: SemaDecl.cpp:8475
Expr * BuildCXXThisExpr(SourceLocation Loc, QualType Type, bool IsImplicit)
Build a CXXThisExpr and mark it referenced in the current context.
QualType BuildReferenceType(QualType T, bool LValueRef, SourceLocation Loc, DeclarationName Entity)
Build a reference type.
Definition: SemaType.cpp:2264
ExprResult BuildDeclarationNameExpr(const CXXScopeSpec &SS, LookupResult &R, bool NeedsADL, bool AcceptInvalidDecl=false)
Definition: SemaExpr.cpp:3423
void DiagPlaceholderVariableDefinition(SourceLocation Loc)
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition: Sema.h:1163
void ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Decl *D)
Act on D, a function definition inside of an omp [begin/end] assumes.
bool inTemplateInstantiation() const
Determine whether we are currently performing template instantiation.
Definition: Sema.h:10722
TryCaptureKind
Definition: Sema.h:5458
@ TryCapture_Implicit
Definition: Sema.h:5459
@ TryCapture_ExplicitByVal
Definition: Sema.h:5460
@ TryCapture_ExplicitByRef
Definition: Sema.h:5461
void ActOnLambdaExplicitTemplateParameterList(LambdaIntroducer &Intro, SourceLocation LAngleLoc, ArrayRef< NamedDecl * > TParams, SourceLocation RAngleLoc, ExprResult RequiresClause)
This is called after parsing the explicit template parameter list on a lambda (if it exists) in C++2a...
Definition: SemaLambda.cpp:502
Decl * ActOnFinishFunctionBody(Decl *Decl, Stmt *Body)
Definition: SemaDecl.cpp:15997
void ActOnLambdaClosureQualifiers(LambdaIntroducer &Intro, SourceLocation MutableLoc)
void ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope, bool IsInstantiation=false)
ActOnLambdaError - If there is an error parsing a lambda, this callback is invoked to pop the informa...
bool DiagnoseUseOfDecl(NamedDecl *D, ArrayRef< SourceLocation > Locs, const ObjCInterfaceDecl *UnknownObjCClass=nullptr, bool ObjCPropertyAccess=false, bool AvoidPartialAvailabilityChecks=false, ObjCInterfaceDecl *ClassReciever=nullptr, bool SkipTrailingRequiresClause=false)
Determine whether the use of this declaration is valid, and emit any corresponding diagnostics.
Definition: SemaExpr.cpp:224
bool CheckParmsForFunctionDef(ArrayRef< ParmVarDecl * > Parameters, bool CheckParameterNames)
CheckParmsForFunctionDef - Check that the parameters of the given function are appropriate for the de...
void CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl, const LookupResult &R)
Diagnose variable or built-in function shadowing.
Definition: SemaDecl.cpp:8357
@ Normal
A normal translation unit fragment.
Definition: Sema.h:783
void CUDASetLambdaAttrs(CXXMethodDecl *Method)
Set device or host device attributes on the given lambda operator() method.
Definition: SemaCUDA.cpp:974
SmallVector< ExprWithCleanups::CleanupObject, 8 > ExprCleanupObjects
ExprCleanupObjects - This is the stack of objects requiring cleanup that are created by the current f...
Definition: Sema.h:5375
@ UnevaluatedAbstract
The current expression occurs within an unevaluated operand that unconditionally permits abstract ref...
@ UnevaluatedList
The current expression occurs within a braced-init-list within an unevaluated operand.
@ ConstantEvaluated
The current context is "potentially evaluated" in C++11 terms, but the expression is evaluated at com...
@ DiscardedStatement
The current expression occurs within a discarded statement.
@ PotentiallyEvaluated
The current expression is potentially evaluated at run time, which means that code may be generated t...
@ Unevaluated
The current expression and its subexpressions occur within an unevaluated operand (C++11 [expr]p7),...
@ ImmediateFunctionContext
In addition of being constant evaluated, the current expression occurs in an immediate function conte...
@ PotentiallyEvaluatedIfUsed
The current expression is potentially evaluated, but any declarations referenced inside that expressi...
TypeSourceInfo * GetTypeForDeclarator(Declarator &D)
GetTypeForDeclarator - Convert the type for the specified declarator to Type instances.
Definition: SemaType.cpp:6102
bool RequireCompleteType(SourceLocation Loc, QualType T, CompleteTypeKind Kind, TypeDiagnoser &Diagnoser)
Ensure that the type T is a complete type.
Definition: SemaType.cpp:9249
void ActOnFields(Scope *S, SourceLocation RecLoc, Decl *TagDecl, ArrayRef< Decl * > Fields, SourceLocation LBrac, SourceLocation RBrac, const ParsedAttributesView &AttrList)
Definition: SemaDecl.cpp:19273
void CheckExplicitObjectLambda(Declarator &D)
QualType getLambdaConversionFunctionResultType(const FunctionProtoType *CallOpType, CallingConv CC)
Get the return type to use for a lambda's conversion function(s) to function pointer type,...
void CheckCompletedCXXClass(Scope *S, CXXRecordDecl *Record)
Perform semantic checks on a class definition that has been completing, introducing implicitly-declar...
void DiscardCleanupsInEvaluationContext()
Definition: SemaExpr.cpp:18709
SmallVector< ExpressionEvaluationContextRecord, 8 > ExprEvalContexts
A stack of expression evaluation contexts.
Definition: Sema.h:6669
void PushDeclContext(Scope *S, DeclContext *DC)
Set the current declaration context until it gets popped.
Definition: SemaDecl.cpp:1324
CXXMethodDecl * CreateLambdaCallOperator(SourceRange IntroducerRange, CXXRecordDecl *Class)
Definition: SemaLambda.cpp:930
void deduceClosureReturnType(sema::CapturingScopeInfo &CSI)
Deduce a block or lambda's return type based on the return statements present in the body.
Definition: SemaLambda.cpp:648
ExprResult PerformCopyInitialization(const InitializedEntity &Entity, SourceLocation EqualLoc, ExprResult Init, bool TopLevelOfInitList=false, bool AllowExplicit=false)
Definition: SemaInit.cpp:10642
void PopDeclContext()
Definition: SemaDecl.cpp:1331
void ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD)
ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in it, apply them to D.
QualType SubstAutoTypeDependent(QualType TypeWithAuto)
std::tuple< MangleNumberingContext *, Decl * > getCurrentMangleNumberContext(const DeclContext *DC)
Compute the mangling number context for a lambda expression or block literal.
Definition: SemaLambda.cpp:278
void CompleteLambdaCallOperator(CXXMethodDecl *Method, SourceLocation LambdaLoc, SourceLocation CallOperatorLoc, Expr *TrailingRequiresClause, TypeSourceInfo *MethodTyInfo, ConstexprSpecKind ConstexprKind, StorageClass SC, ArrayRef< ParmVarDecl * > Params, bool HasExplicitResultType)
Definition: SemaLambda.cpp:966
void finishLambdaExplicitCaptures(sema::LambdaScopeInfo *LSI)
Note that we have finished the explicit captures for the given lambda.
Definition: SemaLambda.cpp:498
bool IsDerivedFrom(SourceLocation Loc, QualType Derived, QualType Base)
Determine whether the type Derived is a C++ class that is derived from the type Base.
@ CheckValid
Identify whether this function satisfies the formal rules for constexpr functions in the current lanu...
bool LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation=false, bool ForceNoCPlusPlus=false)
Perform unqualified name lookup starting from a given scope.
void NoteTemplateParameterLocation(const NamedDecl &Decl)
ExprResult ActOnFinishFullExpr(Expr *Expr, bool DiscardedValue)
Definition: Sema.h:6967
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
bool isValid() const
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:4377
Stmt - This represents one statement.
Definition: Stmt.h:84
SourceLocation getEndLoc() const LLVM_READONLY
Definition: Stmt.cpp:350
bool hasNameForLinkage() const
Is this tag type named, either directly or via being defined in a typedef of this type?
Definition: Decl.h:3773
Exposes information about the current target.
Definition: TargetInfo.h:213
virtual CallingConvCheckResult checkCallingConvention(CallingConv CC) const
Determines whether a given calling convention is valid for the target.
Definition: TargetInfo.h:1643
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Definition: DeclTemplate.h:413
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:73
ArrayRef< NamedDecl * > asArray()
Definition: DeclTemplate.h:139
TyLocType push(QualType T)
Pushes space for a new TypeLoc of the given type.
TypeSourceInfo * getTypeSourceInfo(ASTContext &Context, QualType T)
Creates a TypeSourceInfo for the given type.
Base wrapper for a particular "section" of type source info.
Definition: TypeLoc.h:59
T getAs() const
Convert to the specified TypeLoc type, returning a null TypeLoc if this TypeLoc is not of the desired...
Definition: TypeLoc.h:89
A container of type source information.
Definition: Type.h:6873
TypeLoc getTypeLoc() const
Return the TypeLoc wrapper for the type source info.
Definition: TypeLoc.h:256
QualType getType() const
Return the type wrapped by this type source info.
Definition: Type.h:6884
void setNameLoc(SourceLocation Loc)
Definition: TypeLoc.h:539
The base class of the type hierarchy.
Definition: Type.h:1606
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1819
bool isVoidType() const
Definition: Type.h:7443
bool isIntegralOrUnscopedEnumerationType() const
Determine whether this type is an integral or unscoped enumeration type.
Definition: Type.cpp:2008
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7724
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition: Type.cpp:651
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition: Type.h:2420
bool containsUnexpandedParameterPack() const
Whether this type is or contains an unexpanded parameter pack, used to support C++0x variadic templat...
Definition: Type.h:2094
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition: Type.h:7607
bool isUndeducedType() const
Determine whether this type is an undeduced type, meaning that it somehow involves a C++11 'auto' typ...
Definition: Type.h:7573
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
Definition: Type.cpp:2299
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7657
std::optional< NullabilityKind > getNullability() const
Determine the nullability of the given type.
Definition: Type.cpp:4516
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:706
void setType(QualType newType)
Definition: Decl.h:718
QualType getType() const
Definition: Decl.h:717
VarDecl * getPotentiallyDecomposedVarDecl()
Definition: DeclCXX.cpp:3318
bool isInitCapture() const
Whether this variable is the implicit variable for a lambda init-capture.
Definition: Decl.cpp:5330
Represents a variable declaration or definition.
Definition: Decl.h:918
static VarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S)
Definition: Decl.cpp:2148
void setInitStyle(InitializationStyle Style)
Definition: Decl.h:1429
void setInitCapture(bool IC)
Definition: Decl.h:1558
bool isInitCapture() const
Whether this variable is the implicit variable for a lambda init-capture.
Definition: Decl.h:1555
InitializationStyle
Initialization styles.
Definition: Decl.h:921
@ ListInit
Direct list-initialization (C++11)
Definition: Decl.h:929
@ CInit
C-style initialization with assignment.
Definition: Decl.h:923
@ CallInit
Call-style initialization (C++98)
Definition: Decl.h:926
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1168
void setInit(Expr *I)
Definition: Decl.cpp:2454
StorageClass getStorageClass() const
Returns the storage class as written in the source.
Definition: Decl.h:1152
bool isParameterPack() const
Determine whether this variable is actually a function parameter pack or init-capture pack.
Definition: Decl.cpp:2663
ValueDecl * getVariable() const
Definition: ScopeInfo.h:675
bool isVariableCapture() const
Definition: ScopeInfo.h:650
bool isBlockCapture() const
Definition: ScopeInfo.h:656
SourceLocation getLocation() const
Retrieve the location at which this variable was captured.
Definition: ScopeInfo.h:686
bool isNonODRUsed() const
Definition: ScopeInfo.h:667
bool isODRUsed() const
Definition: ScopeInfo.h:666
bool isInitCapture() const
Determine whether this capture is an init-capture.
Definition: ScopeInfo.cpp:222
bool isInvalid() const
Definition: ScopeInfo.h:661
bool isVLATypeCapture() const
Definition: ScopeInfo.h:657
SourceLocation getEllipsisLoc() const
Retrieve the source location of the ellipsis, whose presence indicates that the capture is a pack exp...
Definition: ScopeInfo.h:690
bool isThisCapture() const
Definition: ScopeInfo.h:649
QualType getCaptureType() const
Retrieve the capture type for this capture, which is effectively the type of the non-static data memb...
Definition: ScopeInfo.h:695
bool isCopyCapture() const
Definition: ScopeInfo.h:654
const VariableArrayType * getCapturedVLAType() const
Definition: ScopeInfo.h:680
QualType ReturnType
ReturnType - The target type of return statements in this context, or null if unknown.
Definition: ScopeInfo.h:729
bool isCaptured(ValueDecl *Var) const
Determine whether the given variable has been captured.
Definition: ScopeInfo.h:758
SmallVector< Capture, 4 > Captures
Captures - The captures.
Definition: ScopeInfo.h:721
ImplicitCaptureStyle ImpCaptureStyle
Definition: ScopeInfo.h:708
Capture & getCXXThisCapture()
Retrieve the capture of C++ 'this', if it has been captured.
Definition: ScopeInfo.h:752
bool isCXXThisCaptured() const
Determine whether the C++ 'this' is captured.
Definition: ScopeInfo.h:749
void addCapture(ValueDecl *Var, bool isBlock, bool isByref, bool isNested, SourceLocation Loc, SourceLocation EllipsisLoc, QualType CaptureType, bool Invalid)
Definition: ScopeInfo.h:731
SmallVector< ReturnStmt *, 4 > Returns
The list of return statements that occur within the function or block, if there is any chance of appl...
Definition: ScopeInfo.h:214
SourceLocation PotentialThisCaptureLocation
Definition: ScopeInfo.h:950
void finishedExplicitCaptures()
Note when all explicit captures have been added.
Definition: ScopeInfo.h:958
bool ContainsUnexpandedParameterPack
Whether the lambda contains an unexpanded parameter pack.
Definition: ScopeInfo.h:899
SmallVector< NamedDecl *, 4 > LocalPacks
Packs introduced by this lambda, if any.
Definition: ScopeInfo.h:902
CleanupInfo Cleanup
Whether any of the capture expressions requires cleanups.
Definition: ScopeInfo.h:896
SourceRange IntroducerRange
Source range covering the lambda introducer [...].
Definition: ScopeInfo.h:878
bool ExplicitParams
Whether the (empty) parameter list is explicit.
Definition: ScopeInfo.h:893
TemplateParameterList * GLTemplateParameterList
If this is a generic lambda, and the template parameter list has been created (from the TemplateParam...
Definition: ScopeInfo.h:915
ExprResult RequiresClause
The requires-clause immediately following the explicit template parameter list, if any.
Definition: ScopeInfo.h:910
SourceRange ExplicitTemplateParamsRange
Source range covering the explicit template parameter list (if it exists).
Definition: ScopeInfo.h:905
CXXRecordDecl * Lambda
The class that describes the lambda.
Definition: ScopeInfo.h:865
unsigned NumExplicitCaptures
The number of captures in the Captures list that are explicit captures.
Definition: ScopeInfo.h:886
SourceLocation CaptureDefaultLoc
Source location of the '&' or '=' specifying the default capture type, if any.
Definition: ScopeInfo.h:882
llvm::DenseMap< unsigned, SourceRange > ExplicitCaptureRanges
A map of explicit capture indices to their introducer source ranges.
Definition: ScopeInfo.h:939
bool AfterParameterList
Indicate that we parsed the parameter list at which point the mutability of the lambda is known.
Definition: ScopeInfo.h:873
CXXMethodDecl * CallOperator
The lambda's compiler-generated operator().
Definition: ScopeInfo.h:868
bool Mutable
Whether this is a mutable lambda.
Definition: ScopeInfo.h:890
Defines the clang::TargetInfo interface.
The JSON file list parser is used to communicate input to InstallAPI.
@ CPlusPlus20
Definition: LangStandard.h:58
@ CPlusPlus
Definition: LangStandard.h:54
@ CPlusPlus14
Definition: LangStandard.h:56
@ CPlusPlus17
Definition: LangStandard.h:57
DeclContext * getLambdaAwareParentOfDeclContext(DeclContext *DC)
Definition: ASTLambda.h:95
std::optional< unsigned > getStackIndexOfNearestEnclosingCaptureCapableLambda(ArrayRef< const sema::FunctionScopeInfo * > FunctionScopes, ValueDecl *VarToCapture, Sema &S)
Examines the FunctionScopeInfo stack to determine the nearest enclosing lambda (to the current lambda...
Definition: SemaLambda.cpp:176
ConstexprSpecKind
Define the kind of constexpr specifier.
Definition: Specifiers.h:35
@ ICIS_NoInit
No in-class initializer.
Definition: Specifiers.h:269
@ RQ_None
No ref-qualifier was provided.
Definition: Type.h:1555
LambdaCaptureKind
The different capture forms in a lambda introducer.
Definition: Lambda.h:33
@ LCK_ByCopy
Capturing by copy (a.k.a., by value)
Definition: Lambda.h:36
@ LCK_ByRef
Capturing by reference.
Definition: Lambda.h:37
@ LCK_VLAType
Capturing variable-length array type.
Definition: Lambda.h:38
@ LCK_StarThis
Capturing the *this object by copy.
Definition: Lambda.h:35
@ LCK_This
Capturing the *this object by reference.
Definition: Lambda.h:34
bool isLambdaCallWithExplicitObjectParameter(const DeclContext *DC)
Definition: ASTLambda.h:38
StorageClass
Storage classes.
Definition: Specifiers.h:245
@ SC_Auto
Definition: Specifiers.h:253
@ SC_Static
Definition: Specifiers.h:249
@ SC_None
Definition: Specifiers.h:247
bool FTIHasSingleVoidParameter(const DeclaratorChunk::FunctionTypeInfo &FTI)
Definition: SemaInternal.h:29
@ CopyInit
[a = b], [a = {b}]
bool isLambdaCallOperator(const CXXMethodDecl *MD)
Definition: ASTLambda.h:27
@ Result
The result type of a method or function.
bool hasWeakerNullability(NullabilityKind L, NullabilityKind R)
Return true if L has a weaker nullability annotation than R.
Definition: Specifiers.h:353
ActionResult< Expr * > ExprResult
Definition: Ownership.h:248
ExprResult ExprError()
Definition: Ownership.h:264
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
LambdaCaptureDefault
The default, if any, capture method for a lambda expression.
Definition: Lambda.h:22
@ LCD_ByRef
Definition: Lambda.h:25
@ LCD_None
Definition: Lambda.h:23
@ LCD_ByCopy
Definition: Lambda.h:24
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:132
StringRef getLambdaStaticInvokerName()
Definition: ASTLambda.h:22
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
CallingConv
CallingConv - Specifies the calling convention that a function uses.
Definition: Specifiers.h:275
@ CC_C
Definition: Specifiers.h:276
@ CC_X86VectorCall
Definition: Specifiers.h:280
@ CC_X86StdCall
Definition: Specifiers.h:277
@ CC_X86FastCall
Definition: Specifiers.h:278
@ Class
The "class" keyword introduces the elaborated-type-specifier.
@ EST_BasicNoexcept
noexcept
@ AS_public
Definition: Specifiers.h:121
@ AS_private
Definition: Specifiers.h:123
MutableArrayRef< Expr * > MultiExprArg
Definition: Ownership.h:258
Information about how a lambda is numbered within its context.
Definition: DeclCXX.h:1797
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
SourceLocation getTrailingReturnTypeLoc() const
Get the trailing-return-type location for this function declarator.
Definition: DeclSpec.h:1592
bool hasTrailingReturnType() const
Determine whether this function declarator had a trailing-return-type.
Definition: DeclSpec.h:1583
ParsedType getTrailingReturnType() const
Get the trailing-return-type for this function declarator.
Definition: DeclSpec.h:1586
bool hasMutableQualifier() const
Determine whether this lambda-declarator contains a 'mutable' qualifier.
Definition: DeclSpec.h:1555
ParamInfo - An array of paraminfo objects is allocated whenever a function declarator is parsed.
Definition: DeclSpec.h:1329
ExceptionSpecificationType Type
The kind of exception specification this is.
Definition: Type.h:4252
Extra information about a function prototype.
Definition: Type.h:4278
ExceptionSpecInfo ExceptionSpec
Definition: Type.h:4285
FunctionType::ExtInfo ExtInfo
Definition: Type.h:4279
unsigned NumExplicitTemplateParams
The number of parameters in the template parameter list that were explicitly specified by the user,...
Definition: DeclSpec.h:2882
SmallVector< NamedDecl *, 4 > TemplateParams
Store the list of the template parameters for a generic lambda or an abbreviated function template.
Definition: DeclSpec.h:2895
Represents a complete lambda introducer.
Definition: DeclSpec.h:2831
SmallVector< LambdaCapture, 4 > Captures
Definition: DeclSpec.h:2856
SourceLocation DefaultLoc
Definition: DeclSpec.h:2854
LambdaCaptureDefault Default
Definition: DeclSpec.h:2855
An RAII helper that pops function a function scope on exit.
Definition: Sema.h:1050