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