/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaLambda.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- SemaLambda.cpp - Semantic Analysis for C++11 Lambdas -------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements semantic analysis for C++ lambda expressions. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | #include "clang/Sema/DeclSpec.h" |
13 | | #include "TypeLocBuilder.h" |
14 | | #include "clang/AST/ASTLambda.h" |
15 | | #include "clang/AST/ExprCXX.h" |
16 | | #include "clang/Basic/TargetInfo.h" |
17 | | #include "clang/Sema/Initialization.h" |
18 | | #include "clang/Sema/Lookup.h" |
19 | | #include "clang/Sema/Scope.h" |
20 | | #include "clang/Sema/ScopeInfo.h" |
21 | | #include "clang/Sema/SemaInternal.h" |
22 | | #include "clang/Sema/SemaLambda.h" |
23 | | #include "llvm/ADT/STLExtras.h" |
24 | | using namespace clang; |
25 | | using namespace sema; |
26 | | |
27 | | /// Examines the FunctionScopeInfo stack to determine the nearest |
28 | | /// enclosing lambda (to the current lambda) that is 'capture-ready' for |
29 | | /// the variable referenced in the current lambda (i.e. \p VarToCapture). |
30 | | /// If successful, returns the index into Sema's FunctionScopeInfo stack |
31 | | /// of the capture-ready lambda's LambdaScopeInfo. |
32 | | /// |
33 | | /// Climbs down the stack of lambdas (deepest nested lambda - i.e. current |
34 | | /// lambda - is on top) to determine the index of the nearest enclosing/outer |
35 | | /// lambda that is ready to capture the \p VarToCapture being referenced in |
36 | | /// the current lambda. |
37 | | /// As we climb down the stack, we want the index of the first such lambda - |
38 | | /// that is the lambda with the highest index that is 'capture-ready'. |
39 | | /// |
40 | | /// A lambda 'L' is capture-ready for 'V' (var or this) if: |
41 | | /// - its enclosing context is non-dependent |
42 | | /// - and if the chain of lambdas between L and the lambda in which |
43 | | /// V is potentially used (i.e. the lambda at the top of the scope info |
44 | | /// stack), can all capture or have already captured V. |
45 | | /// If \p VarToCapture is 'null' then we are trying to capture 'this'. |
46 | | /// |
47 | | /// Note that a lambda that is deemed 'capture-ready' still needs to be checked |
48 | | /// for whether it is 'capture-capable' (see |
49 | | /// getStackIndexOfNearestEnclosingCaptureCapableLambda), before it can truly |
50 | | /// capture. |
51 | | /// |
52 | | /// \param FunctionScopes - Sema's stack of nested FunctionScopeInfo's (which a |
53 | | /// LambdaScopeInfo inherits from). The current/deepest/innermost lambda |
54 | | /// is at the top of the stack and has the highest index. |
55 | | /// \param VarToCapture - the variable to capture. If NULL, capture 'this'. |
56 | | /// |
57 | | /// \returns An Optional<unsigned> Index that if evaluates to 'true' contains |
58 | | /// the index (into Sema's FunctionScopeInfo stack) of the innermost lambda |
59 | | /// which is capture-ready. If the return value evaluates to 'false' then |
60 | | /// no lambda is capture-ready for \p VarToCapture. |
61 | | |
62 | | static inline Optional<unsigned> |
63 | | getStackIndexOfNearestEnclosingCaptureReadyLambda( |
64 | | ArrayRef<const clang::sema::FunctionScopeInfo *> FunctionScopes, |
65 | 1.35k | VarDecl *VarToCapture) { |
66 | | // Label failure to capture. |
67 | 1.35k | const Optional<unsigned> NoLambdaIsCaptureReady; |
68 | | |
69 | | // Ignore all inner captured regions. |
70 | 1.35k | unsigned CurScopeIndex = FunctionScopes.size() - 1; |
71 | 1.36k | while (CurScopeIndex > 0 && isa<clang::sema::CapturedRegionScopeInfo>( |
72 | 1.36k | FunctionScopes[CurScopeIndex])) |
73 | 5 | --CurScopeIndex; |
74 | 1.35k | assert( |
75 | 1.35k | isa<clang::sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex]) && |
76 | 1.35k | "The function on the top of sema's function-info stack must be a lambda"); |
77 | | |
78 | | // If VarToCapture is null, we are attempting to capture 'this'. |
79 | 0 | const bool IsCapturingThis = !VarToCapture; |
80 | 1.35k | const bool IsCapturingVariable = !IsCapturingThis; |
81 | | |
82 | | // Start with the current lambda at the top of the stack (highest index). |
83 | 1.35k | DeclContext *EnclosingDC = |
84 | 1.35k | cast<sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex])->CallOperator; |
85 | | |
86 | 1.94k | do { |
87 | 1.94k | const clang::sema::LambdaScopeInfo *LSI = |
88 | 1.94k | cast<sema::LambdaScopeInfo>(FunctionScopes[CurScopeIndex]); |
89 | | // IF we have climbed down to an intervening enclosing lambda that contains |
90 | | // the variable declaration - it obviously can/must not capture the |
91 | | // variable. |
92 | | // Since its enclosing DC is dependent, all the lambdas between it and the |
93 | | // innermost nested lambda are dependent (otherwise we wouldn't have |
94 | | // arrived here) - so we don't yet have a lambda that can capture the |
95 | | // variable. |
96 | 1.94k | if (IsCapturingVariable && |
97 | 1.94k | VarToCapture->getDeclContext()->Equals(EnclosingDC)1.78k ) |
98 | 219 | return NoLambdaIsCaptureReady; |
99 | | |
100 | | // For an enclosing lambda to be capture ready for an entity, all |
101 | | // intervening lambda's have to be able to capture that entity. If even |
102 | | // one of the intervening lambda's is not capable of capturing the entity |
103 | | // then no enclosing lambda can ever capture that entity. |
104 | | // For e.g. |
105 | | // const int x = 10; |
106 | | // [=](auto a) { #1 |
107 | | // [](auto b) { #2 <-- an intervening lambda that can never capture 'x' |
108 | | // [=](auto c) { #3 |
109 | | // f(x, c); <-- can not lead to x's speculative capture by #1 or #2 |
110 | | // }; }; }; |
111 | | // If they do not have a default implicit capture, check to see |
112 | | // if the entity has already been explicitly captured. |
113 | | // If even a single dependent enclosing lambda lacks the capability |
114 | | // to ever capture this variable, there is no further enclosing |
115 | | // non-dependent lambda that can capture this variable. |
116 | 1.72k | if (LSI->ImpCaptureStyle == sema::LambdaScopeInfo::ImpCap_None) { |
117 | 306 | if (IsCapturingVariable && !LSI->isCaptured(VarToCapture)254 ) |
118 | 173 | return NoLambdaIsCaptureReady; |
119 | 133 | if (IsCapturingThis && !LSI->isCXXThisCaptured()52 ) |
120 | 48 | return NoLambdaIsCaptureReady; |
121 | 133 | } |
122 | 1.50k | EnclosingDC = getLambdaAwareParentOfDeclContext(EnclosingDC); |
123 | | |
124 | 1.50k | assert(CurScopeIndex); |
125 | 0 | --CurScopeIndex; |
126 | 1.50k | } while (!EnclosingDC->isTranslationUnit() && |
127 | 1.50k | EnclosingDC->isDependentContext() && |
128 | 1.50k | isLambdaCallOperator(EnclosingDC)1.08k ); |
129 | | |
130 | 917 | assert(CurScopeIndex < (FunctionScopes.size() - 1)); |
131 | | // If the enclosingDC is not dependent, then the immediately nested lambda |
132 | | // (one index above) is capture-ready. |
133 | 917 | if (!EnclosingDC->isDependentContext()) |
134 | 415 | return CurScopeIndex + 1; |
135 | 502 | return NoLambdaIsCaptureReady; |
136 | 917 | } |
137 | | |
138 | | /// Examines the FunctionScopeInfo stack to determine the nearest |
139 | | /// enclosing lambda (to the current lambda) that is 'capture-capable' for |
140 | | /// the variable referenced in the current lambda (i.e. \p VarToCapture). |
141 | | /// If successful, returns the index into Sema's FunctionScopeInfo stack |
142 | | /// of the capture-capable lambda's LambdaScopeInfo. |
143 | | /// |
144 | | /// Given the current stack of lambdas being processed by Sema and |
145 | | /// the variable of interest, to identify the nearest enclosing lambda (to the |
146 | | /// current lambda at the top of the stack) that can truly capture |
147 | | /// a variable, it has to have the following two properties: |
148 | | /// a) 'capture-ready' - be the innermost lambda that is 'capture-ready': |
149 | | /// - climb down the stack (i.e. starting from the innermost and examining |
150 | | /// each outer lambda step by step) checking if each enclosing |
151 | | /// lambda can either implicitly or explicitly capture the variable. |
152 | | /// Record the first such lambda that is enclosed in a non-dependent |
153 | | /// context. If no such lambda currently exists return failure. |
154 | | /// b) 'capture-capable' - make sure the 'capture-ready' lambda can truly |
155 | | /// capture the variable by checking all its enclosing lambdas: |
156 | | /// - check if all outer lambdas enclosing the 'capture-ready' lambda |
157 | | /// identified above in 'a' can also capture the variable (this is done |
158 | | /// via tryCaptureVariable for variables and CheckCXXThisCapture for |
159 | | /// 'this' by passing in the index of the Lambda identified in step 'a') |
160 | | /// |
161 | | /// \param FunctionScopes - Sema's stack of nested FunctionScopeInfo's (which a |
162 | | /// LambdaScopeInfo inherits from). The current/deepest/innermost lambda |
163 | | /// is at the top of the stack. |
164 | | /// |
165 | | /// \param VarToCapture - the variable to capture. If NULL, capture 'this'. |
166 | | /// |
167 | | /// |
168 | | /// \returns An Optional<unsigned> Index that if evaluates to 'true' contains |
169 | | /// the index (into Sema's FunctionScopeInfo stack) of the innermost lambda |
170 | | /// which is capture-capable. If the return value evaluates to 'false' then |
171 | | /// no lambda is capture-capable for \p VarToCapture. |
172 | | |
173 | | Optional<unsigned> clang::getStackIndexOfNearestEnclosingCaptureCapableLambda( |
174 | | ArrayRef<const sema::FunctionScopeInfo *> FunctionScopes, |
175 | 1.35k | VarDecl *VarToCapture, Sema &S) { |
176 | | |
177 | 1.35k | const Optional<unsigned> NoLambdaIsCaptureCapable; |
178 | | |
179 | 1.35k | const Optional<unsigned> OptionalStackIndex = |
180 | 1.35k | getStackIndexOfNearestEnclosingCaptureReadyLambda(FunctionScopes, |
181 | 1.35k | VarToCapture); |
182 | 1.35k | if (!OptionalStackIndex) |
183 | 942 | return NoLambdaIsCaptureCapable; |
184 | | |
185 | 415 | const unsigned IndexOfCaptureReadyLambda = *OptionalStackIndex; |
186 | 415 | assert(((IndexOfCaptureReadyLambda != (FunctionScopes.size() - 1)) || |
187 | 415 | S.getCurGenericLambda()) && |
188 | 415 | "The capture ready lambda for a potential capture can only be the " |
189 | 415 | "current lambda if it is a generic lambda"); |
190 | | |
191 | 0 | const sema::LambdaScopeInfo *const CaptureReadyLambdaLSI = |
192 | 415 | cast<sema::LambdaScopeInfo>(FunctionScopes[IndexOfCaptureReadyLambda]); |
193 | | |
194 | | // If VarToCapture is null, we are attempting to capture 'this' |
195 | 415 | const bool IsCapturingThis = !VarToCapture; |
196 | 415 | const bool IsCapturingVariable = !IsCapturingThis; |
197 | | |
198 | 415 | if (IsCapturingVariable) { |
199 | | // Check if the capture-ready lambda can truly capture the variable, by |
200 | | // checking whether all enclosing lambdas of the capture-ready lambda allow |
201 | | // the capture - i.e. make sure it is capture-capable. |
202 | 395 | QualType CaptureType, DeclRefType; |
203 | 395 | const bool CanCaptureVariable = |
204 | 395 | !S.tryCaptureVariable(VarToCapture, |
205 | 395 | /*ExprVarIsUsedInLoc*/ SourceLocation(), |
206 | 395 | clang::Sema::TryCapture_Implicit, |
207 | 395 | /*EllipsisLoc*/ SourceLocation(), |
208 | 395 | /*BuildAndDiagnose*/ false, CaptureType, |
209 | 395 | DeclRefType, &IndexOfCaptureReadyLambda); |
210 | 395 | if (!CanCaptureVariable) |
211 | 28 | return NoLambdaIsCaptureCapable; |
212 | 395 | } else { |
213 | | // Check if the capture-ready lambda can truly capture 'this' by checking |
214 | | // whether all enclosing lambdas of the capture-ready lambda can capture |
215 | | // 'this'. |
216 | 20 | const bool CanCaptureThis = |
217 | 20 | !S.CheckCXXThisCapture( |
218 | 20 | CaptureReadyLambdaLSI->PotentialThisCaptureLocation, |
219 | 20 | /*Explicit*/ false, /*BuildAndDiagnose*/ false, |
220 | 20 | &IndexOfCaptureReadyLambda); |
221 | 20 | if (!CanCaptureThis) |
222 | 4 | return NoLambdaIsCaptureCapable; |
223 | 20 | } |
224 | 383 | return IndexOfCaptureReadyLambda; |
225 | 415 | } |
226 | | |
227 | | static inline TemplateParameterList * |
228 | 22.3k | getGenericLambdaTemplateParameterList(LambdaScopeInfo *LSI, Sema &SemaRef) { |
229 | 22.3k | if (!LSI->GLTemplateParameterList && !LSI->TemplateParams.empty()17.1k ) { |
230 | 2.36k | LSI->GLTemplateParameterList = TemplateParameterList::Create( |
231 | 2.36k | SemaRef.Context, |
232 | 2.36k | /*Template kw loc*/ SourceLocation(), |
233 | 2.36k | /*L angle loc*/ LSI->ExplicitTemplateParamsRange.getBegin(), |
234 | 2.36k | LSI->TemplateParams, |
235 | 2.36k | /*R angle loc*/LSI->ExplicitTemplateParamsRange.getEnd(), |
236 | 2.36k | LSI->RequiresClause.get()); |
237 | 2.36k | } |
238 | 22.3k | return LSI->GLTemplateParameterList; |
239 | 22.3k | } |
240 | | |
241 | | CXXRecordDecl * |
242 | | Sema::createLambdaClosureType(SourceRange IntroducerRange, TypeSourceInfo *Info, |
243 | | unsigned LambdaDependencyKind, |
244 | 11.1k | LambdaCaptureDefault CaptureDefault) { |
245 | 11.1k | DeclContext *DC = CurContext; |
246 | 11.1k | while (!(DC->isFunctionOrMethod() || DC->isRecord()1.68k || DC->isFileContext()1.15k )) |
247 | 18 | DC = DC->getParent(); |
248 | 11.1k | bool IsGenericLambda = getGenericLambdaTemplateParameterList(getCurLambda(), |
249 | 11.1k | *this); |
250 | | // Start constructing the lambda class. |
251 | 11.1k | CXXRecordDecl *Class = CXXRecordDecl::CreateLambda( |
252 | 11.1k | Context, DC, Info, IntroducerRange.getBegin(), LambdaDependencyKind, |
253 | 11.1k | IsGenericLambda, CaptureDefault); |
254 | 11.1k | DC->addDecl(Class); |
255 | | |
256 | 11.1k | return Class; |
257 | 11.1k | } |
258 | | |
259 | | /// Determine whether the given context is or is enclosed in an inline |
260 | | /// function. |
261 | 1.17M | static bool isInInlineFunction(const DeclContext *DC) { |
262 | 1.63M | while (!DC->isFileContext()) { |
263 | 473k | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
264 | 27.1k | if (FD->isInlined()) |
265 | 11.4k | return true; |
266 | | |
267 | 461k | DC = DC->getLexicalParent(); |
268 | 461k | } |
269 | | |
270 | 1.16M | return false; |
271 | 1.17M | } |
272 | | |
273 | | std::tuple<MangleNumberingContext *, Decl *> |
274 | 1.20M | Sema::getCurrentMangleNumberContext(const DeclContext *DC) { |
275 | | // Compute the context for allocating mangling numbers in the current |
276 | | // expression, if the ABI requires them. |
277 | 1.20M | Decl *ManglingContextDecl = ExprEvalContexts.back().ManglingContextDecl; |
278 | | |
279 | 1.20M | enum ContextKind { |
280 | 1.20M | Normal, |
281 | 1.20M | DefaultArgument, |
282 | 1.20M | DataMember, |
283 | 1.20M | StaticDataMember, |
284 | 1.20M | InlineVariable, |
285 | 1.20M | VariableTemplate |
286 | 1.20M | } Kind = Normal; |
287 | | |
288 | | // Default arguments of member function parameters that appear in a class |
289 | | // definition, as well as the initializers of data members, receive special |
290 | | // treatment. Identify them. |
291 | 1.20M | if (ManglingContextDecl) { |
292 | 2.73k | if (ParmVarDecl *Param = dyn_cast<ParmVarDecl>(ManglingContextDecl)) { |
293 | 792 | if (const DeclContext *LexicalDC |
294 | 792 | = Param->getDeclContext()->getLexicalParent()) |
295 | 214 | if (LexicalDC->isRecord()) |
296 | 155 | Kind = DefaultArgument; |
297 | 1.94k | } else if (VarDecl *Var = dyn_cast<VarDecl>(ManglingContextDecl)) { |
298 | 1.72k | if (Var->getDeclContext()->isRecord()) |
299 | 22 | Kind = StaticDataMember; |
300 | 1.70k | else if (Var->getMostRecentDecl()->isInline()) |
301 | 23 | Kind = InlineVariable; |
302 | 1.67k | else if (Var->getDescribedVarTemplate()) |
303 | 20 | Kind = VariableTemplate; |
304 | 1.65k | else if (auto *VTS = dyn_cast<VarTemplateSpecializationDecl>(Var)) { |
305 | 11 | if (!VTS->isExplicitSpecialization()) |
306 | 11 | Kind = VariableTemplate; |
307 | 11 | } |
308 | 1.72k | } else if (217 isa<FieldDecl>(ManglingContextDecl)217 ) { |
309 | 217 | Kind = DataMember; |
310 | 217 | } |
311 | 2.73k | } |
312 | | |
313 | | // Itanium ABI [5.1.7]: |
314 | | // In the following contexts [...] the one-definition rule requires closure |
315 | | // types in different translation units to "correspond": |
316 | 1.20M | bool IsInNonspecializedTemplate = |
317 | 1.20M | inTemplateInstantiation() || CurContext->isDependentContext()1.20M ; |
318 | 1.20M | switch (Kind) { |
319 | 1.20M | case Normal: { |
320 | | // -- the bodies of non-exported nonspecialized template functions |
321 | | // -- the bodies of inline functions |
322 | 1.20M | if ((IsInNonspecializedTemplate && |
323 | 1.20M | !(29.3k ManglingContextDecl29.3k && isa<ParmVarDecl>(ManglingContextDecl)978 )) || |
324 | 1.20M | isInInlineFunction(CurContext)1.17M ) { |
325 | 42.7k | while (auto *CD = dyn_cast<CapturedDecl>(DC)) |
326 | 2.03k | DC = CD->getParent(); |
327 | 40.6k | return std::make_tuple(&Context.getManglingNumberContext(DC), nullptr); |
328 | 40.6k | } |
329 | | |
330 | 1.16M | return std::make_tuple(nullptr, nullptr); |
331 | 1.20M | } |
332 | | |
333 | 22 | case StaticDataMember: |
334 | | // -- the initializers of nonspecialized static members of template classes |
335 | 22 | if (!IsInNonspecializedTemplate) |
336 | 2 | return std::make_tuple(nullptr, ManglingContextDecl); |
337 | | // Fall through to get the current context. |
338 | 22 | LLVM_FALLTHROUGH20 ;20 |
339 | | |
340 | 237 | case DataMember: |
341 | | // -- the in-class initializers of class members |
342 | 392 | case DefaultArgument: |
343 | | // -- default arguments appearing in class definitions |
344 | 415 | case InlineVariable: |
345 | | // -- the initializers of inline variables |
346 | 446 | case VariableTemplate: |
347 | | // -- the initializers of templated variables |
348 | 446 | return std::make_tuple( |
349 | 446 | &Context.getManglingNumberContext(ASTContext::NeedExtraManglingDecl, |
350 | 446 | ManglingContextDecl), |
351 | 446 | ManglingContextDecl); |
352 | 1.20M | } |
353 | | |
354 | 0 | llvm_unreachable("unexpected context"); |
355 | 0 | } |
356 | | |
357 | | CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, |
358 | | SourceRange IntroducerRange, |
359 | | TypeSourceInfo *MethodTypeInfo, |
360 | | SourceLocation EndLoc, |
361 | | ArrayRef<ParmVarDecl *> Params, |
362 | | ConstexprSpecKind ConstexprKind, |
363 | 11.1k | Expr *TrailingRequiresClause) { |
364 | 11.1k | QualType MethodType = MethodTypeInfo->getType(); |
365 | 11.1k | TemplateParameterList *TemplateParams = |
366 | 11.1k | getGenericLambdaTemplateParameterList(getCurLambda(), *this); |
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 | 11.1k | if (Class->isDependentContext() || TemplateParams8.34k ) { |
371 | 5.18k | const FunctionProtoType *FPT = MethodType->castAs<FunctionProtoType>(); |
372 | 5.18k | QualType Result = FPT->getReturnType(); |
373 | 5.18k | if (Result->isUndeducedType()) { |
374 | 3.75k | Result = SubstAutoTypeDependent(Result); |
375 | 3.75k | MethodType = Context.getFunctionType(Result, FPT->getParamTypes(), |
376 | 3.75k | FPT->getExtProtoInfo()); |
377 | 3.75k | } |
378 | 5.18k | } |
379 | | |
380 | | // C++11 [expr.prim.lambda]p5: |
381 | | // The closure type for a lambda-expression has a public inline function |
382 | | // call operator (13.5.4) whose parameters and return type are described by |
383 | | // the lambda-expression's parameter-declaration-clause and |
384 | | // trailing-return-type respectively. |
385 | 11.1k | DeclarationName MethodName |
386 | 11.1k | = Context.DeclarationNames.getCXXOperatorName(OO_Call); |
387 | 11.1k | DeclarationNameLoc MethodNameLoc = |
388 | 11.1k | DeclarationNameLoc::makeCXXOperatorNameLoc(IntroducerRange); |
389 | 11.1k | CXXMethodDecl *Method = CXXMethodDecl::Create( |
390 | 11.1k | Context, Class, EndLoc, |
391 | 11.1k | DeclarationNameInfo(MethodName, IntroducerRange.getBegin(), |
392 | 11.1k | MethodNameLoc), |
393 | 11.1k | MethodType, MethodTypeInfo, SC_None, getCurFPFeatures().isFPConstrained(), |
394 | 11.1k | /*isInline=*/true, ConstexprKind, EndLoc, TrailingRequiresClause); |
395 | 11.1k | Method->setAccess(AS_public); |
396 | 11.1k | if (!TemplateParams) |
397 | 7.39k | Class->addDecl(Method); |
398 | | |
399 | | // Temporarily set the lexical declaration context to the current |
400 | | // context, so that the Scope stack matches the lexical nesting. |
401 | 11.1k | Method->setLexicalDeclContext(CurContext); |
402 | | // Create a function template if we have a template parameter list |
403 | 11.1k | FunctionTemplateDecl *const TemplateMethod = TemplateParams ? |
404 | 3.77k | FunctionTemplateDecl::Create(Context, Class, |
405 | 3.77k | Method->getLocation(), MethodName, |
406 | 3.77k | TemplateParams, |
407 | 7.39k | Method) : nullptr; |
408 | 11.1k | if (TemplateMethod) { |
409 | 3.77k | TemplateMethod->setAccess(AS_public); |
410 | 3.77k | Method->setDescribedFunctionTemplate(TemplateMethod); |
411 | 3.77k | Class->addDecl(TemplateMethod); |
412 | 3.77k | TemplateMethod->setLexicalDeclContext(CurContext); |
413 | 3.77k | } |
414 | | |
415 | | // Add parameters. |
416 | 11.1k | if (!Params.empty()) { |
417 | 5.85k | Method->setParams(Params); |
418 | 5.85k | CheckParmsForFunctionDef(Params, |
419 | 5.85k | /*CheckParameterNames=*/false); |
420 | | |
421 | 5.85k | for (auto P : Method->parameters()) |
422 | 7.12k | P->setOwningFunction(Method); |
423 | 5.85k | } |
424 | | |
425 | 11.1k | return Method; |
426 | 11.1k | } |
427 | | |
428 | | void Sema::handleLambdaNumbering( |
429 | | CXXRecordDecl *Class, CXXMethodDecl *Method, |
430 | 11.1k | Optional<std::tuple<bool, unsigned, unsigned, Decl *>> Mangling) { |
431 | 11.1k | if (Mangling) { |
432 | 7 | bool HasKnownInternalLinkage; |
433 | 7 | unsigned ManglingNumber, DeviceManglingNumber; |
434 | 7 | Decl *ManglingContextDecl; |
435 | 7 | std::tie(HasKnownInternalLinkage, ManglingNumber, DeviceManglingNumber, |
436 | 7 | ManglingContextDecl) = *Mangling; |
437 | 7 | Class->setLambdaMangling(ManglingNumber, ManglingContextDecl, |
438 | 7 | HasKnownInternalLinkage); |
439 | 7 | Class->setDeviceLambdaManglingNumber(DeviceManglingNumber); |
440 | 7 | return; |
441 | 7 | } |
442 | | |
443 | 11.1k | auto getMangleNumberingContext = |
444 | 11.1k | [this](CXXRecordDecl *Class, |
445 | 11.1k | Decl *ManglingContextDecl) -> MangleNumberingContext * { |
446 | | // Get mangle numbering context if there's any extra decl context. |
447 | 248 | if (ManglingContextDecl) |
448 | 0 | return &Context.getManglingNumberContext( |
449 | 0 | ASTContext::NeedExtraManglingDecl, ManglingContextDecl); |
450 | | // Otherwise, from that lambda's decl context. |
451 | 248 | auto DC = Class->getDeclContext(); |
452 | 248 | while (auto *CD = dyn_cast<CapturedDecl>(DC)) |
453 | 0 | DC = CD->getParent(); |
454 | 248 | return &Context.getManglingNumberContext(DC); |
455 | 248 | }; |
456 | | |
457 | 11.1k | MangleNumberingContext *MCtx; |
458 | 11.1k | Decl *ManglingContextDecl; |
459 | 11.1k | std::tie(MCtx, ManglingContextDecl) = |
460 | 11.1k | getCurrentMangleNumberContext(Class->getDeclContext()); |
461 | 11.1k | bool HasKnownInternalLinkage = false; |
462 | 11.1k | if (!MCtx && (4.61k getLangOpts().CUDA4.61k || getLangOpts().SYCLIsDevice4.42k || |
463 | 4.61k | getLangOpts().SYCLIsHost4.36k )) { |
464 | | // Force lambda numbering in CUDA/HIP as we need to name lambdas following |
465 | | // ODR. Both device- and host-compilation need to have a consistent naming |
466 | | // on kernel functions. As lambdas are potential part of these `__global__` |
467 | | // function names, they needs numbering following ODR. |
468 | | // Also force for SYCL, since we need this for the |
469 | | // __builtin_sycl_unique_stable_name implementation, which depends on lambda |
470 | | // mangling. |
471 | 248 | MCtx = getMangleNumberingContext(Class, ManglingContextDecl); |
472 | 248 | assert(MCtx && "Retrieving mangle numbering context failed!"); |
473 | 0 | HasKnownInternalLinkage = true; |
474 | 248 | } |
475 | 11.1k | if (MCtx) { |
476 | 6.80k | unsigned ManglingNumber = MCtx->getManglingNumber(Method); |
477 | 6.80k | Class->setLambdaMangling(ManglingNumber, ManglingContextDecl, |
478 | 6.80k | HasKnownInternalLinkage); |
479 | 6.80k | Class->setDeviceLambdaManglingNumber(MCtx->getDeviceManglingNumber(Method)); |
480 | 6.80k | } |
481 | 11.1k | } |
482 | | |
483 | | void Sema::buildLambdaScope(LambdaScopeInfo *LSI, |
484 | | CXXMethodDecl *CallOperator, |
485 | | SourceRange IntroducerRange, |
486 | | LambdaCaptureDefault CaptureDefault, |
487 | | SourceLocation CaptureDefaultLoc, |
488 | | bool ExplicitParams, |
489 | | bool ExplicitResultType, |
490 | 11.1k | bool Mutable) { |
491 | 11.1k | LSI->CallOperator = CallOperator; |
492 | 11.1k | CXXRecordDecl *LambdaClass = CallOperator->getParent(); |
493 | 11.1k | LSI->Lambda = LambdaClass; |
494 | 11.1k | if (CaptureDefault == LCD_ByCopy) |
495 | 1.30k | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByval; |
496 | 9.86k | else if (CaptureDefault == LCD_ByRef) |
497 | 2.22k | LSI->ImpCaptureStyle = LambdaScopeInfo::ImpCap_LambdaByref; |
498 | 11.1k | LSI->CaptureDefaultLoc = CaptureDefaultLoc; |
499 | 11.1k | LSI->IntroducerRange = IntroducerRange; |
500 | 11.1k | LSI->ExplicitParams = ExplicitParams; |
501 | 11.1k | LSI->Mutable = Mutable; |
502 | | |
503 | 11.1k | if (ExplicitResultType) { |
504 | 1.83k | LSI->ReturnType = CallOperator->getReturnType(); |
505 | | |
506 | 1.83k | if (!LSI->ReturnType->isDependentType() && |
507 | 1.83k | !LSI->ReturnType->isVoidType()1.35k ) { |
508 | 871 | if (RequireCompleteType(CallOperator->getBeginLoc(), LSI->ReturnType, |
509 | 871 | diag::err_lambda_incomplete_result)) { |
510 | | // Do nothing. |
511 | 2 | } |
512 | 871 | } |
513 | 9.33k | } else { |
514 | 9.33k | LSI->HasImplicitReturnType = true; |
515 | 9.33k | } |
516 | 11.1k | } |
517 | | |
518 | 11.1k | void Sema::finishLambdaExplicitCaptures(LambdaScopeInfo *LSI) { |
519 | 11.1k | LSI->finishedExplicitCaptures(); |
520 | 11.1k | } |
521 | | |
522 | | void Sema::ActOnLambdaExplicitTemplateParameterList(SourceLocation LAngleLoc, |
523 | | ArrayRef<NamedDecl *> TParams, |
524 | | SourceLocation RAngleLoc, |
525 | 172 | ExprResult RequiresClause) { |
526 | 172 | LambdaScopeInfo *LSI = getCurLambda(); |
527 | 172 | assert(LSI && "Expected a lambda scope"); |
528 | 0 | assert(LSI->NumExplicitTemplateParams == 0 && |
529 | 172 | "Already acted on explicit template parameters"); |
530 | 0 | assert(LSI->TemplateParams.empty() && |
531 | 172 | "Explicit template parameters should come " |
532 | 172 | "before invented (auto) ones"); |
533 | 0 | assert(!TParams.empty() && |
534 | 172 | "No template parameters to act on"); |
535 | 0 | LSI->TemplateParams.append(TParams.begin(), TParams.end()); |
536 | 172 | LSI->NumExplicitTemplateParams = TParams.size(); |
537 | 172 | LSI->ExplicitTemplateParamsRange = {LAngleLoc, RAngleLoc}; |
538 | 172 | LSI->RequiresClause = RequiresClause; |
539 | 172 | } |
540 | | |
541 | | void Sema::addLambdaParameters( |
542 | | ArrayRef<LambdaIntroducer::LambdaCapture> Captures, |
543 | 8.57k | CXXMethodDecl *CallOperator, Scope *CurScope) { |
544 | | // Introduce our parameters into the function scope |
545 | 8.57k | for (unsigned p = 0, NumParams = CallOperator->getNumParams(); |
546 | 13.0k | p < NumParams; ++p4.47k ) { |
547 | 4.47k | ParmVarDecl *Param = CallOperator->getParamDecl(p); |
548 | | |
549 | | // If this has an identifier, add it to the scope stack. |
550 | 4.47k | if (CurScope && Param->getIdentifier()) { |
551 | 4.19k | bool Error = false; |
552 | | // Resolution of CWG 2211 in C++17 renders shadowing ill-formed, but we |
553 | | // retroactively apply it. |
554 | 4.19k | for (const auto &Capture : Captures) { |
555 | 374 | if (Capture.Id == Param->getIdentifier()) { |
556 | 11 | Error = true; |
557 | 11 | Diag(Param->getLocation(), diag::err_parameter_shadow_capture); |
558 | 11 | Diag(Capture.Loc, diag::note_var_explicitly_captured_here) |
559 | 11 | << Capture.Id << true; |
560 | 11 | } |
561 | 374 | } |
562 | 4.19k | if (!Error) |
563 | 4.18k | CheckShadow(CurScope, Param); |
564 | | |
565 | 4.19k | PushOnScopeChains(Param, CurScope); |
566 | 4.19k | } |
567 | 4.47k | } |
568 | 8.57k | } |
569 | | |
570 | | /// If this expression is an enumerator-like expression of some type |
571 | | /// T, return the type T; otherwise, return null. |
572 | | /// |
573 | | /// Pointer comparisons on the result here should always work because |
574 | | /// it's derived from either the parent of an EnumConstantDecl |
575 | | /// (i.e. the definition) or the declaration returned by |
576 | | /// EnumType::getDecl() (i.e. the definition). |
577 | 531 | static EnumDecl *findEnumForBlockReturn(Expr *E) { |
578 | | // An expression is an enumerator-like expression of type T if, |
579 | | // ignoring parens and parens-like expressions: |
580 | 531 | E = E->IgnoreParens(); |
581 | | |
582 | | // - it is an enumerator whose enum type is T or |
583 | 531 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
584 | 28 | if (EnumConstantDecl *D |
585 | 28 | = dyn_cast<EnumConstantDecl>(DRE->getDecl())) { |
586 | 28 | return cast<EnumDecl>(D->getDeclContext()); |
587 | 28 | } |
588 | 0 | return nullptr; |
589 | 28 | } |
590 | | |
591 | | // - it is a comma expression whose RHS is an enumerator-like |
592 | | // expression of type T or |
593 | 503 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
594 | 26 | if (BO->getOpcode() == BO_Comma) |
595 | 1 | return findEnumForBlockReturn(BO->getRHS()); |
596 | 25 | return nullptr; |
597 | 26 | } |
598 | | |
599 | | // - it is a statement-expression whose value expression is an |
600 | | // enumerator-like expression of type T or |
601 | 477 | if (StmtExpr *SE = dyn_cast<StmtExpr>(E)) { |
602 | 1 | if (Expr *last = dyn_cast_or_null<Expr>(SE->getSubStmt()->body_back())) |
603 | 1 | return findEnumForBlockReturn(last); |
604 | 0 | return nullptr; |
605 | 1 | } |
606 | | |
607 | | // - it is a ternary conditional operator (not the GNU ?: |
608 | | // extension) whose second and third operands are |
609 | | // enumerator-like expressions of type T or |
610 | 476 | if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) { |
611 | 3 | if (EnumDecl *ED = findEnumForBlockReturn(CO->getTrueExpr())) |
612 | 3 | if (ED == findEnumForBlockReturn(CO->getFalseExpr())) |
613 | 3 | return ED; |
614 | 0 | return nullptr; |
615 | 3 | } |
616 | | |
617 | | // (implicitly:) |
618 | | // - it is an implicit integral conversion applied to an |
619 | | // enumerator-like expression of type T or |
620 | 473 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
621 | | // We can sometimes see integral conversions in valid |
622 | | // enumerator-like expressions. |
623 | 150 | if (ICE->getCastKind() == CK_IntegralCast) |
624 | 3 | return findEnumForBlockReturn(ICE->getSubExpr()); |
625 | | |
626 | | // Otherwise, just rely on the type. |
627 | 150 | } |
628 | | |
629 | | // - it is an expression of that formal enum type. |
630 | 470 | if (const EnumType *ET = E->getType()->getAs<EnumType>()) { |
631 | 15 | return ET->getDecl(); |
632 | 15 | } |
633 | | |
634 | | // Otherwise, nope. |
635 | 455 | return nullptr; |
636 | 470 | } |
637 | | |
638 | | /// Attempt to find a type T for which the returned expression of the |
639 | | /// given statement is an enumerator-like expression of that type. |
640 | 717 | static EnumDecl *findEnumForBlockReturn(ReturnStmt *ret) { |
641 | 717 | if (Expr *retValue = ret->getRetValue()) |
642 | 520 | return findEnumForBlockReturn(retValue); |
643 | 197 | return nullptr; |
644 | 717 | } |
645 | | |
646 | | /// Attempt to find a common type T for which all of the returned |
647 | | /// expressions in a block are enumerator-like expressions of that |
648 | | /// type. |
649 | 702 | static EnumDecl *findCommonEnumForBlockReturns(ArrayRef<ReturnStmt*> returns) { |
650 | 702 | ArrayRef<ReturnStmt*>::iterator i = returns.begin(), e = returns.end(); |
651 | | |
652 | | // Try to find one for the first return. |
653 | 702 | EnumDecl *ED = findEnumForBlockReturn(*i); |
654 | 702 | if (!ED) return nullptr675 ; |
655 | | |
656 | | // Check that the rest of the returns have the same enum. |
657 | 40 | for (++i; 27 i != e; ++i13 ) { |
658 | 15 | if (findEnumForBlockReturn(*i) != ED) |
659 | 2 | return nullptr; |
660 | 15 | } |
661 | | |
662 | | // Never infer an anonymous enum type. |
663 | 25 | if (!ED->hasNameForLinkage()) return nullptr3 ; |
664 | | |
665 | 22 | return ED; |
666 | 25 | } |
667 | | |
668 | | /// Adjust the given return statements so that they formally return |
669 | | /// the given type. It should require, at most, an IntegralCast. |
670 | | static void adjustBlockReturnsToEnum(Sema &S, ArrayRef<ReturnStmt*> returns, |
671 | 22 | QualType returnType) { |
672 | 22 | for (ArrayRef<ReturnStmt*>::iterator |
673 | 57 | i = returns.begin(), e = returns.end(); i != e; ++i35 ) { |
674 | 35 | ReturnStmt *ret = *i; |
675 | 35 | Expr *retValue = ret->getRetValue(); |
676 | 35 | if (S.Context.hasSameType(retValue->getType(), returnType)) |
677 | 14 | continue; |
678 | | |
679 | | // Right now we only support integral fixup casts. |
680 | 21 | assert(returnType->isIntegralOrUnscopedEnumerationType()); |
681 | 0 | assert(retValue->getType()->isIntegralOrUnscopedEnumerationType()); |
682 | | |
683 | 0 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(retValue); |
684 | | |
685 | 21 | Expr *E = (cleanups ? cleanups->getSubExpr()0 : retValue); |
686 | 21 | E = ImplicitCastExpr::Create(S.Context, returnType, CK_IntegralCast, E, |
687 | 21 | /*base path*/ nullptr, VK_PRValue, |
688 | 21 | FPOptionsOverride()); |
689 | 21 | if (cleanups) { |
690 | 0 | cleanups->setSubExpr(E); |
691 | 21 | } else { |
692 | 21 | ret->setRetValue(E); |
693 | 21 | } |
694 | 21 | } |
695 | 22 | } |
696 | | |
697 | 5.60k | void Sema::deduceClosureReturnType(CapturingScopeInfo &CSI) { |
698 | 5.60k | assert(CSI.HasImplicitReturnType); |
699 | | // If it was ever a placeholder, it had to been deduced to DependentTy. |
700 | 0 | assert(CSI.ReturnType.isNull() || !CSI.ReturnType->isUndeducedType()); |
701 | 0 | assert((!isa<LambdaScopeInfo>(CSI) || !getLangOpts().CPlusPlus14) && |
702 | 5.60k | "lambda expressions use auto deduction in C++14 onwards"); |
703 | | |
704 | | // C++ core issue 975: |
705 | | // If a lambda-expression does not include a trailing-return-type, |
706 | | // it is as if the trailing-return-type denotes the following type: |
707 | | // - if there are no return statements in the compound-statement, |
708 | | // or all return statements return either an expression of type |
709 | | // void or no expression or braced-init-list, the type void; |
710 | | // - otherwise, if all return statements return an expression |
711 | | // and the types of the returned expressions after |
712 | | // lvalue-to-rvalue conversion (4.1 [conv.lval]), |
713 | | // array-to-pointer conversion (4.2 [conv.array]), and |
714 | | // function-to-pointer conversion (4.3 [conv.func]) are the |
715 | | // same, that common type; |
716 | | // - otherwise, the program is ill-formed. |
717 | | // |
718 | | // C++ core issue 1048 additionally removes top-level cv-qualifiers |
719 | | // from the types of returned expressions to match the C++14 auto |
720 | | // deduction rules. |
721 | | // |
722 | | // In addition, in blocks in non-C++ modes, if all of the return |
723 | | // statements are enumerator-like expressions of some type T, where |
724 | | // T has a name for linkage, then we infer the return type of the |
725 | | // block to be that type. |
726 | | |
727 | | // First case: no return statements, implicit void return type. |
728 | 0 | ASTContext &Ctx = getASTContext(); |
729 | 5.60k | if (CSI.Returns.empty()) { |
730 | | // It's possible there were simply no /valid/ return statements. |
731 | | // In this case, the first one we found may have at least given us a type. |
732 | 4.29k | if (CSI.ReturnType.isNull()) |
733 | 4.29k | CSI.ReturnType = Ctx.VoidTy; |
734 | 4.29k | return; |
735 | 4.29k | } |
736 | | |
737 | | // Second case: at least one return statement has dependent type. |
738 | | // Delay type checking until instantiation. |
739 | 1.31k | assert(!CSI.ReturnType.isNull() && "We should have a tentative return type."); |
740 | 1.31k | if (CSI.ReturnType->isDependentType()) |
741 | 72 | return; |
742 | | |
743 | | // Try to apply the enum-fuzz rule. |
744 | 1.24k | if (!getLangOpts().CPlusPlus) { |
745 | 702 | assert(isa<BlockScopeInfo>(CSI)); |
746 | 0 | const EnumDecl *ED = findCommonEnumForBlockReturns(CSI.Returns); |
747 | 702 | if (ED) { |
748 | 22 | CSI.ReturnType = Context.getTypeDeclType(ED); |
749 | 22 | adjustBlockReturnsToEnum(*this, CSI.Returns, CSI.ReturnType); |
750 | 22 | return; |
751 | 22 | } |
752 | 702 | } |
753 | | |
754 | | // Third case: only one return statement. Don't bother doing extra work! |
755 | 1.22k | if (CSI.Returns.size() == 1) |
756 | 1.17k | return; |
757 | | |
758 | | // General case: many return statements. |
759 | | // Check that they all have compatible return types. |
760 | | |
761 | | // We require the return types to strictly match here. |
762 | | // Note that we've already done the required promotions as part of |
763 | | // processing the return statement. |
764 | 120 | for (const ReturnStmt *RS : CSI.Returns)46 { |
765 | 120 | const Expr *RetE = RS->getRetValue(); |
766 | | |
767 | 120 | QualType ReturnType = |
768 | 120 | (RetE ? RetE->getType()115 : Context.VoidTy5 ).getUnqualifiedType(); |
769 | 120 | if (Context.getCanonicalFunctionResultType(ReturnType) == |
770 | 120 | Context.getCanonicalFunctionResultType(CSI.ReturnType)) { |
771 | | // Use the return type with the strictest possible nullability annotation. |
772 | 111 | auto RetTyNullability = ReturnType->getNullability(Ctx); |
773 | 111 | auto BlockNullability = CSI.ReturnType->getNullability(Ctx); |
774 | 111 | if (BlockNullability && |
775 | 111 | (4 !RetTyNullability4 || |
776 | 4 | hasWeakerNullability(*RetTyNullability, *BlockNullability)2 )) |
777 | 2 | CSI.ReturnType = ReturnType; |
778 | 111 | continue; |
779 | 111 | } |
780 | | |
781 | | // FIXME: This is a poor diagnostic for ReturnStmts without expressions. |
782 | | // TODO: It's possible that the *first* return is the divergent one. |
783 | 9 | Diag(RS->getBeginLoc(), |
784 | 9 | diag::err_typecheck_missing_return_type_incompatible) |
785 | 9 | << ReturnType << CSI.ReturnType << isa<LambdaScopeInfo>(CSI); |
786 | | // Continue iterating so that we keep emitting diagnostics. |
787 | 9 | } |
788 | 46 | } |
789 | | |
790 | | QualType Sema::buildLambdaInitCaptureInitialization( |
791 | | SourceLocation Loc, bool ByRef, SourceLocation EllipsisLoc, |
792 | | Optional<unsigned> NumExpansions, IdentifierInfo *Id, bool IsDirectInit, |
793 | 582 | Expr *&Init) { |
794 | | // Create an 'auto' or 'auto&' TypeSourceInfo that we can use to |
795 | | // deduce against. |
796 | 582 | QualType DeductType = Context.getAutoDeductType(); |
797 | 582 | TypeLocBuilder TLB; |
798 | 582 | AutoTypeLoc TL = TLB.push<AutoTypeLoc>(DeductType); |
799 | 582 | TL.setNameLoc(Loc); |
800 | 582 | if (ByRef) { |
801 | 115 | DeductType = BuildReferenceType(DeductType, true, Loc, Id); |
802 | 115 | assert(!DeductType.isNull() && "can't build reference to auto"); |
803 | 0 | TLB.push<ReferenceTypeLoc>(DeductType).setSigilLoc(Loc); |
804 | 115 | } |
805 | 582 | if (EllipsisLoc.isValid()) { |
806 | 64 | if (Init->containsUnexpandedParameterPack()) { |
807 | 63 | Diag(EllipsisLoc, getLangOpts().CPlusPlus20 |
808 | 63 | ? diag::warn_cxx17_compat_init_capture_pack32 |
809 | 63 | : diag::ext_init_capture_pack31 ); |
810 | 63 | DeductType = Context.getPackExpansionType(DeductType, NumExpansions, |
811 | 63 | /*ExpectPackInType=*/false); |
812 | 63 | TLB.push<PackExpansionTypeLoc>(DeductType).setEllipsisLoc(EllipsisLoc); |
813 | 63 | } else { |
814 | | // Just ignore the ellipsis for now and form a non-pack variable. We'll |
815 | | // diagnose this later when we try to capture it. |
816 | 1 | } |
817 | 64 | } |
818 | 582 | TypeSourceInfo *TSI = TLB.getTypeSourceInfo(Context, DeductType); |
819 | | |
820 | | // Deduce the type of the init capture. |
821 | 582 | QualType DeducedType = deduceVarTypeFromInitializer( |
822 | 582 | /*VarDecl*/nullptr, DeclarationName(Id), DeductType, TSI, |
823 | 582 | SourceRange(Loc, Loc), IsDirectInit, Init); |
824 | 582 | if (DeducedType.isNull()) |
825 | 37 | return QualType(); |
826 | | |
827 | | // Are we a non-list direct initialization? |
828 | 545 | ParenListExpr *CXXDirectInit = dyn_cast<ParenListExpr>(Init); |
829 | | |
830 | | // Perform initialization analysis and ensure any implicit conversions |
831 | | // (such as lvalue-to-rvalue) are enforced. |
832 | 545 | InitializedEntity Entity = |
833 | 545 | InitializedEntity::InitializeLambdaCapture(Id, DeducedType, Loc); |
834 | 545 | InitializationKind Kind = |
835 | 545 | IsDirectInit |
836 | 545 | ? (160 CXXDirectInit160 ? InitializationKind::CreateDirect( |
837 | 140 | Loc, Init->getBeginLoc(), Init->getEndLoc()) |
838 | 160 | : InitializationKind::CreateDirectList(Loc)20 ) |
839 | 545 | : InitializationKind::CreateCopy(Loc, Init->getBeginLoc())385 ; |
840 | | |
841 | 545 | MultiExprArg Args = Init; |
842 | 545 | if (CXXDirectInit) |
843 | 140 | Args = |
844 | 140 | MultiExprArg(CXXDirectInit->getExprs(), CXXDirectInit->getNumExprs()); |
845 | 545 | QualType DclT; |
846 | 545 | InitializationSequence InitSeq(*this, Entity, Kind, Args); |
847 | 545 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Args, &DclT); |
848 | | |
849 | 545 | if (Result.isInvalid()) |
850 | 4 | return QualType(); |
851 | | |
852 | 541 | Init = Result.getAs<Expr>(); |
853 | 541 | return DeducedType; |
854 | 545 | } |
855 | | |
856 | | VarDecl *Sema::createLambdaInitCaptureVarDecl(SourceLocation Loc, |
857 | | QualType InitCaptureType, |
858 | | SourceLocation EllipsisLoc, |
859 | | IdentifierInfo *Id, |
860 | 539 | unsigned InitStyle, Expr *Init) { |
861 | | // FIXME: Retain the TypeSourceInfo from buildLambdaInitCaptureInitialization |
862 | | // rather than reconstructing it here. |
863 | 539 | TypeSourceInfo *TSI = Context.getTrivialTypeSourceInfo(InitCaptureType, Loc); |
864 | 539 | if (auto PETL = TSI->getTypeLoc().getAs<PackExpansionTypeLoc>()) |
865 | 61 | PETL.setEllipsisLoc(EllipsisLoc); |
866 | | |
867 | | // Create a dummy variable representing the init-capture. This is not actually |
868 | | // used as a variable, and only exists as a way to name and refer to the |
869 | | // init-capture. |
870 | | // FIXME: Pass in separate source locations for '&' and identifier. |
871 | 539 | VarDecl *NewVD = VarDecl::Create(Context, CurContext, Loc, |
872 | 539 | Loc, Id, InitCaptureType, TSI, SC_Auto); |
873 | 539 | NewVD->setInitCapture(true); |
874 | 539 | NewVD->setReferenced(true); |
875 | | // FIXME: Pass in a VarDecl::InitializationStyle. |
876 | 539 | NewVD->setInitStyle(static_cast<VarDecl::InitializationStyle>(InitStyle)); |
877 | 539 | NewVD->markUsed(Context); |
878 | 539 | NewVD->setInit(Init); |
879 | 539 | if (NewVD->isParameterPack()) |
880 | 61 | getCurLambda()->LocalPacks.push_back(NewVD); |
881 | 539 | return NewVD; |
882 | 539 | } |
883 | | |
884 | 537 | void Sema::addInitCapture(LambdaScopeInfo *LSI, VarDecl *Var) { |
885 | 537 | assert(Var->isInitCapture() && "init capture flag should be set"); |
886 | 0 | LSI->addCapture(Var, /*isBlock*/false, Var->getType()->isReferenceType(), |
887 | 537 | /*isNested*/false, Var->getLocation(), SourceLocation(), |
888 | 537 | Var->getType(), /*Invalid*/false); |
889 | 537 | } |
890 | | |
891 | | void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, |
892 | | Declarator &ParamInfo, |
893 | 8.57k | Scope *CurScope) { |
894 | 8.57k | LambdaScopeInfo *const LSI = getCurLambda(); |
895 | 8.57k | assert(LSI && "LambdaScopeInfo should be on stack!"); |
896 | | |
897 | | // Determine if we're within a context where we know that the lambda will |
898 | | // be dependent, because there are template parameters in scope. |
899 | 0 | CXXRecordDecl::LambdaDependencyKind LambdaDependencyKind = |
900 | 8.57k | CXXRecordDecl::LDK_Unknown; |
901 | 8.57k | if (LSI->NumExplicitTemplateParams > 0) { |
902 | 172 | auto *TemplateParamScope = CurScope->getTemplateParamParent(); |
903 | 172 | assert(TemplateParamScope && |
904 | 172 | "Lambda with explicit template param list should establish a " |
905 | 172 | "template param scope"); |
906 | 0 | assert(TemplateParamScope->getParent()); |
907 | 172 | if (TemplateParamScope->getParent()->getTemplateParamParent() != nullptr) |
908 | 16 | LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent; |
909 | 8.40k | } else if (CurScope->getTemplateParamParent() != nullptr) { |
910 | 1.69k | LambdaDependencyKind = CXXRecordDecl::LDK_AlwaysDependent; |
911 | 1.69k | } |
912 | | |
913 | | // Determine the signature of the call operator. |
914 | 0 | TypeSourceInfo *MethodTyInfo; |
915 | 8.57k | bool ExplicitParams = true; |
916 | 8.57k | bool ExplicitResultType = true; |
917 | 8.57k | bool ContainsUnexpandedParameterPack = false; |
918 | 8.57k | SourceLocation EndLoc; |
919 | 8.57k | SmallVector<ParmVarDecl *, 8> Params; |
920 | 8.57k | if (ParamInfo.getNumTypeObjects() == 0) { |
921 | | // C++11 [expr.prim.lambda]p4: |
922 | | // If a lambda-expression does not include a lambda-declarator, it is as |
923 | | // if the lambda-declarator were (). |
924 | 1.82k | FunctionProtoType::ExtProtoInfo EPI(Context.getDefaultCallingConvention( |
925 | 1.82k | /*IsVariadic=*/false, /*IsCXXMethod=*/true)); |
926 | 1.82k | EPI.HasTrailingReturn = true; |
927 | 1.82k | EPI.TypeQuals.addConst(); |
928 | 1.82k | LangAS AS = getDefaultCXXMethodAddrSpace(); |
929 | 1.82k | if (AS != LangAS::Default) |
930 | 2 | EPI.TypeQuals.addAddressSpace(AS); |
931 | | |
932 | | // C++1y [expr.prim.lambda]: |
933 | | // The lambda return type is 'auto', which is replaced by the |
934 | | // trailing-return type if provided and/or deduced from 'return' |
935 | | // statements |
936 | | // We don't do this before C++1y, because we don't support deduced return |
937 | | // types there. |
938 | 1.82k | QualType DefaultTypeForNoTrailingReturn = |
939 | 1.82k | getLangOpts().CPlusPlus14 ? Context.getAutoDeductType()1.22k |
940 | 1.82k | : Context.DependentTy594 ; |
941 | 1.82k | QualType MethodTy = |
942 | 1.82k | Context.getFunctionType(DefaultTypeForNoTrailingReturn, None, EPI); |
943 | 1.82k | MethodTyInfo = Context.getTrivialTypeSourceInfo(MethodTy); |
944 | 1.82k | ExplicitParams = false; |
945 | 1.82k | ExplicitResultType = false; |
946 | 1.82k | EndLoc = Intro.Range.getEnd(); |
947 | 6.75k | } else { |
948 | 6.75k | assert(ParamInfo.isFunctionDeclarator() && |
949 | 6.75k | "lambda-declarator is a function"); |
950 | 0 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getFunctionTypeInfo(); |
951 | | |
952 | | // C++11 [expr.prim.lambda]p5: |
953 | | // This function call operator is declared const (9.3.1) if and only if |
954 | | // the lambda-expression's parameter-declaration-clause is not followed |
955 | | // by mutable. It is neither virtual nor declared volatile. [...] |
956 | 6.75k | if (!FTI.hasMutableQualifier()) { |
957 | 6.56k | FTI.getOrCreateMethodQualifiers().SetTypeQual(DeclSpec::TQ_const, |
958 | 6.56k | SourceLocation()); |
959 | 6.56k | } |
960 | | |
961 | 6.75k | MethodTyInfo = GetTypeForDeclarator(ParamInfo, CurScope); |
962 | 6.75k | assert(MethodTyInfo && "no type from lambda-declarator"); |
963 | 0 | EndLoc = ParamInfo.getSourceRange().getEnd(); |
964 | | |
965 | 6.75k | ExplicitResultType = FTI.hasTrailingReturnType(); |
966 | | |
967 | 6.75k | if (FTIHasNonVoidParameters(FTI)) { |
968 | 3.81k | Params.reserve(FTI.NumParams); |
969 | 8.28k | for (unsigned i = 0, e = FTI.NumParams; i != e; ++i4.47k ) |
970 | 4.47k | Params.push_back(cast<ParmVarDecl>(FTI.Params[i].Param)); |
971 | 3.81k | } |
972 | | |
973 | | // Check for unexpanded parameter packs in the method type. |
974 | 6.75k | if (MethodTyInfo->getType()->containsUnexpandedParameterPack()) |
975 | 50 | DiagnoseUnexpandedParameterPack(Intro.Range.getBegin(), MethodTyInfo, |
976 | 50 | UPPC_DeclarationType); |
977 | 6.75k | } |
978 | | |
979 | 0 | CXXRecordDecl *Class = createLambdaClosureType( |
980 | 8.57k | Intro.Range, MethodTyInfo, LambdaDependencyKind, Intro.Default); |
981 | 8.57k | CXXMethodDecl *Method = |
982 | 8.57k | startLambdaDefinition(Class, Intro.Range, MethodTyInfo, EndLoc, Params, |
983 | 8.57k | ParamInfo.getDeclSpec().getConstexprSpecifier(), |
984 | 8.57k | ParamInfo.getTrailingRequiresClause()); |
985 | 8.57k | if (ExplicitParams) |
986 | 6.75k | CheckCXXDefaultArguments(Method); |
987 | | |
988 | | // This represents the function body for the lambda function, check if we |
989 | | // have to apply optnone due to a pragma. |
990 | 8.57k | AddRangeBasedOptnone(Method); |
991 | | |
992 | | // code_seg attribute on lambda apply to the method. |
993 | 8.57k | if (Attr *A = getImplicitCodeSegOrSectionAttrForFunction(Method, /*IsDefinition=*/true)) |
994 | 2 | Method->addAttr(A); |
995 | | |
996 | | // Attributes on the lambda apply to the method. |
997 | 8.57k | ProcessDeclAttributes(CurScope, Method, ParamInfo); |
998 | | |
999 | | // CUDA lambdas get implicit host and device attributes. |
1000 | 8.57k | if (getLangOpts().CUDA) |
1001 | 227 | CUDASetLambdaAttrs(Method); |
1002 | | |
1003 | | // OpenMP lambdas might get assumumption attributes. |
1004 | 8.57k | if (LangOpts.OpenMP) |
1005 | 1.40k | ActOnFinishedFunctionDefinitionInOpenMPAssumeScope(Method); |
1006 | | |
1007 | | // Number the lambda for linkage purposes if necessary. |
1008 | 8.57k | handleLambdaNumbering(Class, Method); |
1009 | | |
1010 | | // Introduce the function call operator as the current declaration context. |
1011 | 8.57k | PushDeclContext(CurScope, Method); |
1012 | | |
1013 | | // Build the lambda scope. |
1014 | 8.57k | buildLambdaScope(LSI, Method, Intro.Range, Intro.Default, Intro.DefaultLoc, |
1015 | 8.57k | ExplicitParams, ExplicitResultType, !Method->isConst()); |
1016 | | |
1017 | | // C++11 [expr.prim.lambda]p9: |
1018 | | // A lambda-expression whose smallest enclosing scope is a block scope is a |
1019 | | // local lambda expression; any other lambda expression shall not have a |
1020 | | // capture-default or simple-capture in its lambda-introducer. |
1021 | | // |
1022 | | // For simple-captures, this is covered by the check below that any named |
1023 | | // entity is a variable that can be captured. |
1024 | | // |
1025 | | // For DR1632, we also allow a capture-default in any context where we can |
1026 | | // odr-use 'this' (in particular, in a default initializer for a non-static |
1027 | | // data member). |
1028 | 8.57k | if (Intro.Default != LCD_None && !Class->getParent()->isFunctionOrMethod()2.97k && |
1029 | 8.57k | (37 getCurrentThisType().isNull()37 || |
1030 | 37 | CheckCXXThisCapture(SourceLocation(), /*Explicit*/true, |
1031 | 29 | /*BuildAndDiagnose*/false))) |
1032 | 8 | Diag(Intro.DefaultLoc, diag::err_capture_default_non_local); |
1033 | | |
1034 | | // Distinct capture names, for diagnostics. |
1035 | 8.57k | llvm::SmallSet<IdentifierInfo*, 8> CaptureNames; |
1036 | | |
1037 | | // Handle explicit captures. |
1038 | 8.57k | SourceLocation PrevCaptureLoc |
1039 | 8.57k | = Intro.Default == LCD_None? Intro.Range.getBegin()5.60k : Intro.DefaultLoc2.97k ; |
1040 | 10.3k | for (auto C = Intro.Captures.begin(), E = Intro.Captures.end(); C != E; |
1041 | 8.57k | PrevCaptureLoc = C->Loc, ++C1.73k ) { |
1042 | 1.73k | if (C->Kind == LCK_This || C->Kind == LCK_StarThis1.45k ) { |
1043 | 396 | if (C->Kind == LCK_StarThis) |
1044 | 121 | Diag(C->Loc, !getLangOpts().CPlusPlus17 |
1045 | 121 | ? diag::ext_star_this_lambda_capture_cxx172 |
1046 | 121 | : diag::warn_cxx14_compat_star_this_lambda_capture119 ); |
1047 | | |
1048 | | // C++11 [expr.prim.lambda]p8: |
1049 | | // An identifier or this shall not appear more than once in a |
1050 | | // lambda-capture. |
1051 | 396 | if (LSI->isCXXThisCaptured()) { |
1052 | 5 | Diag(C->Loc, diag::err_capture_more_than_once) |
1053 | 5 | << "'this'" << SourceRange(LSI->getCXXThisCapture().getLocation()) |
1054 | 5 | << FixItHint::CreateRemoval( |
1055 | 5 | SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
1056 | 5 | continue; |
1057 | 5 | } |
1058 | | |
1059 | | // C++2a [expr.prim.lambda]p8: |
1060 | | // If a lambda-capture includes a capture-default that is =, |
1061 | | // each simple-capture of that lambda-capture shall be of the form |
1062 | | // "&identifier", "this", or "* this". [ Note: The form [&,this] is |
1063 | | // redundant but accepted for compatibility with ISO C++14. --end note ] |
1064 | 391 | if (Intro.Default == LCD_ByCopy && C->Kind != LCK_StarThis9 ) |
1065 | 5 | Diag(C->Loc, !getLangOpts().CPlusPlus20 |
1066 | 5 | ? diag::ext_equals_this_lambda_capture_cxx203 |
1067 | 5 | : diag::warn_cxx17_compat_equals_this_lambda_capture2 ); |
1068 | | |
1069 | | // C++11 [expr.prim.lambda]p12: |
1070 | | // If this is captured by a local lambda expression, its nearest |
1071 | | // enclosing function shall be a non-static member function. |
1072 | 391 | QualType ThisCaptureType = getCurrentThisType(); |
1073 | 391 | if (ThisCaptureType.isNull()) { |
1074 | 5 | Diag(C->Loc, diag::err_this_capture) << true; |
1075 | 5 | continue; |
1076 | 5 | } |
1077 | | |
1078 | 386 | CheckCXXThisCapture(C->Loc, /*Explicit=*/true, /*BuildAndDiagnose*/ true, |
1079 | 386 | /*FunctionScopeIndexToStopAtPtr*/ nullptr, |
1080 | 386 | C->Kind == LCK_StarThis); |
1081 | 386 | if (!LSI->Captures.empty()) |
1082 | 384 | LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange; |
1083 | 386 | continue; |
1084 | 391 | } |
1085 | | |
1086 | 1.33k | assert(C->Id && "missing identifier for capture"); |
1087 | | |
1088 | 1.33k | if (C->Init.isInvalid()) |
1089 | 7 | continue; |
1090 | | |
1091 | 1.32k | VarDecl *Var = nullptr; |
1092 | 1.32k | if (C->Init.isUsable()) { |
1093 | 447 | Diag(C->Loc, getLangOpts().CPlusPlus14 |
1094 | 447 | ? diag::warn_cxx11_compat_init_capture396 |
1095 | 447 | : diag::ext_init_capture51 ); |
1096 | | |
1097 | | // If the initializer expression is usable, but the InitCaptureType |
1098 | | // is not, then an error has occurred - so ignore the capture for now. |
1099 | | // for e.g., [n{0}] { }; <-- if no <initializer_list> is included. |
1100 | | // FIXME: we should create the init capture variable and mark it invalid |
1101 | | // in this case. |
1102 | 447 | if (C->InitCaptureType.get().isNull()) |
1103 | 33 | continue; |
1104 | | |
1105 | 414 | if (C->Init.get()->containsUnexpandedParameterPack() && |
1106 | 414 | !C->InitCaptureType.get()->getAs<PackExpansionType>()77 ) |
1107 | 16 | DiagnoseUnexpandedParameterPack(C->Init.get(), UPPC_Initializer); |
1108 | | |
1109 | 414 | unsigned InitStyle; |
1110 | 414 | switch (C->InitKind) { |
1111 | 0 | case LambdaCaptureInitKind::NoInit: |
1112 | 0 | llvm_unreachable("not an init-capture?"); |
1113 | 301 | case LambdaCaptureInitKind::CopyInit: |
1114 | 301 | InitStyle = VarDecl::CInit; |
1115 | 301 | break; |
1116 | 94 | case LambdaCaptureInitKind::DirectInit: |
1117 | 94 | InitStyle = VarDecl::CallInit; |
1118 | 94 | break; |
1119 | 19 | case LambdaCaptureInitKind::ListInit: |
1120 | 19 | InitStyle = VarDecl::ListInit; |
1121 | 19 | break; |
1122 | 414 | } |
1123 | 414 | Var = createLambdaInitCaptureVarDecl(C->Loc, C->InitCaptureType.get(), |
1124 | 414 | C->EllipsisLoc, C->Id, InitStyle, |
1125 | 414 | C->Init.get()); |
1126 | | // C++1y [expr.prim.lambda]p11: |
1127 | | // An init-capture behaves as if it declares and explicitly |
1128 | | // captures a variable [...] whose declarative region is the |
1129 | | // lambda-expression's compound-statement |
1130 | 414 | if (Var) |
1131 | 414 | PushOnScopeChains(Var, CurScope, false); |
1132 | 880 | } else { |
1133 | 880 | assert(C->InitKind == LambdaCaptureInitKind::NoInit && |
1134 | 880 | "init capture has valid but null init?"); |
1135 | | |
1136 | | // C++11 [expr.prim.lambda]p8: |
1137 | | // If a lambda-capture includes a capture-default that is &, the |
1138 | | // identifiers in the lambda-capture shall not be preceded by &. |
1139 | | // If a lambda-capture includes a capture-default that is =, [...] |
1140 | | // each identifier it contains shall be preceded by &. |
1141 | 880 | if (C->Kind == LCK_ByRef && Intro.Default == LCD_ByRef249 ) { |
1142 | 7 | Diag(C->Loc, diag::err_reference_capture_with_reference_default) |
1143 | 7 | << FixItHint::CreateRemoval( |
1144 | 7 | SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
1145 | 7 | continue; |
1146 | 873 | } else if (C->Kind == LCK_ByCopy && Intro.Default == LCD_ByCopy631 ) { |
1147 | 1 | Diag(C->Loc, diag::err_copy_capture_with_copy_default) |
1148 | 1 | << FixItHint::CreateRemoval( |
1149 | 1 | SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
1150 | 1 | continue; |
1151 | 1 | } |
1152 | | |
1153 | | // C++11 [expr.prim.lambda]p10: |
1154 | | // The identifiers in a capture-list are looked up using the usual |
1155 | | // rules for unqualified name lookup (3.4.1) |
1156 | 872 | DeclarationNameInfo Name(C->Id, C->Loc); |
1157 | 872 | LookupResult R(*this, Name, LookupOrdinaryName); |
1158 | 872 | LookupName(R, CurScope); |
1159 | 872 | if (R.isAmbiguous()) |
1160 | 1 | continue; |
1161 | 871 | if (R.empty()) { |
1162 | | // FIXME: Disable corrections that would add qualification? |
1163 | 1 | CXXScopeSpec ScopeSpec; |
1164 | 1 | DeclFilterCCC<VarDecl> Validator{}; |
1165 | 1 | if (DiagnoseEmptyLookup(CurScope, ScopeSpec, R, Validator)) |
1166 | 0 | continue; |
1167 | 1 | } |
1168 | | |
1169 | 871 | Var = R.getAsSingle<VarDecl>(); |
1170 | 871 | if (Var && DiagnoseUseOfDecl(Var, C->Loc)857 ) |
1171 | 2 | continue; |
1172 | 871 | } |
1173 | | |
1174 | | // C++11 [expr.prim.lambda]p8: |
1175 | | // An identifier or this shall not appear more than once in a |
1176 | | // lambda-capture. |
1177 | 1.28k | if (!CaptureNames.insert(C->Id).second) { |
1178 | 9 | if (Var && LSI->isCaptured(Var)) { |
1179 | 7 | Diag(C->Loc, diag::err_capture_more_than_once) |
1180 | 7 | << C->Id << SourceRange(LSI->getCapture(Var).getLocation()) |
1181 | 7 | << FixItHint::CreateRemoval( |
1182 | 7 | SourceRange(getLocForEndOfToken(PrevCaptureLoc), C->Loc)); |
1183 | 7 | } else |
1184 | | // Previous capture captured something different (one or both was |
1185 | | // an init-cpature): no fixit. |
1186 | 2 | Diag(C->Loc, diag::err_capture_more_than_once) << C->Id; |
1187 | 9 | continue; |
1188 | 9 | } |
1189 | | |
1190 | | // C++11 [expr.prim.lambda]p10: |
1191 | | // [...] each such lookup shall find a variable with automatic storage |
1192 | | // duration declared in the reaching scope of the local lambda expression. |
1193 | | // Note that the 'reaching scope' check happens in tryCaptureVariable(). |
1194 | 1.27k | if (!Var) { |
1195 | 14 | Diag(C->Loc, diag::err_capture_does_not_name_variable) << C->Id; |
1196 | 14 | continue; |
1197 | 14 | } |
1198 | | |
1199 | | // Ignore invalid decls; they'll just confuse the code later. |
1200 | 1.26k | if (Var->isInvalidDecl()) |
1201 | 4 | continue; |
1202 | | |
1203 | 1.25k | if (!Var->hasLocalStorage()) { |
1204 | 2 | Diag(C->Loc, diag::err_capture_non_automatic_variable) << C->Id; |
1205 | 2 | Diag(Var->getLocation(), diag::note_previous_decl) << C->Id; |
1206 | 2 | continue; |
1207 | 2 | } |
1208 | | |
1209 | | // C++11 [expr.prim.lambda]p23: |
1210 | | // A capture followed by an ellipsis is a pack expansion (14.5.3). |
1211 | 1.25k | SourceLocation EllipsisLoc; |
1212 | 1.25k | if (C->EllipsisLoc.isValid()) { |
1213 | 142 | if (Var->isParameterPack()) { |
1214 | 138 | EllipsisLoc = C->EllipsisLoc; |
1215 | 138 | } else { |
1216 | 4 | Diag(C->EllipsisLoc, diag::err_pack_expansion_without_parameter_packs) |
1217 | 4 | << (C->Init.isUsable() ? C->Init.get()->getSourceRange()1 |
1218 | 4 | : SourceRange(C->Loc)3 ); |
1219 | | |
1220 | | // Just ignore the ellipsis. |
1221 | 4 | } |
1222 | 1.11k | } else if (Var->isParameterPack()) { |
1223 | 13 | ContainsUnexpandedParameterPack = true; |
1224 | 13 | } |
1225 | | |
1226 | 1.25k | if (C->Init.isUsable()) { |
1227 | 412 | addInitCapture(LSI, Var); |
1228 | 842 | } else { |
1229 | 842 | TryCaptureKind Kind = C->Kind == LCK_ByRef ? TryCapture_ExplicitByRef237 : |
1230 | 842 | TryCapture_ExplicitByVal605 ; |
1231 | 842 | tryCaptureVariable(Var, C->Loc, Kind, EllipsisLoc); |
1232 | 842 | } |
1233 | 1.25k | if (!LSI->Captures.empty()) |
1234 | 1.24k | LSI->ExplicitCaptureRanges[LSI->Captures.size() - 1] = C->ExplicitRange; |
1235 | 1.25k | } |
1236 | 8.57k | finishLambdaExplicitCaptures(LSI); |
1237 | | |
1238 | 8.57k | LSI->ContainsUnexpandedParameterPack |= ContainsUnexpandedParameterPack; |
1239 | | |
1240 | | // Add lambda parameters into scope. |
1241 | 8.57k | addLambdaParameters(Intro.Captures, Method, CurScope); |
1242 | | |
1243 | | // Enter a new evaluation context to insulate the lambda from any |
1244 | | // cleanups from the enclosing full-expression. |
1245 | 8.57k | PushExpressionEvaluationContext( |
1246 | 8.57k | LSI->CallOperator->isConsteval() |
1247 | 8.57k | ? ExpressionEvaluationContext::ImmediateFunctionContext6 |
1248 | 8.57k | : ExpressionEvaluationContext::PotentiallyEvaluated8.56k ); |
1249 | 8.57k | } |
1250 | | |
1251 | | void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope, |
1252 | 210 | bool IsInstantiation) { |
1253 | 210 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(FunctionScopes.back()); |
1254 | | |
1255 | | // Leave the expression-evaluation context. |
1256 | 210 | DiscardCleanupsInEvaluationContext(); |
1257 | 210 | PopExpressionEvaluationContext(); |
1258 | | |
1259 | | // Leave the context of the lambda. |
1260 | 210 | if (!IsInstantiation) |
1261 | 189 | PopDeclContext(); |
1262 | | |
1263 | | // Finalize the lambda. |
1264 | 210 | CXXRecordDecl *Class = LSI->Lambda; |
1265 | 210 | Class->setInvalidDecl(); |
1266 | 210 | SmallVector<Decl*, 4> Fields(Class->fields()); |
1267 | 210 | ActOnFields(nullptr, Class->getLocation(), Class, Fields, SourceLocation(), |
1268 | 210 | SourceLocation(), ParsedAttributesView()); |
1269 | 210 | CheckCompletedCXXClass(nullptr, Class); |
1270 | | |
1271 | 210 | PopFunctionScopeInfo(); |
1272 | 210 | } |
1273 | | |
1274 | | template <typename Func> |
1275 | | static void repeatForLambdaConversionFunctionCallingConvs( |
1276 | 5.85k | Sema &S, const FunctionProtoType &CallOpProto, Func F) { |
1277 | 5.85k | CallingConv DefaultFree = S.Context.getDefaultCallingConvention( |
1278 | 5.85k | CallOpProto.isVariadic(), /*IsCXXMethod=*/false); |
1279 | 5.85k | CallingConv DefaultMember = S.Context.getDefaultCallingConvention( |
1280 | 5.85k | CallOpProto.isVariadic(), /*IsCXXMethod=*/true); |
1281 | 5.85k | CallingConv CallOpCC = CallOpProto.getCallConv(); |
1282 | | |
1283 | | /// Implement emitting a version of the operator for many of the calling |
1284 | | /// conventions for MSVC, as described here: |
1285 | | /// https://devblogs.microsoft.com/oldnewthing/20150220-00/?p=44623. |
1286 | | /// Experimentally, we determined that cdecl, stdcall, fastcall, and |
1287 | | /// vectorcall are generated by MSVC when it is supported by the target. |
1288 | | /// Additionally, we are ensuring that the default-free/default-member and |
1289 | | /// call-operator calling convention are generated as well. |
1290 | | /// NOTE: We intentionally generate a 'thiscall' on Win32 implicitly from the |
1291 | | /// 'member default', despite MSVC not doing so. We do this in order to ensure |
1292 | | /// that someone who intentionally places 'thiscall' on the lambda call |
1293 | | /// operator will still get that overload, since we don't have the a way of |
1294 | | /// detecting the attribute by the time we get here. |
1295 | 5.85k | if (S.getLangOpts().MSVCCompat) { |
1296 | 82 | CallingConv Convs[] = { |
1297 | 82 | CC_C, CC_X86StdCall, CC_X86FastCall, CC_X86VectorCall, |
1298 | 82 | DefaultFree, DefaultMember, CallOpCC}; |
1299 | 82 | llvm::sort(Convs); |
1300 | 82 | llvm::iterator_range<CallingConv *> Range( |
1301 | 82 | std::begin(Convs), std::unique(std::begin(Convs), std::end(Convs))); |
1302 | 82 | const TargetInfo &TI = S.getASTContext().getTargetInfo(); |
1303 | | |
1304 | 330 | for (CallingConv C : Range) { |
1305 | 330 | if (TI.checkCallingConvention(C) == TargetInfo::CCCR_OK) |
1306 | 170 | F(C); |
1307 | 330 | } |
1308 | 82 | return; |
1309 | 82 | } |
1310 | | |
1311 | 5.77k | if (CallOpCC == DefaultMember && DefaultMember != DefaultFree5.75k ) { |
1312 | 1.60k | F(DefaultFree); |
1313 | 1.60k | F(DefaultMember); |
1314 | 4.16k | } else { |
1315 | 4.16k | F(CallOpCC); |
1316 | 4.16k | } |
1317 | 5.77k | } |
1318 | | |
1319 | | // Returns the 'standard' calling convention to be used for the lambda |
1320 | | // conversion function, that is, the 'free' function calling convention unless |
1321 | | // it is overridden by a non-default calling convention attribute. |
1322 | | static CallingConv |
1323 | | getLambdaConversionFunctionCallConv(Sema &S, |
1324 | 162 | const FunctionProtoType *CallOpProto) { |
1325 | 162 | CallingConv DefaultFree = S.Context.getDefaultCallingConvention( |
1326 | 162 | CallOpProto->isVariadic(), /*IsCXXMethod=*/false); |
1327 | 162 | CallingConv DefaultMember = S.Context.getDefaultCallingConvention( |
1328 | 162 | CallOpProto->isVariadic(), /*IsCXXMethod=*/true); |
1329 | 162 | CallingConv CallOpCC = CallOpProto->getCallConv(); |
1330 | | |
1331 | | // If the call-operator hasn't been changed, return both the 'free' and |
1332 | | // 'member' function calling convention. |
1333 | 162 | if (CallOpCC == DefaultMember && DefaultMember != DefaultFree) |
1334 | 0 | return DefaultFree; |
1335 | 162 | return CallOpCC; |
1336 | 162 | } |
1337 | | |
1338 | | QualType Sema::getLambdaConversionFunctionResultType( |
1339 | 8.10k | const FunctionProtoType *CallOpProto, CallingConv CC) { |
1340 | 8.10k | const FunctionProtoType::ExtProtoInfo CallOpExtInfo = |
1341 | 8.10k | CallOpProto->getExtProtoInfo(); |
1342 | 8.10k | FunctionProtoType::ExtProtoInfo InvokerExtInfo = CallOpExtInfo; |
1343 | 8.10k | InvokerExtInfo.ExtInfo = InvokerExtInfo.ExtInfo.withCallingConv(CC); |
1344 | 8.10k | InvokerExtInfo.TypeQuals = Qualifiers(); |
1345 | 8.10k | assert(InvokerExtInfo.RefQualifier == RQ_None && |
1346 | 8.10k | "Lambda's call operator should not have a reference qualifier"); |
1347 | 0 | return Context.getFunctionType(CallOpProto->getReturnType(), |
1348 | 8.10k | CallOpProto->getParamTypes(), InvokerExtInfo); |
1349 | 8.10k | } |
1350 | | |
1351 | | /// Add a lambda's conversion to function pointer, as described in |
1352 | | /// C++11 [expr.prim.lambda]p6. |
1353 | | static void addFunctionPointerConversion(Sema &S, SourceRange IntroducerRange, |
1354 | | CXXRecordDecl *Class, |
1355 | | CXXMethodDecl *CallOperator, |
1356 | 7.55k | QualType InvokerFunctionTy) { |
1357 | | // This conversion is explicitly disabled if the lambda's function has |
1358 | | // pass_object_size attributes on any of its parameters. |
1359 | 7.55k | auto HasPassObjectSizeAttr = [](const ParmVarDecl *P) { |
1360 | 7.38k | return P->hasAttr<PassObjectSizeAttr>(); |
1361 | 7.38k | }; |
1362 | 7.55k | if (llvm::any_of(CallOperator->parameters(), HasPassObjectSizeAttr)) |
1363 | 2 | return; |
1364 | | |
1365 | | // Add the conversion to function pointer. |
1366 | 7.55k | QualType PtrToFunctionTy = S.Context.getPointerType(InvokerFunctionTy); |
1367 | | |
1368 | | // Create the type of the conversion function. |
1369 | 7.55k | FunctionProtoType::ExtProtoInfo ConvExtInfo( |
1370 | 7.55k | S.Context.getDefaultCallingConvention( |
1371 | 7.55k | /*IsVariadic=*/false, /*IsCXXMethod=*/true)); |
1372 | | // The conversion function is always const and noexcept. |
1373 | 7.55k | ConvExtInfo.TypeQuals = Qualifiers(); |
1374 | 7.55k | ConvExtInfo.TypeQuals.addConst(); |
1375 | 7.55k | ConvExtInfo.ExceptionSpec.Type = EST_BasicNoexcept; |
1376 | 7.55k | QualType ConvTy = |
1377 | 7.55k | S.Context.getFunctionType(PtrToFunctionTy, None, ConvExtInfo); |
1378 | | |
1379 | 7.55k | SourceLocation Loc = IntroducerRange.getBegin(); |
1380 | 7.55k | DeclarationName ConversionName |
1381 | 7.55k | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
1382 | 7.55k | S.Context.getCanonicalType(PtrToFunctionTy)); |
1383 | | // Construct a TypeSourceInfo for the conversion function, and wire |
1384 | | // all the parameters appropriately for the FunctionProtoTypeLoc |
1385 | | // so that everything works during transformation/instantiation of |
1386 | | // generic lambdas. |
1387 | | // The main reason for wiring up the parameters of the conversion |
1388 | | // function with that of the call operator is so that constructs |
1389 | | // like the following work: |
1390 | | // auto L = [](auto b) { <-- 1 |
1391 | | // return [](auto a) -> decltype(a) { <-- 2 |
1392 | | // return a; |
1393 | | // }; |
1394 | | // }; |
1395 | | // int (*fp)(int) = L(5); |
1396 | | // Because the trailing return type can contain DeclRefExprs that refer |
1397 | | // to the original call operator's variables, we hijack the call |
1398 | | // operators ParmVarDecls below. |
1399 | 7.55k | TypeSourceInfo *ConvNamePtrToFunctionTSI = |
1400 | 7.55k | S.Context.getTrivialTypeSourceInfo(PtrToFunctionTy, Loc); |
1401 | 7.55k | DeclarationNameLoc ConvNameLoc = |
1402 | 7.55k | DeclarationNameLoc::makeNamedTypeLoc(ConvNamePtrToFunctionTSI); |
1403 | | |
1404 | | // The conversion function is a conversion to a pointer-to-function. |
1405 | 7.55k | TypeSourceInfo *ConvTSI = S.Context.getTrivialTypeSourceInfo(ConvTy, Loc); |
1406 | 7.55k | FunctionProtoTypeLoc ConvTL = |
1407 | 7.55k | ConvTSI->getTypeLoc().getAs<FunctionProtoTypeLoc>(); |
1408 | | // Get the result of the conversion function which is a pointer-to-function. |
1409 | 7.55k | PointerTypeLoc PtrToFunctionTL = |
1410 | 7.55k | ConvTL.getReturnLoc().getAs<PointerTypeLoc>(); |
1411 | | // Do the same for the TypeSourceInfo that is used to name the conversion |
1412 | | // operator. |
1413 | 7.55k | PointerTypeLoc ConvNamePtrToFunctionTL = |
1414 | 7.55k | ConvNamePtrToFunctionTSI->getTypeLoc().getAs<PointerTypeLoc>(); |
1415 | | |
1416 | | // Get the underlying function types that the conversion function will |
1417 | | // be converting to (should match the type of the call operator). |
1418 | 7.55k | FunctionProtoTypeLoc CallOpConvTL = |
1419 | 7.55k | PtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>(); |
1420 | 7.55k | FunctionProtoTypeLoc CallOpConvNameTL = |
1421 | 7.55k | ConvNamePtrToFunctionTL.getPointeeLoc().getAs<FunctionProtoTypeLoc>(); |
1422 | | |
1423 | | // Wire up the FunctionProtoTypeLocs with the call operator's parameters. |
1424 | | // These parameter's are essentially used to transform the name and |
1425 | | // the type of the conversion operator. By using the same parameters |
1426 | | // as the call operator's we don't have to fix any back references that |
1427 | | // the trailing return type of the call operator's uses (such as |
1428 | | // decltype(some_type<decltype(a)>::type{} + decltype(a){}) etc.) |
1429 | | // - we can simply use the return type of the call operator, and |
1430 | | // everything should work. |
1431 | 7.55k | SmallVector<ParmVarDecl *, 4> InvokerParams; |
1432 | 14.9k | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I7.38k ) { |
1433 | 7.38k | ParmVarDecl *From = CallOperator->getParamDecl(I); |
1434 | | |
1435 | 7.38k | InvokerParams.push_back(ParmVarDecl::Create( |
1436 | 7.38k | S.Context, |
1437 | | // Temporarily add to the TU. This is set to the invoker below. |
1438 | 7.38k | S.Context.getTranslationUnitDecl(), From->getBeginLoc(), |
1439 | 7.38k | From->getLocation(), From->getIdentifier(), From->getType(), |
1440 | 7.38k | From->getTypeSourceInfo(), From->getStorageClass(), |
1441 | 7.38k | /*DefArg=*/nullptr)); |
1442 | 7.38k | CallOpConvTL.setParam(I, From); |
1443 | 7.38k | CallOpConvNameTL.setParam(I, From); |
1444 | 7.38k | } |
1445 | | |
1446 | 7.55k | CXXConversionDecl *Conversion = CXXConversionDecl::Create( |
1447 | 7.55k | S.Context, Class, Loc, |
1448 | 7.55k | DeclarationNameInfo(ConversionName, Loc, ConvNameLoc), ConvTy, ConvTSI, |
1449 | 7.55k | S.getCurFPFeatures().isFPConstrained(), |
1450 | 7.55k | /*isInline=*/true, ExplicitSpecifier(), |
1451 | 7.55k | S.getLangOpts().CPlusPlus17 ? ConstexprSpecKind::Constexpr1.74k |
1452 | 7.55k | : ConstexprSpecKind::Unspecified5.80k , |
1453 | 7.55k | CallOperator->getBody()->getEndLoc()); |
1454 | 7.55k | Conversion->setAccess(AS_public); |
1455 | 7.55k | Conversion->setImplicit(true); |
1456 | | |
1457 | 7.55k | if (Class->isGenericLambda()) { |
1458 | | // Create a template version of the conversion operator, using the template |
1459 | | // parameter list of the function call operator. |
1460 | 3.84k | FunctionTemplateDecl *TemplateCallOperator = |
1461 | 3.84k | CallOperator->getDescribedFunctionTemplate(); |
1462 | 3.84k | FunctionTemplateDecl *ConversionTemplate = |
1463 | 3.84k | FunctionTemplateDecl::Create(S.Context, Class, |
1464 | 3.84k | Loc, ConversionName, |
1465 | 3.84k | TemplateCallOperator->getTemplateParameters(), |
1466 | 3.84k | Conversion); |
1467 | 3.84k | ConversionTemplate->setAccess(AS_public); |
1468 | 3.84k | ConversionTemplate->setImplicit(true); |
1469 | 3.84k | Conversion->setDescribedFunctionTemplate(ConversionTemplate); |
1470 | 3.84k | Class->addDecl(ConversionTemplate); |
1471 | 3.84k | } else |
1472 | 3.70k | Class->addDecl(Conversion); |
1473 | | // Add a non-static member function that will be the result of |
1474 | | // the conversion with a certain unique ID. |
1475 | 7.55k | DeclarationName InvokerName = &S.Context.Idents.get( |
1476 | 7.55k | getLambdaStaticInvokerName()); |
1477 | | // FIXME: Instead of passing in the CallOperator->getTypeSourceInfo() |
1478 | | // we should get a prebuilt TrivialTypeSourceInfo from Context |
1479 | | // using FunctionTy & Loc and get its TypeLoc as a FunctionProtoTypeLoc |
1480 | | // then rewire the parameters accordingly, by hoisting up the InvokeParams |
1481 | | // loop below and then use its Params to set Invoke->setParams(...) below. |
1482 | | // This would avoid the 'const' qualifier of the calloperator from |
1483 | | // contaminating the type of the invoker, which is currently adjusted |
1484 | | // in SemaTemplateDeduction.cpp:DeduceTemplateArguments. Fixing the |
1485 | | // trailing return type of the invoker would require a visitor to rebuild |
1486 | | // the trailing return type and adjusting all back DeclRefExpr's to refer |
1487 | | // to the new static invoker parameters - not the call operator's. |
1488 | 7.55k | CXXMethodDecl *Invoke = CXXMethodDecl::Create( |
1489 | 7.55k | S.Context, Class, Loc, DeclarationNameInfo(InvokerName, Loc), |
1490 | 7.55k | InvokerFunctionTy, CallOperator->getTypeSourceInfo(), SC_Static, |
1491 | 7.55k | S.getCurFPFeatures().isFPConstrained(), |
1492 | 7.55k | /*isInline=*/true, ConstexprSpecKind::Unspecified, |
1493 | 7.55k | CallOperator->getBody()->getEndLoc()); |
1494 | 14.9k | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I7.38k ) |
1495 | 7.38k | InvokerParams[I]->setOwningFunction(Invoke); |
1496 | 7.55k | Invoke->setParams(InvokerParams); |
1497 | 7.55k | Invoke->setAccess(AS_private); |
1498 | 7.55k | Invoke->setImplicit(true); |
1499 | 7.55k | if (Class->isGenericLambda()) { |
1500 | 3.84k | FunctionTemplateDecl *TemplateCallOperator = |
1501 | 3.84k | CallOperator->getDescribedFunctionTemplate(); |
1502 | 3.84k | FunctionTemplateDecl *StaticInvokerTemplate = FunctionTemplateDecl::Create( |
1503 | 3.84k | S.Context, Class, Loc, InvokerName, |
1504 | 3.84k | TemplateCallOperator->getTemplateParameters(), |
1505 | 3.84k | Invoke); |
1506 | 3.84k | StaticInvokerTemplate->setAccess(AS_private); |
1507 | 3.84k | StaticInvokerTemplate->setImplicit(true); |
1508 | 3.84k | Invoke->setDescribedFunctionTemplate(StaticInvokerTemplate); |
1509 | 3.84k | Class->addDecl(StaticInvokerTemplate); |
1510 | 3.84k | } else |
1511 | 3.70k | Class->addDecl(Invoke); |
1512 | 7.55k | } |
1513 | | |
1514 | | /// Add a lambda's conversion to function pointers, as described in |
1515 | | /// C++11 [expr.prim.lambda]p6. Note that in most cases, this should emit only a |
1516 | | /// single pointer conversion. In the event that the default calling convention |
1517 | | /// for free and member functions is different, it will emit both conventions. |
1518 | | static void addFunctionPointerConversions(Sema &S, SourceRange IntroducerRange, |
1519 | | CXXRecordDecl *Class, |
1520 | 5.85k | CXXMethodDecl *CallOperator) { |
1521 | 5.85k | const FunctionProtoType *CallOpProto = |
1522 | 5.85k | CallOperator->getType()->castAs<FunctionProtoType>(); |
1523 | | |
1524 | 5.85k | repeatForLambdaConversionFunctionCallingConvs( |
1525 | 7.55k | S, *CallOpProto, [&](CallingConv CC) { |
1526 | 7.55k | QualType InvokerFunctionTy = |
1527 | 7.55k | S.getLambdaConversionFunctionResultType(CallOpProto, CC); |
1528 | 7.55k | addFunctionPointerConversion(S, IntroducerRange, Class, CallOperator, |
1529 | 7.55k | InvokerFunctionTy); |
1530 | 7.55k | }); |
1531 | 5.85k | } |
1532 | | |
1533 | | /// Add a lambda's conversion to block pointer. |
1534 | | static void addBlockPointerConversion(Sema &S, |
1535 | | SourceRange IntroducerRange, |
1536 | | CXXRecordDecl *Class, |
1537 | 162 | CXXMethodDecl *CallOperator) { |
1538 | 162 | const FunctionProtoType *CallOpProto = |
1539 | 162 | CallOperator->getType()->castAs<FunctionProtoType>(); |
1540 | 162 | QualType FunctionTy = S.getLambdaConversionFunctionResultType( |
1541 | 162 | CallOpProto, getLambdaConversionFunctionCallConv(S, CallOpProto)); |
1542 | 162 | QualType BlockPtrTy = S.Context.getBlockPointerType(FunctionTy); |
1543 | | |
1544 | 162 | FunctionProtoType::ExtProtoInfo ConversionEPI( |
1545 | 162 | S.Context.getDefaultCallingConvention( |
1546 | 162 | /*IsVariadic=*/false, /*IsCXXMethod=*/true)); |
1547 | 162 | ConversionEPI.TypeQuals = Qualifiers(); |
1548 | 162 | ConversionEPI.TypeQuals.addConst(); |
1549 | 162 | QualType ConvTy = S.Context.getFunctionType(BlockPtrTy, None, ConversionEPI); |
1550 | | |
1551 | 162 | SourceLocation Loc = IntroducerRange.getBegin(); |
1552 | 162 | DeclarationName Name |
1553 | 162 | = S.Context.DeclarationNames.getCXXConversionFunctionName( |
1554 | 162 | S.Context.getCanonicalType(BlockPtrTy)); |
1555 | 162 | DeclarationNameLoc NameLoc = DeclarationNameLoc::makeNamedTypeLoc( |
1556 | 162 | S.Context.getTrivialTypeSourceInfo(BlockPtrTy, Loc)); |
1557 | 162 | CXXConversionDecl *Conversion = CXXConversionDecl::Create( |
1558 | 162 | S.Context, Class, Loc, DeclarationNameInfo(Name, Loc, NameLoc), ConvTy, |
1559 | 162 | S.Context.getTrivialTypeSourceInfo(ConvTy, Loc), |
1560 | 162 | S.getCurFPFeatures().isFPConstrained(), |
1561 | 162 | /*isInline=*/true, ExplicitSpecifier(), ConstexprSpecKind::Unspecified, |
1562 | 162 | CallOperator->getBody()->getEndLoc()); |
1563 | 162 | Conversion->setAccess(AS_public); |
1564 | 162 | Conversion->setImplicit(true); |
1565 | 162 | Class->addDecl(Conversion); |
1566 | 162 | } |
1567 | | |
1568 | | ExprResult Sema::BuildCaptureInit(const Capture &Cap, |
1569 | | SourceLocation ImplicitCaptureLoc, |
1570 | 420k | bool IsOpenMPMapping) { |
1571 | | // VLA captures don't have a stored initialization expression. |
1572 | 420k | if (Cap.isVLATypeCapture()) |
1573 | 8.96k | return ExprResult(); |
1574 | | |
1575 | | // An init-capture is initialized directly from its stored initializer. |
1576 | 411k | if (Cap.isInitCapture()) |
1577 | 533 | return Cap.getVariable()->getInit(); |
1578 | | |
1579 | | // For anything else, build an initialization expression. For an implicit |
1580 | | // capture, the capture notionally happens at the capture-default, so use |
1581 | | // that location here. |
1582 | 411k | SourceLocation Loc = |
1583 | 411k | ImplicitCaptureLoc.isValid() ? ImplicitCaptureLoc409k : Cap.getLocation()1.57k ; |
1584 | | |
1585 | | // C++11 [expr.prim.lambda]p21: |
1586 | | // When the lambda-expression is evaluated, the entities that |
1587 | | // are captured by copy are used to direct-initialize each |
1588 | | // corresponding non-static data member of the resulting closure |
1589 | | // object. (For array members, the array elements are |
1590 | | // direct-initialized in increasing subscript order.) These |
1591 | | // initializations are performed in the (unspecified) order in |
1592 | | // which the non-static data members are declared. |
1593 | | |
1594 | | // C++ [expr.prim.lambda]p12: |
1595 | | // An entity captured by a lambda-expression is odr-used (3.2) in |
1596 | | // the scope containing the lambda-expression. |
1597 | 411k | ExprResult Init; |
1598 | 411k | IdentifierInfo *Name = nullptr; |
1599 | 411k | if (Cap.isThisCapture()) { |
1600 | 11.5k | QualType ThisTy = getCurrentThisType(); |
1601 | 11.5k | Expr *This = BuildCXXThisExpr(Loc, ThisTy, ImplicitCaptureLoc.isValid()); |
1602 | 11.5k | if (Cap.isCopyCapture()) |
1603 | 137 | Init = CreateBuiltinUnaryOp(Loc, UO_Deref, This); |
1604 | 11.4k | else |
1605 | 11.4k | Init = This; |
1606 | 399k | } else { |
1607 | 399k | assert(Cap.isVariableCapture() && "unknown kind of capture"); |
1608 | 0 | VarDecl *Var = Cap.getVariable(); |
1609 | 399k | Name = Var->getIdentifier(); |
1610 | 399k | Init = BuildDeclarationNameExpr( |
1611 | 399k | CXXScopeSpec(), DeclarationNameInfo(Var->getDeclName(), Loc), Var); |
1612 | 399k | } |
1613 | | |
1614 | | // In OpenMP, the capture kind doesn't actually describe how to capture: |
1615 | | // variables are "mapped" onto the device in a process that does not formally |
1616 | | // make a copy, even for a "copy capture". |
1617 | 411k | if (IsOpenMPMapping) |
1618 | 405k | return Init; |
1619 | | |
1620 | 5.73k | if (Init.isInvalid()) |
1621 | 0 | return ExprError(); |
1622 | | |
1623 | 5.73k | Expr *InitExpr = Init.get(); |
1624 | 5.73k | InitializedEntity Entity = InitializedEntity::InitializeLambdaCapture( |
1625 | 5.73k | Name, Cap.getCaptureType(), Loc); |
1626 | 5.73k | InitializationKind InitKind = |
1627 | 5.73k | InitializationKind::CreateDirect(Loc, Loc, Loc); |
1628 | 5.73k | InitializationSequence InitSeq(*this, Entity, InitKind, InitExpr); |
1629 | 5.73k | return InitSeq.Perform(*this, Entity, InitKind, InitExpr); |
1630 | 5.73k | } |
1631 | | |
1632 | | ExprResult Sema::ActOnLambdaExpr(SourceLocation StartLoc, Stmt *Body, |
1633 | 8.38k | Scope *CurScope) { |
1634 | 8.38k | LambdaScopeInfo LSI = *cast<LambdaScopeInfo>(FunctionScopes.back()); |
1635 | 8.38k | ActOnFinishFunctionBody(LSI.CallOperator, Body); |
1636 | 8.38k | return BuildLambdaExpr(StartLoc, Body->getEndLoc(), &LSI); |
1637 | 8.38k | } |
1638 | | |
1639 | | static LambdaCaptureDefault |
1640 | 10.9k | mapImplicitCaptureStyle(CapturingScopeInfo::ImplicitCaptureStyle ICS) { |
1641 | 10.9k | switch (ICS) { |
1642 | 7.43k | case CapturingScopeInfo::ImpCap_None: |
1643 | 7.43k | return LCD_None; |
1644 | 1.30k | case CapturingScopeInfo::ImpCap_LambdaByval: |
1645 | 1.30k | return LCD_ByCopy; |
1646 | 0 | case CapturingScopeInfo::ImpCap_CapturedRegion: |
1647 | 2.22k | case CapturingScopeInfo::ImpCap_LambdaByref: |
1648 | 2.22k | return LCD_ByRef; |
1649 | 0 | case CapturingScopeInfo::ImpCap_Block: |
1650 | 0 | llvm_unreachable("block capture in lambda"); |
1651 | 10.9k | } |
1652 | 0 | llvm_unreachable("Unknown implicit capture style"); |
1653 | 0 | } |
1654 | | |
1655 | 718 | bool Sema::CaptureHasSideEffects(const Capture &From) { |
1656 | 718 | if (From.isInitCapture()) { |
1657 | 211 | Expr *Init = From.getVariable()->getInit(); |
1658 | 211 | if (Init && Init->HasSideEffects(Context)) |
1659 | 54 | return true; |
1660 | 211 | } |
1661 | | |
1662 | 664 | if (!From.isCopyCapture()) |
1663 | 169 | return false; |
1664 | | |
1665 | 495 | const QualType T = From.isThisCapture() |
1666 | 495 | ? getCurrentThisType()->getPointeeType()42 |
1667 | 495 | : From.getCaptureType()453 ; |
1668 | | |
1669 | 495 | if (T.isVolatileQualified()) |
1670 | 2 | return true; |
1671 | | |
1672 | 493 | const Type *BaseT = T->getBaseElementTypeUnsafe(); |
1673 | 493 | if (const CXXRecordDecl *RD = BaseT->getAsCXXRecordDecl()) |
1674 | 102 | return !RD->isCompleteDefinition() || !RD->hasTrivialCopyConstructor() || |
1675 | 102 | !RD->hasTrivialDestructor()74 ; |
1676 | | |
1677 | 391 | return false; |
1678 | 493 | } |
1679 | | |
1680 | | bool Sema::DiagnoseUnusedLambdaCapture(SourceRange CaptureRange, |
1681 | 718 | const Capture &From) { |
1682 | 718 | if (CaptureHasSideEffects(From)) |
1683 | 91 | return false; |
1684 | | |
1685 | 627 | if (From.isVLATypeCapture()) |
1686 | 22 | return false; |
1687 | | |
1688 | 605 | auto diag = Diag(From.getLocation(), diag::warn_unused_lambda_capture); |
1689 | 605 | if (From.isThisCapture()) |
1690 | 89 | diag << "'this'"; |
1691 | 516 | else |
1692 | 516 | diag << From.getVariable(); |
1693 | 605 | diag << From.isNonODRUsed(); |
1694 | 605 | diag << FixItHint::CreateRemoval(CaptureRange); |
1695 | 605 | return true; |
1696 | 627 | } |
1697 | | |
1698 | | /// Create a field within the lambda class or captured statement record for the |
1699 | | /// given capture. |
1700 | | FieldDecl *Sema::BuildCaptureField(RecordDecl *RD, |
1701 | 420k | const sema::Capture &Capture) { |
1702 | 420k | SourceLocation Loc = Capture.getLocation(); |
1703 | 420k | QualType FieldType = Capture.getCaptureType(); |
1704 | | |
1705 | 420k | TypeSourceInfo *TSI = nullptr; |
1706 | 420k | if (Capture.isVariableCapture()) { |
1707 | 400k | auto *Var = Capture.getVariable(); |
1708 | 400k | if (Var->isInitCapture()) |
1709 | 580 | TSI = Capture.getVariable()->getTypeSourceInfo(); |
1710 | 400k | } |
1711 | | |
1712 | | // FIXME: Should we really be doing this? A null TypeSourceInfo seems more |
1713 | | // appropriate, at least for an implicit capture. |
1714 | 420k | if (!TSI) |
1715 | 420k | TSI = Context.getTrivialTypeSourceInfo(FieldType, Loc); |
1716 | | |
1717 | | // Build the non-static data member. |
1718 | 420k | FieldDecl *Field = |
1719 | 420k | FieldDecl::Create(Context, RD, /*StartLoc=*/Loc, /*IdLoc=*/Loc, |
1720 | 420k | /*Id=*/nullptr, FieldType, TSI, /*BW=*/nullptr, |
1721 | 420k | /*Mutable=*/false, ICIS_NoInit); |
1722 | | // If the variable being captured has an invalid type, mark the class as |
1723 | | // invalid as well. |
1724 | 420k | if (!FieldType->isDependentType()) { |
1725 | 416k | if (RequireCompleteSizedType(Loc, FieldType, |
1726 | 416k | diag::err_field_incomplete_or_sizeless)) { |
1727 | 3 | RD->setInvalidDecl(); |
1728 | 3 | Field->setInvalidDecl(); |
1729 | 416k | } else { |
1730 | 416k | NamedDecl *Def; |
1731 | 416k | FieldType->isIncompleteType(&Def); |
1732 | 416k | if (Def && Def->isInvalidDecl()308 ) { |
1733 | 2 | RD->setInvalidDecl(); |
1734 | 2 | Field->setInvalidDecl(); |
1735 | 2 | } |
1736 | 416k | } |
1737 | 416k | } |
1738 | 420k | Field->setImplicit(true); |
1739 | 420k | Field->setAccess(AS_private); |
1740 | 420k | RD->addDecl(Field); |
1741 | | |
1742 | 420k | if (Capture.isVLATypeCapture()) |
1743 | 8.96k | Field->setCapturedVLAType(Capture.getCapturedVLAType()); |
1744 | | |
1745 | 420k | return Field; |
1746 | 420k | } |
1747 | | |
1748 | | ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc, |
1749 | 10.9k | LambdaScopeInfo *LSI) { |
1750 | | // Collect information from the lambda scope. |
1751 | 10.9k | SmallVector<LambdaCapture, 4> Captures; |
1752 | 10.9k | SmallVector<Expr *, 4> CaptureInits; |
1753 | 10.9k | SourceLocation CaptureDefaultLoc = LSI->CaptureDefaultLoc; |
1754 | 10.9k | LambdaCaptureDefault CaptureDefault = |
1755 | 10.9k | mapImplicitCaptureStyle(LSI->ImpCaptureStyle); |
1756 | 10.9k | CXXRecordDecl *Class; |
1757 | 10.9k | CXXMethodDecl *CallOperator; |
1758 | 10.9k | SourceRange IntroducerRange; |
1759 | 10.9k | bool ExplicitParams; |
1760 | 10.9k | bool ExplicitResultType; |
1761 | 10.9k | CleanupInfo LambdaCleanup; |
1762 | 10.9k | bool ContainsUnexpandedParameterPack; |
1763 | 10.9k | bool IsGenericLambda; |
1764 | 10.9k | { |
1765 | 10.9k | CallOperator = LSI->CallOperator; |
1766 | 10.9k | Class = LSI->Lambda; |
1767 | 10.9k | IntroducerRange = LSI->IntroducerRange; |
1768 | 10.9k | ExplicitParams = LSI->ExplicitParams; |
1769 | 10.9k | ExplicitResultType = !LSI->HasImplicitReturnType; |
1770 | 10.9k | LambdaCleanup = LSI->Cleanup; |
1771 | 10.9k | ContainsUnexpandedParameterPack = LSI->ContainsUnexpandedParameterPack; |
1772 | 10.9k | IsGenericLambda = Class->isGenericLambda(); |
1773 | | |
1774 | 10.9k | CallOperator->setLexicalDeclContext(Class); |
1775 | 10.9k | Decl *TemplateOrNonTemplateCallOperatorDecl = |
1776 | 10.9k | CallOperator->getDescribedFunctionTemplate() |
1777 | 10.9k | ? CallOperator->getDescribedFunctionTemplate()3.76k |
1778 | 10.9k | : cast<Decl>(CallOperator)7.19k ; |
1779 | | |
1780 | | // FIXME: Is this really the best choice? Keeping the lexical decl context |
1781 | | // set as CurContext seems more faithful to the source. |
1782 | 10.9k | TemplateOrNonTemplateCallOperatorDecl->setLexicalDeclContext(Class); |
1783 | | |
1784 | 10.9k | PopExpressionEvaluationContext(); |
1785 | | |
1786 | | // True if the current capture has a used capture or default before it. |
1787 | 10.9k | bool CurHasPreviousCapture = CaptureDefault != LCD_None; |
1788 | 10.9k | SourceLocation PrevCaptureLoc = CurHasPreviousCapture ? |
1789 | 7.43k | CaptureDefaultLoc3.53k : IntroducerRange.getBegin(); |
1790 | | |
1791 | 16.9k | for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I6.01k ) { |
1792 | 6.04k | const Capture &From = LSI->Captures[I]; |
1793 | | |
1794 | 6.04k | if (From.isInvalid()) |
1795 | 24 | return ExprError(); |
1796 | | |
1797 | 6.01k | assert(!From.isBlockCapture() && "Cannot capture __block variables"); |
1798 | 0 | bool IsImplicit = I >= LSI->NumExplicitCaptures; |
1799 | 6.01k | SourceLocation ImplicitCaptureLoc = |
1800 | 6.01k | IsImplicit ? CaptureDefaultLoc3.98k : SourceLocation()2.03k ; |
1801 | | |
1802 | | // Use source ranges of explicit captures for fixits where available. |
1803 | 6.01k | SourceRange CaptureRange = LSI->ExplicitCaptureRanges[I]; |
1804 | | |
1805 | | // Warn about unused explicit captures. |
1806 | 6.01k | bool IsCaptureUsed = true; |
1807 | 6.01k | if (!CurContext->isDependentContext() && !IsImplicit5.43k && |
1808 | 6.01k | !From.isODRUsed()1.59k ) { |
1809 | | // Initialized captures that are non-ODR used may not be eliminated. |
1810 | | // FIXME: Where did the IsGenericLambda here come from? |
1811 | 729 | bool NonODRUsedInitCapture = |
1812 | 729 | IsGenericLambda && From.isNonODRUsed()39 && From.isInitCapture()12 ; |
1813 | 729 | if (!NonODRUsedInitCapture) { |
1814 | 718 | bool IsLast = (I + 1) == LSI->NumExplicitCaptures; |
1815 | 718 | SourceRange FixItRange; |
1816 | 718 | if (CaptureRange.isValid()) { |
1817 | 551 | if (!CurHasPreviousCapture && !IsLast483 ) { |
1818 | | // If there are no captures preceding this capture, remove the |
1819 | | // following comma. |
1820 | 97 | FixItRange = SourceRange(CaptureRange.getBegin(), |
1821 | 97 | getLocForEndOfToken(CaptureRange.getEnd())); |
1822 | 454 | } else { |
1823 | | // Otherwise, remove the comma since the last used capture. |
1824 | 454 | FixItRange = SourceRange(getLocForEndOfToken(PrevCaptureLoc), |
1825 | 454 | CaptureRange.getEnd()); |
1826 | 454 | } |
1827 | 551 | } |
1828 | | |
1829 | 718 | IsCaptureUsed = !DiagnoseUnusedLambdaCapture(FixItRange, From); |
1830 | 718 | } |
1831 | 729 | } |
1832 | | |
1833 | 6.01k | if (CaptureRange.isValid()) { |
1834 | 1.60k | CurHasPreviousCapture |= IsCaptureUsed; |
1835 | 1.60k | PrevCaptureLoc = CaptureRange.getEnd(); |
1836 | 1.60k | } |
1837 | | |
1838 | | // Map the capture to our AST representation. |
1839 | 6.01k | LambdaCapture Capture = [&] { |
1840 | 6.01k | if (From.isThisCapture()) { |
1841 | | // Capturing 'this' implicitly with a default of '[=]' is deprecated, |
1842 | | // because it results in a reference capture. Don't warn prior to |
1843 | | // C++2a; there's nothing that can be done about it before then. |
1844 | 851 | if (getLangOpts().CPlusPlus20 && IsImplicit88 && |
1845 | 851 | CaptureDefault == LCD_ByCopy52 ) { |
1846 | 41 | Diag(From.getLocation(), diag::warn_deprecated_this_capture); |
1847 | 41 | Diag(CaptureDefaultLoc, diag::note_deprecated_this_capture) |
1848 | 41 | << FixItHint::CreateInsertion( |
1849 | 41 | getLocForEndOfToken(CaptureDefaultLoc), ", this"); |
1850 | 41 | } |
1851 | 851 | return LambdaCapture(From.getLocation(), IsImplicit, |
1852 | 851 | From.isCopyCapture() ? LCK_StarThis137 : LCK_This714 ); |
1853 | 5.16k | } else if (From.isVLATypeCapture()) { |
1854 | 50 | return LambdaCapture(From.getLocation(), IsImplicit, LCK_VLAType); |
1855 | 5.11k | } else { |
1856 | 5.11k | assert(From.isVariableCapture() && "unknown kind of capture"); |
1857 | 0 | VarDecl *Var = From.getVariable(); |
1858 | 5.11k | LambdaCaptureKind Kind = |
1859 | 5.11k | From.isCopyCapture() ? LCK_ByCopy1.85k : LCK_ByRef3.26k ; |
1860 | 5.11k | return LambdaCapture(From.getLocation(), IsImplicit, Kind, Var, |
1861 | 5.11k | From.getEllipsisLoc()); |
1862 | 5.11k | } |
1863 | 6.01k | }(); |
1864 | | |
1865 | | // Form the initializer for the capture field. |
1866 | 6.01k | ExprResult Init = BuildCaptureInit(From, ImplicitCaptureLoc); |
1867 | | |
1868 | | // FIXME: Skip this capture if the capture is not used, the initializer |
1869 | | // has no side-effects, the type of the capture is trivial, and the |
1870 | | // lambda is not externally visible. |
1871 | | |
1872 | | // Add a FieldDecl for the capture and form its initializer. |
1873 | 6.01k | BuildCaptureField(Class, From); |
1874 | 6.01k | Captures.push_back(Capture); |
1875 | 6.01k | CaptureInits.push_back(Init.get()); |
1876 | | |
1877 | 6.01k | if (LangOpts.CUDA) |
1878 | 98 | CUDACheckLambdaCapture(CallOperator, From); |
1879 | 6.01k | } |
1880 | | |
1881 | 10.9k | Class->setCaptures(Context, Captures); |
1882 | | |
1883 | | // C++11 [expr.prim.lambda]p6: |
1884 | | // The closure type for a lambda-expression with no lambda-capture |
1885 | | // has a public non-virtual non-explicit const conversion function |
1886 | | // to pointer to function having the same parameter and return |
1887 | | // types as the closure type's function call operator. |
1888 | 10.9k | if (Captures.empty() && CaptureDefault == LCD_None7.15k ) |
1889 | 5.85k | addFunctionPointerConversions(*this, IntroducerRange, Class, |
1890 | 5.85k | CallOperator); |
1891 | | |
1892 | | // Objective-C++: |
1893 | | // The closure type for a lambda-expression has a public non-virtual |
1894 | | // non-explicit const conversion function to a block pointer having the |
1895 | | // same parameter and return types as the closure type's function call |
1896 | | // operator. |
1897 | | // FIXME: Fix generic lambda to block conversions. |
1898 | 10.9k | if (getLangOpts().Blocks && getLangOpts().ObjC5.87k && !IsGenericLambda171 ) |
1899 | 162 | addBlockPointerConversion(*this, IntroducerRange, Class, CallOperator); |
1900 | | |
1901 | | // Finalize the lambda class. |
1902 | 10.9k | SmallVector<Decl*, 4> Fields(Class->fields()); |
1903 | 10.9k | ActOnFields(nullptr, Class->getLocation(), Class, Fields, SourceLocation(), |
1904 | 10.9k | SourceLocation(), ParsedAttributesView()); |
1905 | 10.9k | CheckCompletedCXXClass(nullptr, Class); |
1906 | 10.9k | } |
1907 | | |
1908 | 0 | Cleanup.mergeFrom(LambdaCleanup); |
1909 | | |
1910 | 10.9k | LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, |
1911 | 10.9k | CaptureDefault, CaptureDefaultLoc, |
1912 | 10.9k | ExplicitParams, ExplicitResultType, |
1913 | 10.9k | CaptureInits, EndLoc, |
1914 | 10.9k | ContainsUnexpandedParameterPack); |
1915 | | // If the lambda expression's call operator is not explicitly marked constexpr |
1916 | | // and we are not in a dependent context, analyze the call operator to infer |
1917 | | // its constexpr-ness, suppressing diagnostics while doing so. |
1918 | 10.9k | if (getLangOpts().CPlusPlus17 && !CallOperator->isInvalidDecl()3.41k && |
1919 | 10.9k | !CallOperator->isConstexpr()3.39k && |
1920 | 10.9k | !isa<CoroutineBodyStmt>(CallOperator->getBody())3.22k && |
1921 | 10.9k | !Class->getDeclContext()->isDependentContext()3.18k ) { |
1922 | 2.16k | CallOperator->setConstexprKind( |
1923 | 2.16k | CheckConstexprFunctionDefinition(CallOperator, |
1924 | 2.16k | CheckConstexprKind::CheckValid) |
1925 | 2.16k | ? ConstexprSpecKind::Constexpr2.11k |
1926 | 2.16k | : ConstexprSpecKind::Unspecified50 ); |
1927 | 2.16k | } |
1928 | | |
1929 | | // Emit delayed shadowing warnings now that the full capture list is known. |
1930 | 10.9k | DiagnoseShadowingLambdaDecls(LSI); |
1931 | | |
1932 | 10.9k | if (!CurContext->isDependentContext()) { |
1933 | 8.18k | switch (ExprEvalContexts.back().Context) { |
1934 | | // C++11 [expr.prim.lambda]p2: |
1935 | | // A lambda-expression shall not appear in an unevaluated operand |
1936 | | // (Clause 5). |
1937 | 39 | case ExpressionEvaluationContext::Unevaluated: |
1938 | 39 | case ExpressionEvaluationContext::UnevaluatedList: |
1939 | 39 | case ExpressionEvaluationContext::UnevaluatedAbstract: |
1940 | | // C++1y [expr.const]p2: |
1941 | | // A conditional-expression e is a core constant expression unless the |
1942 | | // evaluation of e, following the rules of the abstract machine, would |
1943 | | // evaluate [...] a lambda-expression. |
1944 | | // |
1945 | | // This is technically incorrect, there are some constant evaluated contexts |
1946 | | // where this should be allowed. We should probably fix this when DR1607 is |
1947 | | // ratified, it lays out the exact set of conditions where we shouldn't |
1948 | | // allow a lambda-expression. |
1949 | 220 | case ExpressionEvaluationContext::ConstantEvaluated: |
1950 | 220 | case ExpressionEvaluationContext::ImmediateFunctionContext: |
1951 | | // We don't actually diagnose this case immediately, because we |
1952 | | // could be within a context where we might find out later that |
1953 | | // the expression is potentially evaluated (e.g., for typeid). |
1954 | 220 | ExprEvalContexts.back().Lambdas.push_back(Lambda); |
1955 | 220 | break; |
1956 | | |
1957 | 0 | case ExpressionEvaluationContext::DiscardedStatement: |
1958 | 7.68k | case ExpressionEvaluationContext::PotentiallyEvaluated: |
1959 | 7.96k | case ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed: |
1960 | 7.96k | break; |
1961 | 8.18k | } |
1962 | 8.18k | } |
1963 | | |
1964 | 10.9k | return MaybeBindToTemporary(Lambda); |
1965 | 10.9k | } |
1966 | | |
1967 | | ExprResult Sema::BuildBlockForLambdaConversion(SourceLocation CurrentLocation, |
1968 | | SourceLocation ConvLocation, |
1969 | | CXXConversionDecl *Conv, |
1970 | 31 | Expr *Src) { |
1971 | | // Make sure that the lambda call operator is marked used. |
1972 | 31 | CXXRecordDecl *Lambda = Conv->getParent(); |
1973 | 31 | CXXMethodDecl *CallOperator |
1974 | 31 | = cast<CXXMethodDecl>( |
1975 | 31 | Lambda->lookup( |
1976 | 31 | Context.DeclarationNames.getCXXOperatorName(OO_Call)).front()); |
1977 | 31 | CallOperator->setReferenced(); |
1978 | 31 | CallOperator->markUsed(Context); |
1979 | | |
1980 | 31 | ExprResult Init = PerformCopyInitialization( |
1981 | 31 | InitializedEntity::InitializeLambdaToBlock(ConvLocation, Src->getType()), |
1982 | 31 | CurrentLocation, Src); |
1983 | 31 | if (!Init.isInvalid()) |
1984 | 31 | Init = ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false); |
1985 | | |
1986 | 31 | if (Init.isInvalid()) |
1987 | 0 | return ExprError(); |
1988 | | |
1989 | | // Create the new block to be returned. |
1990 | 31 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, ConvLocation); |
1991 | | |
1992 | | // Set the type information. |
1993 | 31 | Block->setSignatureAsWritten(CallOperator->getTypeSourceInfo()); |
1994 | 31 | Block->setIsVariadic(CallOperator->isVariadic()); |
1995 | 31 | Block->setBlockMissingReturnType(false); |
1996 | | |
1997 | | // Add parameters. |
1998 | 31 | SmallVector<ParmVarDecl *, 4> BlockParams; |
1999 | 42 | for (unsigned I = 0, N = CallOperator->getNumParams(); I != N; ++I11 ) { |
2000 | 11 | ParmVarDecl *From = CallOperator->getParamDecl(I); |
2001 | 11 | BlockParams.push_back(ParmVarDecl::Create( |
2002 | 11 | Context, Block, From->getBeginLoc(), From->getLocation(), |
2003 | 11 | From->getIdentifier(), From->getType(), From->getTypeSourceInfo(), |
2004 | 11 | From->getStorageClass(), |
2005 | 11 | /*DefArg=*/nullptr)); |
2006 | 11 | } |
2007 | 31 | Block->setParams(BlockParams); |
2008 | | |
2009 | 31 | Block->setIsConversionFromLambda(true); |
2010 | | |
2011 | | // Add capture. The capture uses a fake variable, which doesn't correspond |
2012 | | // to any actual memory location. However, the initializer copy-initializes |
2013 | | // the lambda object. |
2014 | 31 | TypeSourceInfo *CapVarTSI = |
2015 | 31 | Context.getTrivialTypeSourceInfo(Src->getType()); |
2016 | 31 | VarDecl *CapVar = VarDecl::Create(Context, Block, ConvLocation, |
2017 | 31 | ConvLocation, nullptr, |
2018 | 31 | Src->getType(), CapVarTSI, |
2019 | 31 | SC_None); |
2020 | 31 | BlockDecl::Capture Capture(/*variable=*/CapVar, /*byRef=*/false, |
2021 | 31 | /*nested=*/false, /*copy=*/Init.get()); |
2022 | 31 | Block->setCaptures(Context, Capture, /*CapturesCXXThis=*/false); |
2023 | | |
2024 | | // Add a fake function body to the block. IR generation is responsible |
2025 | | // for filling in the actual body, which cannot be expressed as an AST. |
2026 | 31 | Block->setBody(new (Context) CompoundStmt(ConvLocation)); |
2027 | | |
2028 | | // Create the block literal expression. |
2029 | 31 | Expr *BuildBlock = new (Context) BlockExpr(Block, Conv->getConversionType()); |
2030 | 31 | ExprCleanupObjects.push_back(Block); |
2031 | 31 | Cleanup.setExprNeedsCleanups(true); |
2032 | | |
2033 | 31 | return BuildBlock; |
2034 | 31 | } |