clang  3.7.0
LambdaCapture.h
Go to the documentation of this file.
1 //===--- LambdaCapture.h - Types for C++ Lambda Captures --------*- 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 /// \file
11 /// \brief Defines the LambdaCapture class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_AST_LAMBDACAPTURE_H
16 #define LLVM_CLANG_AST_LAMBDACAPTURE_H
17 
18 #include "clang/AST/Decl.h"
19 #include "clang/Basic/Lambda.h"
20 #include "llvm/ADT/PointerIntPair.h"
21 
22 namespace clang {
23 
24 /// \brief Describes the capture of a variable or of \c this, or of a
25 /// C++1y init-capture.
27  enum {
28  /// \brief Flag used by the Capture class to indicate that the given
29  /// capture was implicit.
30  Capture_Implicit = 0x01,
31 
32  /// \brief Flag used by the Capture class to indicate that the
33  /// given capture was by-copy.
34  ///
35  /// This includes the case of a non-reference init-capture.
36  Capture_ByCopy = 0x02
37  };
38 
39  llvm::PointerIntPair<Decl *, 2> DeclAndBits;
40  SourceLocation Loc;
41  SourceLocation EllipsisLoc;
42 
43  friend class ASTStmtReader;
44  friend class ASTStmtWriter;
45 
46 public:
47  /// \brief Create a new capture of a variable or of \c this.
48  ///
49  /// \param Loc The source location associated with this capture.
50  ///
51  /// \param Kind The kind of capture (this, byref, bycopy), which must
52  /// not be init-capture.
53  ///
54  /// \param Implicit Whether the capture was implicit or explicit.
55  ///
56  /// \param Var The local variable being captured, or null if capturing
57  /// \c this.
58  ///
59  /// \param EllipsisLoc The location of the ellipsis (...) for a
60  /// capture that is a pack expansion, or an invalid source
61  /// location to indicate that this is not a pack expansion.
63  VarDecl *Var = nullptr,
64  SourceLocation EllipsisLoc = SourceLocation());
65 
66  /// \brief Determine the kind of capture.
68 
69  /// \brief Determine whether this capture handles the C++ \c this
70  /// pointer.
71  bool capturesThis() const {
72  return (DeclAndBits.getPointer() == nullptr) &&
73  !(DeclAndBits.getInt() & Capture_ByCopy);
74  }
75 
76  /// \brief Determine whether this capture handles a variable.
77  bool capturesVariable() const {
78  return dyn_cast_or_null<VarDecl>(DeclAndBits.getPointer());
79  }
80 
81  /// \brief Determine whether this captures a variable length array bound
82  /// expression.
83  bool capturesVLAType() const {
84  return (DeclAndBits.getPointer() == nullptr) &&
85  (DeclAndBits.getInt() & Capture_ByCopy);
86  }
87 
88  /// \brief Retrieve the declaration of the local variable being
89  /// captured.
90  ///
91  /// This operation is only valid if this capture is a variable capture
92  /// (other than a capture of \c this).
94  assert(capturesVariable() && "No variable available for 'this' capture");
95  return cast<VarDecl>(DeclAndBits.getPointer());
96  }
97 
98  /// \brief Determine whether this was an implicit capture (not
99  /// written between the square brackets introducing the lambda).
100  bool isImplicit() const { return DeclAndBits.getInt() & Capture_Implicit; }
101 
102  /// \brief Determine whether this was an explicit capture (written
103  /// between the square brackets introducing the lambda).
104  bool isExplicit() const { return !isImplicit(); }
105 
106  /// \brief Retrieve the source location of the capture.
107  ///
108  /// For an explicit capture, this returns the location of the
109  /// explicit capture in the source. For an implicit capture, this
110  /// returns the location at which the variable or \c this was first
111  /// used.
112  SourceLocation getLocation() const { return Loc; }
113 
114  /// \brief Determine whether this capture is a pack expansion,
115  /// which captures a function parameter pack.
116  bool isPackExpansion() const { return EllipsisLoc.isValid(); }
117 
118  /// \brief Retrieve the location of the ellipsis for a capture
119  /// that is a pack expansion.
121  assert(isPackExpansion() && "No ellipsis location for a non-expansion");
122  return EllipsisLoc;
123  }
124 };
125 
126 } // end namespace clang
127 
128 #endif // LLVM_CLANG_AST_LAMBDACAPTURE_H
LambdaCapture(SourceLocation Loc, bool Implicit, LambdaCaptureKind Kind, VarDecl *Var=nullptr, SourceLocation EllipsisLoc=SourceLocation())
Create a new capture of a variable or of this.
Definition: ExprCXX.cpp:888
Describes the capture of a variable or of this, or of a C++1y init-capture.
Definition: LambdaCapture.h:26
SourceLocation getEllipsisLoc() const
Retrieve the location of the ellipsis for a capture that is a pack expansion.
LambdaCaptureKind
The different capture forms in a lambda introducer.
Definition: Lambda.h:34
VarDecl * getCapturedVar() const
Retrieve the declaration of the local variable being captured.
Definition: LambdaCapture.h:93
bool capturesVLAType() const
Determine whether this captures a variable length array bound expression.
Definition: LambdaCapture.h:83
bool isImplicit() const
Determine whether this was an implicit capture (not written between the square brackets introducing t...
SourceLocation getLocation() const
Retrieve the source location of the capture.
bool isExplicit() const
Determine whether this was an explicit capture (written between the square brackets introducing the l...
LambdaCaptureKind getCaptureKind() const
Determine the kind of capture.
Definition: ExprCXX.cpp:916
Kind
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
bool isValid() const
Return true if this is a valid SourceLocation object.
Defines several types used to describe C++ lambda expressions that are shared between the parser and ...
bool isPackExpansion() const
Determine whether this capture is a pack expansion, which captures a function parameter pack...
bool capturesThis() const
Determine whether this capture handles the C++ this pointer.
Definition: LambdaCapture.h:71
bool capturesVariable() const
Determine whether this capture handles a variable.
Definition: LambdaCapture.h:77