clang API Documentation

LambdaCapture.h
Go to the documentation of this file.
00001 //===--- LambdaCapture.h - Types for C++ Lambda Captures --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Defines the LambdaCapture class.
00012 ///
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_AST_LAMBDACAPTURE_H
00016 #define LLVM_CLANG_AST_LAMBDACAPTURE_H
00017 
00018 #include "clang/AST/Decl.h"
00019 #include "clang/Basic/Lambda.h"
00020 #include "llvm/ADT/PointerIntPair.h"
00021 
00022 namespace clang {
00023 
00024 /// \brief Describes the capture of a variable or of \c this, or of a
00025 /// C++1y init-capture.
00026 class LambdaCapture {
00027   enum {
00028     /// \brief Flag used by the Capture class to indicate that the given
00029     /// capture was implicit.
00030     Capture_Implicit = 0x01,
00031 
00032     /// \brief Flag used by the Capture class to indicate that the
00033     /// given capture was by-copy.
00034     ///
00035     /// This includes the case of a non-reference init-capture.
00036     Capture_ByCopy = 0x02
00037   };
00038 
00039   llvm::PointerIntPair<Decl *, 2> DeclAndBits;
00040   SourceLocation Loc;
00041   SourceLocation EllipsisLoc;
00042 
00043   friend class ASTStmtReader;
00044   friend class ASTStmtWriter;
00045 
00046 public:
00047   /// \brief Create a new capture of a variable or of \c this.
00048   ///
00049   /// \param Loc The source location associated with this capture.
00050   ///
00051   /// \param Kind The kind of capture (this, byref, bycopy), which must
00052   /// not be init-capture.
00053   ///
00054   /// \param Implicit Whether the capture was implicit or explicit.
00055   ///
00056   /// \param Var The local variable being captured, or null if capturing
00057   /// \c this.
00058   ///
00059   /// \param EllipsisLoc The location of the ellipsis (...) for a
00060   /// capture that is a pack expansion, or an invalid source
00061   /// location to indicate that this is not a pack expansion.
00062   LambdaCapture(SourceLocation Loc, bool Implicit, LambdaCaptureKind Kind,
00063                 VarDecl *Var = nullptr,
00064                 SourceLocation EllipsisLoc = SourceLocation());
00065 
00066   /// \brief Determine the kind of capture.
00067   LambdaCaptureKind getCaptureKind() const;
00068 
00069   /// \brief Determine whether this capture handles the C++ \c this
00070   /// pointer.
00071   bool capturesThis() const {
00072     return (DeclAndBits.getPointer() == nullptr) &&
00073            !(DeclAndBits.getInt() & Capture_ByCopy);
00074   }
00075 
00076   /// \brief Determine whether this capture handles a variable.
00077   bool capturesVariable() const {
00078     return dyn_cast_or_null<VarDecl>(DeclAndBits.getPointer());
00079   }
00080 
00081   /// \brief Determine whether this captures a variable length array bound
00082   /// expression.
00083   bool capturesVLAType() const {
00084     return (DeclAndBits.getPointer() == nullptr) &&
00085            (DeclAndBits.getInt() & Capture_ByCopy);
00086   }
00087 
00088   /// \brief Determine whether this is an init-capture.
00089   bool isInitCapture() const {
00090     return capturesVariable() && getCapturedVar()->isInitCapture();
00091   }
00092 
00093   /// \brief Retrieve the declaration of the local variable being
00094   /// captured.
00095   ///
00096   /// This operation is only valid if this capture is a variable capture
00097   /// (other than a capture of \c this).
00098   VarDecl *getCapturedVar() const {
00099     assert(capturesVariable() && "No variable available for 'this' capture");
00100     return cast<VarDecl>(DeclAndBits.getPointer());
00101   }
00102 
00103   /// \brief Determine whether this was an implicit capture (not
00104   /// written between the square brackets introducing the lambda).
00105   bool isImplicit() const { return DeclAndBits.getInt() & Capture_Implicit; }
00106 
00107   /// \brief Determine whether this was an explicit capture (written
00108   /// between the square brackets introducing the lambda).
00109   bool isExplicit() const { return !isImplicit(); }
00110 
00111   /// \brief Retrieve the source location of the capture.
00112   ///
00113   /// For an explicit capture, this returns the location of the
00114   /// explicit capture in the source. For an implicit capture, this
00115   /// returns the location at which the variable or \c this was first
00116   /// used.
00117   SourceLocation getLocation() const { return Loc; }
00118 
00119   /// \brief Determine whether this capture is a pack expansion,
00120   /// which captures a function parameter pack.
00121   bool isPackExpansion() const { return EllipsisLoc.isValid(); }
00122 
00123   /// \brief Retrieve the location of the ellipsis for a capture
00124   /// that is a pack expansion.
00125   SourceLocation getEllipsisLoc() const {
00126     assert(isPackExpansion() && "No ellipsis location for a non-expansion");
00127     return EllipsisLoc;
00128   }
00129 };
00130 
00131 } // end namespace clang
00132 
00133 #endif // LLVM_CLANG_AST_LAMBDACAPTURE_H