1*67e74705SXin Li //===--- ScopeInfo.h - Information about a semantic context -----*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file defines FunctionScopeInfo and its subclasses, which contain
11*67e74705SXin Li // information about a single function, block, lambda, or method body.
12*67e74705SXin Li //
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li
15*67e74705SXin Li #ifndef LLVM_CLANG_SEMA_SCOPEINFO_H
16*67e74705SXin Li #define LLVM_CLANG_SEMA_SCOPEINFO_H
17*67e74705SXin Li
18*67e74705SXin Li #include "clang/AST/Expr.h"
19*67e74705SXin Li #include "clang/AST/Type.h"
20*67e74705SXin Li #include "clang/Basic/CapturedStmt.h"
21*67e74705SXin Li #include "clang/Basic/PartialDiagnostic.h"
22*67e74705SXin Li #include "clang/Sema/CleanupInfo.h"
23*67e74705SXin Li #include "clang/Sema/Ownership.h"
24*67e74705SXin Li #include "llvm/ADT/DenseMap.h"
25*67e74705SXin Li #include "llvm/ADT/SmallSet.h"
26*67e74705SXin Li #include "llvm/ADT/SmallVector.h"
27*67e74705SXin Li #include <algorithm>
28*67e74705SXin Li
29*67e74705SXin Li namespace clang {
30*67e74705SXin Li
31*67e74705SXin Li class Decl;
32*67e74705SXin Li class BlockDecl;
33*67e74705SXin Li class CapturedDecl;
34*67e74705SXin Li class CXXMethodDecl;
35*67e74705SXin Li class FieldDecl;
36*67e74705SXin Li class ObjCPropertyDecl;
37*67e74705SXin Li class IdentifierInfo;
38*67e74705SXin Li class ImplicitParamDecl;
39*67e74705SXin Li class LabelDecl;
40*67e74705SXin Li class ReturnStmt;
41*67e74705SXin Li class Scope;
42*67e74705SXin Li class SwitchStmt;
43*67e74705SXin Li class TemplateTypeParmDecl;
44*67e74705SXin Li class TemplateParameterList;
45*67e74705SXin Li class VarDecl;
46*67e74705SXin Li class ObjCIvarRefExpr;
47*67e74705SXin Li class ObjCPropertyRefExpr;
48*67e74705SXin Li class ObjCMessageExpr;
49*67e74705SXin Li
50*67e74705SXin Li namespace sema {
51*67e74705SXin Li
52*67e74705SXin Li /// \brief Contains information about the compound statement currently being
53*67e74705SXin Li /// parsed.
54*67e74705SXin Li class CompoundScopeInfo {
55*67e74705SXin Li public:
CompoundScopeInfo()56*67e74705SXin Li CompoundScopeInfo()
57*67e74705SXin Li : HasEmptyLoopBodies(false) { }
58*67e74705SXin Li
59*67e74705SXin Li /// \brief Whether this compound stamement contains `for' or `while' loops
60*67e74705SXin Li /// with empty bodies.
61*67e74705SXin Li bool HasEmptyLoopBodies;
62*67e74705SXin Li
setHasEmptyLoopBodies()63*67e74705SXin Li void setHasEmptyLoopBodies() {
64*67e74705SXin Li HasEmptyLoopBodies = true;
65*67e74705SXin Li }
66*67e74705SXin Li };
67*67e74705SXin Li
68*67e74705SXin Li class PossiblyUnreachableDiag {
69*67e74705SXin Li public:
70*67e74705SXin Li PartialDiagnostic PD;
71*67e74705SXin Li SourceLocation Loc;
72*67e74705SXin Li const Stmt *stmt;
73*67e74705SXin Li
PossiblyUnreachableDiag(const PartialDiagnostic & PD,SourceLocation Loc,const Stmt * stmt)74*67e74705SXin Li PossiblyUnreachableDiag(const PartialDiagnostic &PD, SourceLocation Loc,
75*67e74705SXin Li const Stmt *stmt)
76*67e74705SXin Li : PD(PD), Loc(Loc), stmt(stmt) {}
77*67e74705SXin Li };
78*67e74705SXin Li
79*67e74705SXin Li /// \brief Retains information about a function, method, or block that is
80*67e74705SXin Li /// currently being parsed.
81*67e74705SXin Li class FunctionScopeInfo {
82*67e74705SXin Li protected:
83*67e74705SXin Li enum ScopeKind {
84*67e74705SXin Li SK_Function,
85*67e74705SXin Li SK_Block,
86*67e74705SXin Li SK_Lambda,
87*67e74705SXin Li SK_CapturedRegion
88*67e74705SXin Li };
89*67e74705SXin Li
90*67e74705SXin Li public:
91*67e74705SXin Li /// \brief What kind of scope we are describing.
92*67e74705SXin Li ///
93*67e74705SXin Li ScopeKind Kind : 3;
94*67e74705SXin Li
95*67e74705SXin Li /// \brief Whether this function contains a VLA, \@try, try, C++
96*67e74705SXin Li /// initializer, or anything else that can't be jumped past.
97*67e74705SXin Li bool HasBranchProtectedScope : 1;
98*67e74705SXin Li
99*67e74705SXin Li /// \brief Whether this function contains any switches or direct gotos.
100*67e74705SXin Li bool HasBranchIntoScope : 1;
101*67e74705SXin Li
102*67e74705SXin Li /// \brief Whether this function contains any indirect gotos.
103*67e74705SXin Li bool HasIndirectGoto : 1;
104*67e74705SXin Li
105*67e74705SXin Li /// \brief Whether a statement was dropped because it was invalid.
106*67e74705SXin Li bool HasDroppedStmt : 1;
107*67e74705SXin Li
108*67e74705SXin Li /// \brief True if current scope is for OpenMP declare reduction combiner.
109*67e74705SXin Li bool HasOMPDeclareReductionCombiner;
110*67e74705SXin Li
111*67e74705SXin Li /// \brief Whether there is a fallthrough statement in this function.
112*67e74705SXin Li bool HasFallthroughStmt : 1;
113*67e74705SXin Li
114*67e74705SXin Li /// A flag that is set when parsing a method that must call super's
115*67e74705SXin Li /// implementation, such as \c -dealloc, \c -finalize, or any method marked
116*67e74705SXin Li /// with \c __attribute__((objc_requires_super)).
117*67e74705SXin Li bool ObjCShouldCallSuper : 1;
118*67e74705SXin Li
119*67e74705SXin Li /// True when this is a method marked as a designated initializer.
120*67e74705SXin Li bool ObjCIsDesignatedInit : 1;
121*67e74705SXin Li /// This starts true for a method marked as designated initializer and will
122*67e74705SXin Li /// be set to false if there is an invocation to a designated initializer of
123*67e74705SXin Li /// the super class.
124*67e74705SXin Li bool ObjCWarnForNoDesignatedInitChain : 1;
125*67e74705SXin Li
126*67e74705SXin Li /// True when this is an initializer method not marked as a designated
127*67e74705SXin Li /// initializer within a class that has at least one initializer marked as a
128*67e74705SXin Li /// designated initializer.
129*67e74705SXin Li bool ObjCIsSecondaryInit : 1;
130*67e74705SXin Li /// This starts true for a secondary initializer method and will be set to
131*67e74705SXin Li /// false if there is an invocation of an initializer on 'self'.
132*67e74705SXin Li bool ObjCWarnForNoInitDelegation : 1;
133*67e74705SXin Li
134*67e74705SXin Li /// First 'return' statement in the current function.
135*67e74705SXin Li SourceLocation FirstReturnLoc;
136*67e74705SXin Li
137*67e74705SXin Li /// First C++ 'try' statement in the current function.
138*67e74705SXin Li SourceLocation FirstCXXTryLoc;
139*67e74705SXin Li
140*67e74705SXin Li /// First SEH '__try' statement in the current function.
141*67e74705SXin Li SourceLocation FirstSEHTryLoc;
142*67e74705SXin Li
143*67e74705SXin Li /// \brief Used to determine if errors occurred in this function or block.
144*67e74705SXin Li DiagnosticErrorTrap ErrorTrap;
145*67e74705SXin Li
146*67e74705SXin Li /// SwitchStack - This is the current set of active switch statements in the
147*67e74705SXin Li /// block.
148*67e74705SXin Li SmallVector<SwitchStmt*, 8> SwitchStack;
149*67e74705SXin Li
150*67e74705SXin Li /// \brief The list of return statements that occur within the function or
151*67e74705SXin Li /// block, if there is any chance of applying the named return value
152*67e74705SXin Li /// optimization, or if we need to infer a return type.
153*67e74705SXin Li SmallVector<ReturnStmt*, 4> Returns;
154*67e74705SXin Li
155*67e74705SXin Li /// \brief The promise object for this coroutine, if any.
156*67e74705SXin Li VarDecl *CoroutinePromise;
157*67e74705SXin Li
158*67e74705SXin Li /// \brief The list of coroutine control flow constructs (co_await, co_yield,
159*67e74705SXin Li /// co_return) that occur within the function or block. Empty if and only if
160*67e74705SXin Li /// this function or block is not (yet known to be) a coroutine.
161*67e74705SXin Li SmallVector<Stmt*, 4> CoroutineStmts;
162*67e74705SXin Li
163*67e74705SXin Li /// \brief The stack of currently active compound stamement scopes in the
164*67e74705SXin Li /// function.
165*67e74705SXin Li SmallVector<CompoundScopeInfo, 4> CompoundScopes;
166*67e74705SXin Li
167*67e74705SXin Li /// \brief A list of PartialDiagnostics created but delayed within the
168*67e74705SXin Li /// current function scope. These diagnostics are vetted for reachability
169*67e74705SXin Li /// prior to being emitted.
170*67e74705SXin Li SmallVector<PossiblyUnreachableDiag, 4> PossiblyUnreachableDiags;
171*67e74705SXin Li
172*67e74705SXin Li /// \brief A list of parameters which have the nonnull attribute and are
173*67e74705SXin Li /// modified in the function.
174*67e74705SXin Li llvm::SmallPtrSet<const ParmVarDecl*, 8> ModifiedNonNullParams;
175*67e74705SXin Li
176*67e74705SXin Li public:
177*67e74705SXin Li /// Represents a simple identification of a weak object.
178*67e74705SXin Li ///
179*67e74705SXin Li /// Part of the implementation of -Wrepeated-use-of-weak.
180*67e74705SXin Li ///
181*67e74705SXin Li /// This is used to determine if two weak accesses refer to the same object.
182*67e74705SXin Li /// Here are some examples of how various accesses are "profiled":
183*67e74705SXin Li ///
184*67e74705SXin Li /// Access Expression | "Base" Decl | "Property" Decl
185*67e74705SXin Li /// :---------------: | :-----------------: | :------------------------------:
186*67e74705SXin Li /// self.property | self (VarDecl) | property (ObjCPropertyDecl)
187*67e74705SXin Li /// self.implicitProp | self (VarDecl) | -implicitProp (ObjCMethodDecl)
188*67e74705SXin Li /// self->ivar.prop | ivar (ObjCIvarDecl) | prop (ObjCPropertyDecl)
189*67e74705SXin Li /// cxxObj.obj.prop | obj (FieldDecl) | prop (ObjCPropertyDecl)
190*67e74705SXin Li /// [self foo].prop | 0 (unknown) | prop (ObjCPropertyDecl)
191*67e74705SXin Li /// self.prop1.prop2 | prop1 (ObjCPropertyDecl) | prop2 (ObjCPropertyDecl)
192*67e74705SXin Li /// MyClass.prop | MyClass (ObjCInterfaceDecl) | -prop (ObjCMethodDecl)
193*67e74705SXin Li /// MyClass.foo.prop | +foo (ObjCMethodDecl) | -prop (ObjCPropertyDecl)
194*67e74705SXin Li /// weakVar | 0 (known) | weakVar (VarDecl)
195*67e74705SXin Li /// self->weakIvar | self (VarDecl) | weakIvar (ObjCIvarDecl)
196*67e74705SXin Li ///
197*67e74705SXin Li /// Objects are identified with only two Decls to make it reasonably fast to
198*67e74705SXin Li /// compare them.
199*67e74705SXin Li class WeakObjectProfileTy {
200*67e74705SXin Li /// The base object decl, as described in the class documentation.
201*67e74705SXin Li ///
202*67e74705SXin Li /// The extra flag is "true" if the Base and Property are enough to uniquely
203*67e74705SXin Li /// identify the object in memory.
204*67e74705SXin Li ///
205*67e74705SXin Li /// \sa isExactProfile()
206*67e74705SXin Li typedef llvm::PointerIntPair<const NamedDecl *, 1, bool> BaseInfoTy;
207*67e74705SXin Li BaseInfoTy Base;
208*67e74705SXin Li
209*67e74705SXin Li /// The "property" decl, as described in the class documentation.
210*67e74705SXin Li ///
211*67e74705SXin Li /// Note that this may not actually be an ObjCPropertyDecl, e.g. in the
212*67e74705SXin Li /// case of "implicit" properties (regular methods accessed via dot syntax).
213*67e74705SXin Li const NamedDecl *Property;
214*67e74705SXin Li
215*67e74705SXin Li /// Used to find the proper base profile for a given base expression.
216*67e74705SXin Li static BaseInfoTy getBaseInfo(const Expr *BaseE);
217*67e74705SXin Li
218*67e74705SXin Li inline WeakObjectProfileTy();
219*67e74705SXin Li static inline WeakObjectProfileTy getSentinel();
220*67e74705SXin Li
221*67e74705SXin Li public:
222*67e74705SXin Li WeakObjectProfileTy(const ObjCPropertyRefExpr *RE);
223*67e74705SXin Li WeakObjectProfileTy(const Expr *Base, const ObjCPropertyDecl *Property);
224*67e74705SXin Li WeakObjectProfileTy(const DeclRefExpr *RE);
225*67e74705SXin Li WeakObjectProfileTy(const ObjCIvarRefExpr *RE);
226*67e74705SXin Li
getBase()227*67e74705SXin Li const NamedDecl *getBase() const { return Base.getPointer(); }
getProperty()228*67e74705SXin Li const NamedDecl *getProperty() const { return Property; }
229*67e74705SXin Li
230*67e74705SXin Li /// Returns true if the object base specifies a known object in memory,
231*67e74705SXin Li /// rather than, say, an instance variable or property of another object.
232*67e74705SXin Li ///
233*67e74705SXin Li /// Note that this ignores the effects of aliasing; that is, \c foo.bar is
234*67e74705SXin Li /// considered an exact profile if \c foo is a local variable, even if
235*67e74705SXin Li /// another variable \c foo2 refers to the same object as \c foo.
236*67e74705SXin Li ///
237*67e74705SXin Li /// For increased precision, accesses with base variables that are
238*67e74705SXin Li /// properties or ivars of 'self' (e.g. self.prop1.prop2) are considered to
239*67e74705SXin Li /// be exact, though this is not true for arbitrary variables
240*67e74705SXin Li /// (foo.prop1.prop2).
isExactProfile()241*67e74705SXin Li bool isExactProfile() const {
242*67e74705SXin Li return Base.getInt();
243*67e74705SXin Li }
244*67e74705SXin Li
245*67e74705SXin Li bool operator==(const WeakObjectProfileTy &Other) const {
246*67e74705SXin Li return Base == Other.Base && Property == Other.Property;
247*67e74705SXin Li }
248*67e74705SXin Li
249*67e74705SXin Li // For use in DenseMap.
250*67e74705SXin Li // We can't specialize the usual llvm::DenseMapInfo at the end of the file
251*67e74705SXin Li // because by that point the DenseMap in FunctionScopeInfo has already been
252*67e74705SXin Li // instantiated.
253*67e74705SXin Li class DenseMapInfo {
254*67e74705SXin Li public:
getEmptyKey()255*67e74705SXin Li static inline WeakObjectProfileTy getEmptyKey() {
256*67e74705SXin Li return WeakObjectProfileTy();
257*67e74705SXin Li }
getTombstoneKey()258*67e74705SXin Li static inline WeakObjectProfileTy getTombstoneKey() {
259*67e74705SXin Li return WeakObjectProfileTy::getSentinel();
260*67e74705SXin Li }
261*67e74705SXin Li
getHashValue(const WeakObjectProfileTy & Val)262*67e74705SXin Li static unsigned getHashValue(const WeakObjectProfileTy &Val) {
263*67e74705SXin Li typedef std::pair<BaseInfoTy, const NamedDecl *> Pair;
264*67e74705SXin Li return llvm::DenseMapInfo<Pair>::getHashValue(Pair(Val.Base,
265*67e74705SXin Li Val.Property));
266*67e74705SXin Li }
267*67e74705SXin Li
isEqual(const WeakObjectProfileTy & LHS,const WeakObjectProfileTy & RHS)268*67e74705SXin Li static bool isEqual(const WeakObjectProfileTy &LHS,
269*67e74705SXin Li const WeakObjectProfileTy &RHS) {
270*67e74705SXin Li return LHS == RHS;
271*67e74705SXin Li }
272*67e74705SXin Li };
273*67e74705SXin Li };
274*67e74705SXin Li
275*67e74705SXin Li /// Represents a single use of a weak object.
276*67e74705SXin Li ///
277*67e74705SXin Li /// Stores both the expression and whether the access is potentially unsafe
278*67e74705SXin Li /// (i.e. it could potentially be warned about).
279*67e74705SXin Li ///
280*67e74705SXin Li /// Part of the implementation of -Wrepeated-use-of-weak.
281*67e74705SXin Li class WeakUseTy {
282*67e74705SXin Li llvm::PointerIntPair<const Expr *, 1, bool> Rep;
283*67e74705SXin Li public:
WeakUseTy(const Expr * Use,bool IsRead)284*67e74705SXin Li WeakUseTy(const Expr *Use, bool IsRead) : Rep(Use, IsRead) {}
285*67e74705SXin Li
getUseExpr()286*67e74705SXin Li const Expr *getUseExpr() const { return Rep.getPointer(); }
isUnsafe()287*67e74705SXin Li bool isUnsafe() const { return Rep.getInt(); }
markSafe()288*67e74705SXin Li void markSafe() { Rep.setInt(false); }
289*67e74705SXin Li
290*67e74705SXin Li bool operator==(const WeakUseTy &Other) const {
291*67e74705SXin Li return Rep == Other.Rep;
292*67e74705SXin Li }
293*67e74705SXin Li };
294*67e74705SXin Li
295*67e74705SXin Li /// Used to collect uses of a particular weak object in a function body.
296*67e74705SXin Li ///
297*67e74705SXin Li /// Part of the implementation of -Wrepeated-use-of-weak.
298*67e74705SXin Li typedef SmallVector<WeakUseTy, 4> WeakUseVector;
299*67e74705SXin Li
300*67e74705SXin Li /// Used to collect all uses of weak objects in a function body.
301*67e74705SXin Li ///
302*67e74705SXin Li /// Part of the implementation of -Wrepeated-use-of-weak.
303*67e74705SXin Li typedef llvm::SmallDenseMap<WeakObjectProfileTy, WeakUseVector, 8,
304*67e74705SXin Li WeakObjectProfileTy::DenseMapInfo>
305*67e74705SXin Li WeakObjectUseMap;
306*67e74705SXin Li
307*67e74705SXin Li private:
308*67e74705SXin Li /// Used to collect all uses of weak objects in this function body.
309*67e74705SXin Li ///
310*67e74705SXin Li /// Part of the implementation of -Wrepeated-use-of-weak.
311*67e74705SXin Li WeakObjectUseMap WeakObjectUses;
312*67e74705SXin Li
313*67e74705SXin Li protected:
314*67e74705SXin Li FunctionScopeInfo(const FunctionScopeInfo&) = default;
315*67e74705SXin Li
316*67e74705SXin Li public:
317*67e74705SXin Li /// Record that a weak object was accessed.
318*67e74705SXin Li ///
319*67e74705SXin Li /// Part of the implementation of -Wrepeated-use-of-weak.
320*67e74705SXin Li template <typename ExprT>
321*67e74705SXin Li inline void recordUseOfWeak(const ExprT *E, bool IsRead = true);
322*67e74705SXin Li
323*67e74705SXin Li void recordUseOfWeak(const ObjCMessageExpr *Msg,
324*67e74705SXin Li const ObjCPropertyDecl *Prop);
325*67e74705SXin Li
326*67e74705SXin Li /// Record that a given expression is a "safe" access of a weak object (e.g.
327*67e74705SXin Li /// assigning it to a strong variable.)
328*67e74705SXin Li ///
329*67e74705SXin Li /// Part of the implementation of -Wrepeated-use-of-weak.
330*67e74705SXin Li void markSafeWeakUse(const Expr *E);
331*67e74705SXin Li
getWeakObjectUses()332*67e74705SXin Li const WeakObjectUseMap &getWeakObjectUses() const {
333*67e74705SXin Li return WeakObjectUses;
334*67e74705SXin Li }
335*67e74705SXin Li
setHasBranchIntoScope()336*67e74705SXin Li void setHasBranchIntoScope() {
337*67e74705SXin Li HasBranchIntoScope = true;
338*67e74705SXin Li }
339*67e74705SXin Li
setHasBranchProtectedScope()340*67e74705SXin Li void setHasBranchProtectedScope() {
341*67e74705SXin Li HasBranchProtectedScope = true;
342*67e74705SXin Li }
343*67e74705SXin Li
setHasIndirectGoto()344*67e74705SXin Li void setHasIndirectGoto() {
345*67e74705SXin Li HasIndirectGoto = true;
346*67e74705SXin Li }
347*67e74705SXin Li
setHasDroppedStmt()348*67e74705SXin Li void setHasDroppedStmt() {
349*67e74705SXin Li HasDroppedStmt = true;
350*67e74705SXin Li }
351*67e74705SXin Li
setHasOMPDeclareReductionCombiner()352*67e74705SXin Li void setHasOMPDeclareReductionCombiner() {
353*67e74705SXin Li HasOMPDeclareReductionCombiner = true;
354*67e74705SXin Li }
355*67e74705SXin Li
setHasFallthroughStmt()356*67e74705SXin Li void setHasFallthroughStmt() {
357*67e74705SXin Li HasFallthroughStmt = true;
358*67e74705SXin Li }
359*67e74705SXin Li
setHasCXXTry(SourceLocation TryLoc)360*67e74705SXin Li void setHasCXXTry(SourceLocation TryLoc) {
361*67e74705SXin Li setHasBranchProtectedScope();
362*67e74705SXin Li FirstCXXTryLoc = TryLoc;
363*67e74705SXin Li }
364*67e74705SXin Li
setHasSEHTry(SourceLocation TryLoc)365*67e74705SXin Li void setHasSEHTry(SourceLocation TryLoc) {
366*67e74705SXin Li setHasBranchProtectedScope();
367*67e74705SXin Li FirstSEHTryLoc = TryLoc;
368*67e74705SXin Li }
369*67e74705SXin Li
NeedsScopeChecking()370*67e74705SXin Li bool NeedsScopeChecking() const {
371*67e74705SXin Li return !HasDroppedStmt &&
372*67e74705SXin Li (HasIndirectGoto ||
373*67e74705SXin Li (HasBranchProtectedScope && HasBranchIntoScope));
374*67e74705SXin Li }
375*67e74705SXin Li
FunctionScopeInfo(DiagnosticsEngine & Diag)376*67e74705SXin Li FunctionScopeInfo(DiagnosticsEngine &Diag)
377*67e74705SXin Li : Kind(SK_Function),
378*67e74705SXin Li HasBranchProtectedScope(false),
379*67e74705SXin Li HasBranchIntoScope(false),
380*67e74705SXin Li HasIndirectGoto(false),
381*67e74705SXin Li HasDroppedStmt(false),
382*67e74705SXin Li HasOMPDeclareReductionCombiner(false),
383*67e74705SXin Li HasFallthroughStmt(false),
384*67e74705SXin Li ObjCShouldCallSuper(false),
385*67e74705SXin Li ObjCIsDesignatedInit(false),
386*67e74705SXin Li ObjCWarnForNoDesignatedInitChain(false),
387*67e74705SXin Li ObjCIsSecondaryInit(false),
388*67e74705SXin Li ObjCWarnForNoInitDelegation(false),
389*67e74705SXin Li ErrorTrap(Diag) { }
390*67e74705SXin Li
391*67e74705SXin Li virtual ~FunctionScopeInfo();
392*67e74705SXin Li
393*67e74705SXin Li /// \brief Clear out the information in this function scope, making it
394*67e74705SXin Li /// suitable for reuse.
395*67e74705SXin Li void Clear();
396*67e74705SXin Li };
397*67e74705SXin Li
398*67e74705SXin Li class CapturingScopeInfo : public FunctionScopeInfo {
399*67e74705SXin Li protected:
400*67e74705SXin Li CapturingScopeInfo(const CapturingScopeInfo&) = default;
401*67e74705SXin Li
402*67e74705SXin Li public:
403*67e74705SXin Li enum ImplicitCaptureStyle {
404*67e74705SXin Li ImpCap_None, ImpCap_LambdaByval, ImpCap_LambdaByref, ImpCap_Block,
405*67e74705SXin Li ImpCap_CapturedRegion
406*67e74705SXin Li };
407*67e74705SXin Li
408*67e74705SXin Li ImplicitCaptureStyle ImpCaptureStyle;
409*67e74705SXin Li
410*67e74705SXin Li class Capture {
411*67e74705SXin Li // There are three categories of capture: capturing 'this', capturing
412*67e74705SXin Li // local variables, and C++1y initialized captures (which can have an
413*67e74705SXin Li // arbitrary initializer, and don't really capture in the traditional
414*67e74705SXin Li // sense at all).
415*67e74705SXin Li //
416*67e74705SXin Li // There are three ways to capture a local variable:
417*67e74705SXin Li // - capture by copy in the C++11 sense,
418*67e74705SXin Li // - capture by reference in the C++11 sense, and
419*67e74705SXin Li // - __block capture.
420*67e74705SXin Li // Lambdas explicitly specify capture by copy or capture by reference.
421*67e74705SXin Li // For blocks, __block capture applies to variables with that annotation,
422*67e74705SXin Li // variables of reference type are captured by reference, and other
423*67e74705SXin Li // variables are captured by copy.
424*67e74705SXin Li enum CaptureKind {
425*67e74705SXin Li Cap_ByCopy, Cap_ByRef, Cap_Block, Cap_VLA
426*67e74705SXin Li };
427*67e74705SXin Li enum {
428*67e74705SXin Li IsNestedCapture = 0x1,
429*67e74705SXin Li IsThisCaptured = 0x2
430*67e74705SXin Li };
431*67e74705SXin Li /// The variable being captured (if we are not capturing 'this') and whether
432*67e74705SXin Li /// this is a nested capture, and whether we are capturing 'this'
433*67e74705SXin Li llvm::PointerIntPair<VarDecl*, 2> VarAndNestedAndThis;
434*67e74705SXin Li /// Expression to initialize a field of the given type, and the kind of
435*67e74705SXin Li /// capture (if this is a capture and not an init-capture). The expression
436*67e74705SXin Li /// is only required if we are capturing ByVal and the variable's type has
437*67e74705SXin Li /// a non-trivial copy constructor.
438*67e74705SXin Li llvm::PointerIntPair<void *, 2, CaptureKind> InitExprAndCaptureKind;
439*67e74705SXin Li
440*67e74705SXin Li /// \brief The source location at which the first capture occurred.
441*67e74705SXin Li SourceLocation Loc;
442*67e74705SXin Li
443*67e74705SXin Li /// \brief The location of the ellipsis that expands a parameter pack.
444*67e74705SXin Li SourceLocation EllipsisLoc;
445*67e74705SXin Li
446*67e74705SXin Li /// \brief The type as it was captured, which is in effect the type of the
447*67e74705SXin Li /// non-static data member that would hold the capture.
448*67e74705SXin Li QualType CaptureType;
449*67e74705SXin Li
450*67e74705SXin Li public:
Capture(VarDecl * Var,bool Block,bool ByRef,bool IsNested,SourceLocation Loc,SourceLocation EllipsisLoc,QualType CaptureType,Expr * Cpy)451*67e74705SXin Li Capture(VarDecl *Var, bool Block, bool ByRef, bool IsNested,
452*67e74705SXin Li SourceLocation Loc, SourceLocation EllipsisLoc,
453*67e74705SXin Li QualType CaptureType, Expr *Cpy)
454*67e74705SXin Li : VarAndNestedAndThis(Var, IsNested ? IsNestedCapture : 0),
455*67e74705SXin Li InitExprAndCaptureKind(
456*67e74705SXin Li Cpy, !Var ? Cap_VLA : Block ? Cap_Block : ByRef ? Cap_ByRef
457*67e74705SXin Li : Cap_ByCopy),
458*67e74705SXin Li Loc(Loc), EllipsisLoc(EllipsisLoc), CaptureType(CaptureType) {}
459*67e74705SXin Li
460*67e74705SXin Li enum IsThisCapture { ThisCapture };
Capture(IsThisCapture,bool IsNested,SourceLocation Loc,QualType CaptureType,Expr * Cpy,const bool ByCopy)461*67e74705SXin Li Capture(IsThisCapture, bool IsNested, SourceLocation Loc,
462*67e74705SXin Li QualType CaptureType, Expr *Cpy, const bool ByCopy)
463*67e74705SXin Li : VarAndNestedAndThis(
464*67e74705SXin Li nullptr, (IsThisCaptured | (IsNested ? IsNestedCapture : 0))),
465*67e74705SXin Li InitExprAndCaptureKind(Cpy, ByCopy ? Cap_ByCopy : Cap_ByRef),
466*67e74705SXin Li Loc(Loc), EllipsisLoc(), CaptureType(CaptureType) {}
467*67e74705SXin Li
isThisCapture()468*67e74705SXin Li bool isThisCapture() const {
469*67e74705SXin Li return VarAndNestedAndThis.getInt() & IsThisCaptured;
470*67e74705SXin Li }
isVariableCapture()471*67e74705SXin Li bool isVariableCapture() const {
472*67e74705SXin Li return !isThisCapture() && !isVLATypeCapture();
473*67e74705SXin Li }
isCopyCapture()474*67e74705SXin Li bool isCopyCapture() const {
475*67e74705SXin Li return InitExprAndCaptureKind.getInt() == Cap_ByCopy;
476*67e74705SXin Li }
isReferenceCapture()477*67e74705SXin Li bool isReferenceCapture() const {
478*67e74705SXin Li return InitExprAndCaptureKind.getInt() == Cap_ByRef;
479*67e74705SXin Li }
isBlockCapture()480*67e74705SXin Li bool isBlockCapture() const {
481*67e74705SXin Li return InitExprAndCaptureKind.getInt() == Cap_Block;
482*67e74705SXin Li }
isVLATypeCapture()483*67e74705SXin Li bool isVLATypeCapture() const {
484*67e74705SXin Li return InitExprAndCaptureKind.getInt() == Cap_VLA;
485*67e74705SXin Li }
isNested()486*67e74705SXin Li bool isNested() const {
487*67e74705SXin Li return VarAndNestedAndThis.getInt() & IsNestedCapture;
488*67e74705SXin Li }
489*67e74705SXin Li
getVariable()490*67e74705SXin Li VarDecl *getVariable() const {
491*67e74705SXin Li return VarAndNestedAndThis.getPointer();
492*67e74705SXin Li }
493*67e74705SXin Li
494*67e74705SXin Li /// \brief Retrieve the location at which this variable was captured.
getLocation()495*67e74705SXin Li SourceLocation getLocation() const { return Loc; }
496*67e74705SXin Li
497*67e74705SXin Li /// \brief Retrieve the source location of the ellipsis, whose presence
498*67e74705SXin Li /// indicates that the capture is a pack expansion.
getEllipsisLoc()499*67e74705SXin Li SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
500*67e74705SXin Li
501*67e74705SXin Li /// \brief Retrieve the capture type for this capture, which is effectively
502*67e74705SXin Li /// the type of the non-static data member in the lambda/block structure
503*67e74705SXin Li /// that would store this capture.
getCaptureType()504*67e74705SXin Li QualType getCaptureType() const {
505*67e74705SXin Li assert(!isThisCapture());
506*67e74705SXin Li return CaptureType;
507*67e74705SXin Li }
508*67e74705SXin Li
getInitExpr()509*67e74705SXin Li Expr *getInitExpr() const {
510*67e74705SXin Li assert(!isVLATypeCapture() && "no init expression for type capture");
511*67e74705SXin Li return static_cast<Expr *>(InitExprAndCaptureKind.getPointer());
512*67e74705SXin Li }
513*67e74705SXin Li };
514*67e74705SXin Li
CapturingScopeInfo(DiagnosticsEngine & Diag,ImplicitCaptureStyle Style)515*67e74705SXin Li CapturingScopeInfo(DiagnosticsEngine &Diag, ImplicitCaptureStyle Style)
516*67e74705SXin Li : FunctionScopeInfo(Diag), ImpCaptureStyle(Style), CXXThisCaptureIndex(0),
517*67e74705SXin Li HasImplicitReturnType(false)
518*67e74705SXin Li {}
519*67e74705SXin Li
520*67e74705SXin Li /// CaptureMap - A map of captured variables to (index+1) into Captures.
521*67e74705SXin Li llvm::DenseMap<VarDecl*, unsigned> CaptureMap;
522*67e74705SXin Li
523*67e74705SXin Li /// CXXThisCaptureIndex - The (index+1) of the capture of 'this';
524*67e74705SXin Li /// zero if 'this' is not captured.
525*67e74705SXin Li unsigned CXXThisCaptureIndex;
526*67e74705SXin Li
527*67e74705SXin Li /// Captures - The captures.
528*67e74705SXin Li SmallVector<Capture, 4> Captures;
529*67e74705SXin Li
530*67e74705SXin Li /// \brief - Whether the target type of return statements in this context
531*67e74705SXin Li /// is deduced (e.g. a lambda or block with omitted return type).
532*67e74705SXin Li bool HasImplicitReturnType;
533*67e74705SXin Li
534*67e74705SXin Li /// ReturnType - The target type of return statements in this context,
535*67e74705SXin Li /// or null if unknown.
536*67e74705SXin Li QualType ReturnType;
537*67e74705SXin Li
addCapture(VarDecl * Var,bool isBlock,bool isByref,bool isNested,SourceLocation Loc,SourceLocation EllipsisLoc,QualType CaptureType,Expr * Cpy)538*67e74705SXin Li void addCapture(VarDecl *Var, bool isBlock, bool isByref, bool isNested,
539*67e74705SXin Li SourceLocation Loc, SourceLocation EllipsisLoc,
540*67e74705SXin Li QualType CaptureType, Expr *Cpy) {
541*67e74705SXin Li Captures.push_back(Capture(Var, isBlock, isByref, isNested, Loc,
542*67e74705SXin Li EllipsisLoc, CaptureType, Cpy));
543*67e74705SXin Li CaptureMap[Var] = Captures.size();
544*67e74705SXin Li }
545*67e74705SXin Li
addVLATypeCapture(SourceLocation Loc,QualType CaptureType)546*67e74705SXin Li void addVLATypeCapture(SourceLocation Loc, QualType CaptureType) {
547*67e74705SXin Li Captures.push_back(Capture(/*Var*/ nullptr, /*isBlock*/ false,
548*67e74705SXin Li /*isByref*/ false, /*isNested*/ false, Loc,
549*67e74705SXin Li /*EllipsisLoc*/ SourceLocation(), CaptureType,
550*67e74705SXin Li /*Cpy*/ nullptr));
551*67e74705SXin Li }
552*67e74705SXin Li
553*67e74705SXin Li // Note, we do not need to add the type of 'this' since that is always
554*67e74705SXin Li // retrievable from Sema::getCurrentThisType - and is also encoded within the
555*67e74705SXin Li // type of the corresponding FieldDecl.
556*67e74705SXin Li void addThisCapture(bool isNested, SourceLocation Loc,
557*67e74705SXin Li Expr *Cpy, bool ByCopy);
558*67e74705SXin Li
559*67e74705SXin Li /// \brief Determine whether the C++ 'this' is captured.
isCXXThisCaptured()560*67e74705SXin Li bool isCXXThisCaptured() const { return CXXThisCaptureIndex != 0; }
561*67e74705SXin Li
562*67e74705SXin Li /// \brief Retrieve the capture of C++ 'this', if it has been captured.
getCXXThisCapture()563*67e74705SXin Li Capture &getCXXThisCapture() {
564*67e74705SXin Li assert(isCXXThisCaptured() && "this has not been captured");
565*67e74705SXin Li return Captures[CXXThisCaptureIndex - 1];
566*67e74705SXin Li }
567*67e74705SXin Li
568*67e74705SXin Li /// \brief Determine whether the given variable has been captured.
isCaptured(VarDecl * Var)569*67e74705SXin Li bool isCaptured(VarDecl *Var) const {
570*67e74705SXin Li return CaptureMap.count(Var);
571*67e74705SXin Li }
572*67e74705SXin Li
573*67e74705SXin Li /// \brief Determine whether the given variable-array type has been captured.
574*67e74705SXin Li bool isVLATypeCaptured(const VariableArrayType *VAT) const;
575*67e74705SXin Li
576*67e74705SXin Li /// \brief Retrieve the capture of the given variable, if it has been
577*67e74705SXin Li /// captured already.
getCapture(VarDecl * Var)578*67e74705SXin Li Capture &getCapture(VarDecl *Var) {
579*67e74705SXin Li assert(isCaptured(Var) && "Variable has not been captured");
580*67e74705SXin Li return Captures[CaptureMap[Var] - 1];
581*67e74705SXin Li }
582*67e74705SXin Li
getCapture(VarDecl * Var)583*67e74705SXin Li const Capture &getCapture(VarDecl *Var) const {
584*67e74705SXin Li llvm::DenseMap<VarDecl*, unsigned>::const_iterator Known
585*67e74705SXin Li = CaptureMap.find(Var);
586*67e74705SXin Li assert(Known != CaptureMap.end() && "Variable has not been captured");
587*67e74705SXin Li return Captures[Known->second - 1];
588*67e74705SXin Li }
589*67e74705SXin Li
classof(const FunctionScopeInfo * FSI)590*67e74705SXin Li static bool classof(const FunctionScopeInfo *FSI) {
591*67e74705SXin Li return FSI->Kind == SK_Block || FSI->Kind == SK_Lambda
592*67e74705SXin Li || FSI->Kind == SK_CapturedRegion;
593*67e74705SXin Li }
594*67e74705SXin Li };
595*67e74705SXin Li
596*67e74705SXin Li /// \brief Retains information about a block that is currently being parsed.
597*67e74705SXin Li class BlockScopeInfo final : public CapturingScopeInfo {
598*67e74705SXin Li public:
599*67e74705SXin Li BlockDecl *TheDecl;
600*67e74705SXin Li
601*67e74705SXin Li /// TheScope - This is the scope for the block itself, which contains
602*67e74705SXin Li /// arguments etc.
603*67e74705SXin Li Scope *TheScope;
604*67e74705SXin Li
605*67e74705SXin Li /// BlockType - The function type of the block, if one was given.
606*67e74705SXin Li /// Its return type may be BuiltinType::Dependent.
607*67e74705SXin Li QualType FunctionType;
608*67e74705SXin Li
BlockScopeInfo(DiagnosticsEngine & Diag,Scope * BlockScope,BlockDecl * Block)609*67e74705SXin Li BlockScopeInfo(DiagnosticsEngine &Diag, Scope *BlockScope, BlockDecl *Block)
610*67e74705SXin Li : CapturingScopeInfo(Diag, ImpCap_Block), TheDecl(Block),
611*67e74705SXin Li TheScope(BlockScope)
612*67e74705SXin Li {
613*67e74705SXin Li Kind = SK_Block;
614*67e74705SXin Li }
615*67e74705SXin Li
616*67e74705SXin Li ~BlockScopeInfo() override;
617*67e74705SXin Li
classof(const FunctionScopeInfo * FSI)618*67e74705SXin Li static bool classof(const FunctionScopeInfo *FSI) {
619*67e74705SXin Li return FSI->Kind == SK_Block;
620*67e74705SXin Li }
621*67e74705SXin Li };
622*67e74705SXin Li
623*67e74705SXin Li /// \brief Retains information about a captured region.
624*67e74705SXin Li class CapturedRegionScopeInfo final : public CapturingScopeInfo {
625*67e74705SXin Li public:
626*67e74705SXin Li /// \brief The CapturedDecl for this statement.
627*67e74705SXin Li CapturedDecl *TheCapturedDecl;
628*67e74705SXin Li /// \brief The captured record type.
629*67e74705SXin Li RecordDecl *TheRecordDecl;
630*67e74705SXin Li /// \brief This is the enclosing scope of the captured region.
631*67e74705SXin Li Scope *TheScope;
632*67e74705SXin Li /// \brief The implicit parameter for the captured variables.
633*67e74705SXin Li ImplicitParamDecl *ContextParam;
634*67e74705SXin Li /// \brief The kind of captured region.
635*67e74705SXin Li unsigned short CapRegionKind;
636*67e74705SXin Li unsigned short OpenMPLevel;
637*67e74705SXin Li
CapturedRegionScopeInfo(DiagnosticsEngine & Diag,Scope * S,CapturedDecl * CD,RecordDecl * RD,ImplicitParamDecl * Context,CapturedRegionKind K,unsigned OpenMPLevel)638*67e74705SXin Li CapturedRegionScopeInfo(DiagnosticsEngine &Diag, Scope *S, CapturedDecl *CD,
639*67e74705SXin Li RecordDecl *RD, ImplicitParamDecl *Context,
640*67e74705SXin Li CapturedRegionKind K, unsigned OpenMPLevel)
641*67e74705SXin Li : CapturingScopeInfo(Diag, ImpCap_CapturedRegion),
642*67e74705SXin Li TheCapturedDecl(CD), TheRecordDecl(RD), TheScope(S),
643*67e74705SXin Li ContextParam(Context), CapRegionKind(K), OpenMPLevel(OpenMPLevel)
644*67e74705SXin Li {
645*67e74705SXin Li Kind = SK_CapturedRegion;
646*67e74705SXin Li }
647*67e74705SXin Li
648*67e74705SXin Li ~CapturedRegionScopeInfo() override;
649*67e74705SXin Li
650*67e74705SXin Li /// \brief A descriptive name for the kind of captured region this is.
getRegionName()651*67e74705SXin Li StringRef getRegionName() const {
652*67e74705SXin Li switch (CapRegionKind) {
653*67e74705SXin Li case CR_Default:
654*67e74705SXin Li return "default captured statement";
655*67e74705SXin Li case CR_OpenMP:
656*67e74705SXin Li return "OpenMP region";
657*67e74705SXin Li }
658*67e74705SXin Li llvm_unreachable("Invalid captured region kind!");
659*67e74705SXin Li }
660*67e74705SXin Li
classof(const FunctionScopeInfo * FSI)661*67e74705SXin Li static bool classof(const FunctionScopeInfo *FSI) {
662*67e74705SXin Li return FSI->Kind == SK_CapturedRegion;
663*67e74705SXin Li }
664*67e74705SXin Li };
665*67e74705SXin Li
666*67e74705SXin Li class LambdaScopeInfo final : public CapturingScopeInfo {
667*67e74705SXin Li public:
668*67e74705SXin Li /// \brief The class that describes the lambda.
669*67e74705SXin Li CXXRecordDecl *Lambda;
670*67e74705SXin Li
671*67e74705SXin Li /// \brief The lambda's compiler-generated \c operator().
672*67e74705SXin Li CXXMethodDecl *CallOperator;
673*67e74705SXin Li
674*67e74705SXin Li /// \brief Source range covering the lambda introducer [...].
675*67e74705SXin Li SourceRange IntroducerRange;
676*67e74705SXin Li
677*67e74705SXin Li /// \brief Source location of the '&' or '=' specifying the default capture
678*67e74705SXin Li /// type, if any.
679*67e74705SXin Li SourceLocation CaptureDefaultLoc;
680*67e74705SXin Li
681*67e74705SXin Li /// \brief The number of captures in the \c Captures list that are
682*67e74705SXin Li /// explicit captures.
683*67e74705SXin Li unsigned NumExplicitCaptures;
684*67e74705SXin Li
685*67e74705SXin Li /// \brief Whether this is a mutable lambda.
686*67e74705SXin Li bool Mutable;
687*67e74705SXin Li
688*67e74705SXin Li /// \brief Whether the (empty) parameter list is explicit.
689*67e74705SXin Li bool ExplicitParams;
690*67e74705SXin Li
691*67e74705SXin Li /// \brief Whether any of the capture expressions requires cleanups.
692*67e74705SXin Li CleanupInfo Cleanup;
693*67e74705SXin Li
694*67e74705SXin Li /// \brief Whether the lambda contains an unexpanded parameter pack.
695*67e74705SXin Li bool ContainsUnexpandedParameterPack;
696*67e74705SXin Li
697*67e74705SXin Li /// \brief If this is a generic lambda, use this as the depth of
698*67e74705SXin Li /// each 'auto' parameter, during initial AST construction.
699*67e74705SXin Li unsigned AutoTemplateParameterDepth;
700*67e74705SXin Li
701*67e74705SXin Li /// \brief Store the list of the auto parameters for a generic lambda.
702*67e74705SXin Li /// If this is a generic lambda, store the list of the auto
703*67e74705SXin Li /// parameters converted into TemplateTypeParmDecls into a vector
704*67e74705SXin Li /// that can be used to construct the generic lambda's template
705*67e74705SXin Li /// parameter list, during initial AST construction.
706*67e74705SXin Li SmallVector<TemplateTypeParmDecl*, 4> AutoTemplateParams;
707*67e74705SXin Li
708*67e74705SXin Li /// If this is a generic lambda, and the template parameter
709*67e74705SXin Li /// list has been created (from the AutoTemplateParams) then
710*67e74705SXin Li /// store a reference to it (cache it to avoid reconstructing it).
711*67e74705SXin Li TemplateParameterList *GLTemplateParameterList;
712*67e74705SXin Li
713*67e74705SXin Li /// \brief Contains all variable-referring-expressions (i.e. DeclRefExprs
714*67e74705SXin Li /// or MemberExprs) that refer to local variables in a generic lambda
715*67e74705SXin Li /// or a lambda in a potentially-evaluated-if-used context.
716*67e74705SXin Li ///
717*67e74705SXin Li /// Potentially capturable variables of a nested lambda that might need
718*67e74705SXin Li /// to be captured by the lambda are housed here.
719*67e74705SXin Li /// This is specifically useful for generic lambdas or
720*67e74705SXin Li /// lambdas within a a potentially evaluated-if-used context.
721*67e74705SXin Li /// If an enclosing variable is named in an expression of a lambda nested
722*67e74705SXin Li /// within a generic lambda, we don't always know know whether the variable
723*67e74705SXin Li /// will truly be odr-used (i.e. need to be captured) by that nested lambda,
724*67e74705SXin Li /// until its instantiation. But we still need to capture it in the
725*67e74705SXin Li /// enclosing lambda if all intervening lambdas can capture the variable.
726*67e74705SXin Li
727*67e74705SXin Li llvm::SmallVector<Expr*, 4> PotentiallyCapturingExprs;
728*67e74705SXin Li
729*67e74705SXin Li /// \brief Contains all variable-referring-expressions that refer
730*67e74705SXin Li /// to local variables that are usable as constant expressions and
731*67e74705SXin Li /// do not involve an odr-use (they may still need to be captured
732*67e74705SXin Li /// if the enclosing full-expression is instantiation dependent).
733*67e74705SXin Li llvm::SmallSet<Expr*, 8> NonODRUsedCapturingExprs;
734*67e74705SXin Li
735*67e74705SXin Li SourceLocation PotentialThisCaptureLocation;
736*67e74705SXin Li
LambdaScopeInfo(DiagnosticsEngine & Diag)737*67e74705SXin Li LambdaScopeInfo(DiagnosticsEngine &Diag)
738*67e74705SXin Li : CapturingScopeInfo(Diag, ImpCap_None), Lambda(nullptr),
739*67e74705SXin Li CallOperator(nullptr), NumExplicitCaptures(0), Mutable(false),
740*67e74705SXin Li ExplicitParams(false), Cleanup{},
741*67e74705SXin Li ContainsUnexpandedParameterPack(false), AutoTemplateParameterDepth(0),
742*67e74705SXin Li GLTemplateParameterList(nullptr) {
743*67e74705SXin Li Kind = SK_Lambda;
744*67e74705SXin Li }
745*67e74705SXin Li
746*67e74705SXin Li /// \brief Note when all explicit captures have been added.
finishedExplicitCaptures()747*67e74705SXin Li void finishedExplicitCaptures() {
748*67e74705SXin Li NumExplicitCaptures = Captures.size();
749*67e74705SXin Li }
750*67e74705SXin Li
classof(const FunctionScopeInfo * FSI)751*67e74705SXin Li static bool classof(const FunctionScopeInfo *FSI) {
752*67e74705SXin Li return FSI->Kind == SK_Lambda;
753*67e74705SXin Li }
754*67e74705SXin Li
755*67e74705SXin Li ///
756*67e74705SXin Li /// \brief Add a variable that might potentially be captured by the
757*67e74705SXin Li /// lambda and therefore the enclosing lambdas.
758*67e74705SXin Li ///
759*67e74705SXin Li /// This is also used by enclosing lambda's to speculatively capture
760*67e74705SXin Li /// variables that nested lambda's - depending on their enclosing
761*67e74705SXin Li /// specialization - might need to capture.
762*67e74705SXin Li /// Consider:
763*67e74705SXin Li /// void f(int, int); <-- don't capture
764*67e74705SXin Li /// void f(const int&, double); <-- capture
765*67e74705SXin Li /// void foo() {
766*67e74705SXin Li /// const int x = 10;
767*67e74705SXin Li /// auto L = [=](auto a) { // capture 'x'
768*67e74705SXin Li /// return [=](auto b) {
769*67e74705SXin Li /// f(x, a); // we may or may not need to capture 'x'
770*67e74705SXin Li /// };
771*67e74705SXin Li /// };
772*67e74705SXin Li /// }
addPotentialCapture(Expr * VarExpr)773*67e74705SXin Li void addPotentialCapture(Expr *VarExpr) {
774*67e74705SXin Li assert(isa<DeclRefExpr>(VarExpr) || isa<MemberExpr>(VarExpr));
775*67e74705SXin Li PotentiallyCapturingExprs.push_back(VarExpr);
776*67e74705SXin Li }
777*67e74705SXin Li
addPotentialThisCapture(SourceLocation Loc)778*67e74705SXin Li void addPotentialThisCapture(SourceLocation Loc) {
779*67e74705SXin Li PotentialThisCaptureLocation = Loc;
780*67e74705SXin Li }
hasPotentialThisCapture()781*67e74705SXin Li bool hasPotentialThisCapture() const {
782*67e74705SXin Li return PotentialThisCaptureLocation.isValid();
783*67e74705SXin Li }
784*67e74705SXin Li
785*67e74705SXin Li /// \brief Mark a variable's reference in a lambda as non-odr using.
786*67e74705SXin Li ///
787*67e74705SXin Li /// For generic lambdas, if a variable is named in a potentially evaluated
788*67e74705SXin Li /// expression, where the enclosing full expression is dependent then we
789*67e74705SXin Li /// must capture the variable (given a default capture).
790*67e74705SXin Li /// This is accomplished by recording all references to variables
791*67e74705SXin Li /// (DeclRefExprs or MemberExprs) within said nested lambda in its array of
792*67e74705SXin Li /// PotentialCaptures. All such variables have to be captured by that lambda,
793*67e74705SXin Li /// except for as described below.
794*67e74705SXin Li /// If that variable is usable as a constant expression and is named in a
795*67e74705SXin Li /// manner that does not involve its odr-use (e.g. undergoes
796*67e74705SXin Li /// lvalue-to-rvalue conversion, or discarded) record that it is so. Upon the
797*67e74705SXin Li /// act of analyzing the enclosing full expression (ActOnFinishFullExpr)
798*67e74705SXin Li /// if we can determine that the full expression is not instantiation-
799*67e74705SXin Li /// dependent, then we can entirely avoid its capture.
800*67e74705SXin Li ///
801*67e74705SXin Li /// const int n = 0;
802*67e74705SXin Li /// [&] (auto x) {
803*67e74705SXin Li /// (void)+n + x;
804*67e74705SXin Li /// };
805*67e74705SXin Li /// Interestingly, this strategy would involve a capture of n, even though
806*67e74705SXin Li /// it's obviously not odr-used here, because the full-expression is
807*67e74705SXin Li /// instantiation-dependent. It could be useful to avoid capturing such
808*67e74705SXin Li /// variables, even when they are referred to in an instantiation-dependent
809*67e74705SXin Li /// expression, if we can unambiguously determine that they shall never be
810*67e74705SXin Li /// odr-used. This would involve removal of the variable-referring-expression
811*67e74705SXin Li /// from the array of PotentialCaptures during the lvalue-to-rvalue
812*67e74705SXin Li /// conversions. But per the working draft N3797, (post-chicago 2013) we must
813*67e74705SXin Li /// capture such variables.
814*67e74705SXin Li /// Before anyone is tempted to implement a strategy for not-capturing 'n',
815*67e74705SXin Li /// consider the insightful warning in:
816*67e74705SXin Li /// /cfe-commits/Week-of-Mon-20131104/092596.html
817*67e74705SXin Li /// "The problem is that the set of captures for a lambda is part of the ABI
818*67e74705SXin Li /// (since lambda layout can be made visible through inline functions and the
819*67e74705SXin Li /// like), and there are no guarantees as to which cases we'll manage to build
820*67e74705SXin Li /// an lvalue-to-rvalue conversion in, when parsing a template -- some
821*67e74705SXin Li /// seemingly harmless change elsewhere in Sema could cause us to start or stop
822*67e74705SXin Li /// building such a node. So we need a rule that anyone can implement and get
823*67e74705SXin Li /// exactly the same result".
824*67e74705SXin Li ///
markVariableExprAsNonODRUsed(Expr * CapturingVarExpr)825*67e74705SXin Li void markVariableExprAsNonODRUsed(Expr *CapturingVarExpr) {
826*67e74705SXin Li assert(isa<DeclRefExpr>(CapturingVarExpr)
827*67e74705SXin Li || isa<MemberExpr>(CapturingVarExpr));
828*67e74705SXin Li NonODRUsedCapturingExprs.insert(CapturingVarExpr);
829*67e74705SXin Li }
isVariableExprMarkedAsNonODRUsed(Expr * CapturingVarExpr)830*67e74705SXin Li bool isVariableExprMarkedAsNonODRUsed(Expr *CapturingVarExpr) const {
831*67e74705SXin Li assert(isa<DeclRefExpr>(CapturingVarExpr)
832*67e74705SXin Li || isa<MemberExpr>(CapturingVarExpr));
833*67e74705SXin Li return NonODRUsedCapturingExprs.count(CapturingVarExpr);
834*67e74705SXin Li }
removePotentialCapture(Expr * E)835*67e74705SXin Li void removePotentialCapture(Expr *E) {
836*67e74705SXin Li PotentiallyCapturingExprs.erase(
837*67e74705SXin Li std::remove(PotentiallyCapturingExprs.begin(),
838*67e74705SXin Li PotentiallyCapturingExprs.end(), E),
839*67e74705SXin Li PotentiallyCapturingExprs.end());
840*67e74705SXin Li }
clearPotentialCaptures()841*67e74705SXin Li void clearPotentialCaptures() {
842*67e74705SXin Li PotentiallyCapturingExprs.clear();
843*67e74705SXin Li PotentialThisCaptureLocation = SourceLocation();
844*67e74705SXin Li }
getNumPotentialVariableCaptures()845*67e74705SXin Li unsigned getNumPotentialVariableCaptures() const {
846*67e74705SXin Li return PotentiallyCapturingExprs.size();
847*67e74705SXin Li }
848*67e74705SXin Li
hasPotentialCaptures()849*67e74705SXin Li bool hasPotentialCaptures() const {
850*67e74705SXin Li return getNumPotentialVariableCaptures() ||
851*67e74705SXin Li PotentialThisCaptureLocation.isValid();
852*67e74705SXin Li }
853*67e74705SXin Li
854*67e74705SXin Li // When passed the index, returns the VarDecl and Expr associated
855*67e74705SXin Li // with the index.
856*67e74705SXin Li void getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, Expr *&E) const;
857*67e74705SXin Li };
858*67e74705SXin Li
WeakObjectProfileTy()859*67e74705SXin Li FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy()
860*67e74705SXin Li : Base(nullptr, false), Property(nullptr) {}
861*67e74705SXin Li
862*67e74705SXin Li FunctionScopeInfo::WeakObjectProfileTy
getSentinel()863*67e74705SXin Li FunctionScopeInfo::WeakObjectProfileTy::getSentinel() {
864*67e74705SXin Li FunctionScopeInfo::WeakObjectProfileTy Result;
865*67e74705SXin Li Result.Base.setInt(true);
866*67e74705SXin Li return Result;
867*67e74705SXin Li }
868*67e74705SXin Li
869*67e74705SXin Li template <typename ExprT>
recordUseOfWeak(const ExprT * E,bool IsRead)870*67e74705SXin Li void FunctionScopeInfo::recordUseOfWeak(const ExprT *E, bool IsRead) {
871*67e74705SXin Li assert(E);
872*67e74705SXin Li WeakUseVector &Uses = WeakObjectUses[WeakObjectProfileTy(E)];
873*67e74705SXin Li Uses.push_back(WeakUseTy(E, IsRead));
874*67e74705SXin Li }
875*67e74705SXin Li
876*67e74705SXin Li inline void
addThisCapture(bool isNested,SourceLocation Loc,Expr * Cpy,const bool ByCopy)877*67e74705SXin Li CapturingScopeInfo::addThisCapture(bool isNested, SourceLocation Loc,
878*67e74705SXin Li Expr *Cpy,
879*67e74705SXin Li const bool ByCopy) {
880*67e74705SXin Li Captures.push_back(Capture(Capture::ThisCapture, isNested, Loc, QualType(),
881*67e74705SXin Li Cpy, ByCopy));
882*67e74705SXin Li CXXThisCaptureIndex = Captures.size();
883*67e74705SXin Li }
884*67e74705SXin Li
885*67e74705SXin Li } // end namespace sema
886*67e74705SXin Li } // end namespace clang
887*67e74705SXin Li
888*67e74705SXin Li #endif
889