LambdaCapture.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- LambdaCapture.h - Types for C++ Lambda Captures --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// Defines the LambdaCapture class.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_LAMBDACAPTURE_H
  19. #define LLVM_CLANG_AST_LAMBDACAPTURE_H
  20. #include "clang/AST/Decl.h"
  21. #include "clang/Basic/Lambda.h"
  22. #include "llvm/ADT/PointerIntPair.h"
  23. namespace clang {
  24. /// Describes the capture of a variable or of \c this, or of a
  25. /// C++1y init-capture.
  26. class LambdaCapture {
  27. enum {
  28. /// Flag used by the Capture class to indicate that the given
  29. /// capture was implicit.
  30. Capture_Implicit = 0x01,
  31. /// Flag used by the Capture class to indicate that the
  32. /// given capture was by-copy.
  33. ///
  34. /// This includes the case of a non-reference init-capture.
  35. Capture_ByCopy = 0x02,
  36. /// Flag used by the Capture class to distinguish between a capture
  37. /// of '*this' and a capture of a VLA type.
  38. Capture_This = 0x04
  39. };
  40. // Decl could represent:
  41. // - a VarDecl* that represents the variable that was captured or the
  42. // init-capture.
  43. // - or, is a nullptr and Capture_This is set in Bits if this represents a
  44. // capture of '*this' by value or reference.
  45. // - or, is a nullptr and Capture_This is not set in Bits if this represents
  46. // a capture of a VLA type.
  47. llvm::PointerIntPair<Decl*, 3> DeclAndBits;
  48. SourceLocation Loc;
  49. SourceLocation EllipsisLoc;
  50. friend class ASTStmtReader;
  51. friend class ASTStmtWriter;
  52. public:
  53. /// Create a new capture of a variable or of \c this.
  54. ///
  55. /// \param Loc The source location associated with this capture.
  56. ///
  57. /// \param Kind The kind of capture (this, byref, bycopy), which must
  58. /// not be init-capture.
  59. ///
  60. /// \param Implicit Whether the capture was implicit or explicit.
  61. ///
  62. /// \param Var The local variable being captured, or null if capturing
  63. /// \c this.
  64. ///
  65. /// \param EllipsisLoc The location of the ellipsis (...) for a
  66. /// capture that is a pack expansion, or an invalid source
  67. /// location to indicate that this is not a pack expansion.
  68. LambdaCapture(SourceLocation Loc, bool Implicit, LambdaCaptureKind Kind,
  69. ValueDecl *Var = nullptr,
  70. SourceLocation EllipsisLoc = SourceLocation());
  71. /// Determine the kind of capture.
  72. LambdaCaptureKind getCaptureKind() const;
  73. /// Determine whether this capture handles the C++ \c this
  74. /// pointer.
  75. bool capturesThis() const {
  76. return DeclAndBits.getPointer() == nullptr &&
  77. (DeclAndBits.getInt() & Capture_This);
  78. }
  79. /// Determine whether this capture handles a variable.
  80. bool capturesVariable() const {
  81. return isa_and_nonnull<ValueDecl>(DeclAndBits.getPointer());
  82. }
  83. /// Determine whether this captures a variable length array bound
  84. /// expression.
  85. bool capturesVLAType() const {
  86. return DeclAndBits.getPointer() == nullptr &&
  87. !(DeclAndBits.getInt() & Capture_This);
  88. }
  89. /// Retrieve the declaration of the local variable being
  90. /// captured.
  91. ///
  92. /// This operation is only valid if this capture is a variable capture
  93. /// (other than a capture of \c this).
  94. ValueDecl *getCapturedVar() const {
  95. assert(capturesVariable() && "No variable available for capture");
  96. return static_cast<ValueDecl *>(DeclAndBits.getPointer());
  97. }
  98. /// Determine whether this was an implicit capture (not
  99. /// written between the square brackets introducing the lambda).
  100. bool isImplicit() const {
  101. return DeclAndBits.getInt() & Capture_Implicit;
  102. }
  103. /// Determine whether this was an explicit capture (written
  104. /// between the square brackets introducing the lambda).
  105. bool isExplicit() const { return !isImplicit(); }
  106. /// 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. /// Determine whether this capture is a pack expansion,
  114. /// which captures a function parameter pack.
  115. bool isPackExpansion() const { return EllipsisLoc.isValid(); }
  116. /// Retrieve the location of the ellipsis for a capture
  117. /// that is a pack expansion.
  118. SourceLocation getEllipsisLoc() const {
  119. assert(isPackExpansion() && "No ellipsis location for a non-expansion");
  120. return EllipsisLoc;
  121. }
  122. };
  123. } // end namespace clang
  124. #endif // LLVM_CLANG_AST_LAMBDACAPTURE_H
  125. #ifdef __GNUC__
  126. #pragma GCC diagnostic pop
  127. #endif