IgnoreExpr.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- IgnoreExpr.h - Ignore intermediate Expressions -----------------===//
  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. // This file defines common functions to ignore intermediate expression nodes
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_IGNOREEXPR_H
  18. #define LLVM_CLANG_AST_IGNOREEXPR_H
  19. #include "clang/AST/Expr.h"
  20. #include "clang/AST/ExprCXX.h"
  21. namespace clang {
  22. namespace detail {
  23. /// Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *,
  24. /// Return Fn_n(...(Fn_1(E)))
  25. inline Expr *IgnoreExprNodesImpl(Expr *E) { return E; }
  26. template <typename FnTy, typename... FnTys>
  27. Expr *IgnoreExprNodesImpl(Expr *E, FnTy &&Fn, FnTys &&... Fns) {
  28. return IgnoreExprNodesImpl(Fn(E), std::forward<FnTys>(Fns)...);
  29. }
  30. } // namespace detail
  31. /// Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *,
  32. /// Recursively apply each of the functions to E until reaching a fixed point.
  33. /// Note that a null E is valid; in this case nothing is done.
  34. template <typename... FnTys> Expr *IgnoreExprNodes(Expr *E, FnTys &&... Fns) {
  35. Expr *LastE = nullptr;
  36. while (E != LastE) {
  37. LastE = E;
  38. E = detail::IgnoreExprNodesImpl(E, std::forward<FnTys>(Fns)...);
  39. }
  40. return E;
  41. }
  42. template <typename... FnTys>
  43. const Expr *IgnoreExprNodes(const Expr *E, FnTys &&...Fns) {
  44. return IgnoreExprNodes(const_cast<Expr *>(E), std::forward<FnTys>(Fns)...);
  45. }
  46. inline Expr *IgnoreImplicitCastsSingleStep(Expr *E) {
  47. if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
  48. return ICE->getSubExpr();
  49. if (auto *FE = dyn_cast<FullExpr>(E))
  50. return FE->getSubExpr();
  51. return E;
  52. }
  53. inline Expr *IgnoreImplicitCastsExtraSingleStep(Expr *E) {
  54. // FIXME: Skip MaterializeTemporaryExpr and SubstNonTypeTemplateParmExpr in
  55. // addition to what IgnoreImpCasts() skips to account for the current
  56. // behaviour of IgnoreParenImpCasts().
  57. Expr *SubE = IgnoreImplicitCastsSingleStep(E);
  58. if (SubE != E)
  59. return SubE;
  60. if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
  61. return MTE->getSubExpr();
  62. if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
  63. return NTTP->getReplacement();
  64. return E;
  65. }
  66. inline Expr *IgnoreCastsSingleStep(Expr *E) {
  67. if (auto *CE = dyn_cast<CastExpr>(E))
  68. return CE->getSubExpr();
  69. if (auto *FE = dyn_cast<FullExpr>(E))
  70. return FE->getSubExpr();
  71. if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
  72. return MTE->getSubExpr();
  73. if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
  74. return NTTP->getReplacement();
  75. return E;
  76. }
  77. inline Expr *IgnoreLValueCastsSingleStep(Expr *E) {
  78. // Skip what IgnoreCastsSingleStep skips, except that only
  79. // lvalue-to-rvalue casts are skipped.
  80. if (auto *CE = dyn_cast<CastExpr>(E))
  81. if (CE->getCastKind() != CK_LValueToRValue)
  82. return E;
  83. return IgnoreCastsSingleStep(E);
  84. }
  85. inline Expr *IgnoreBaseCastsSingleStep(Expr *E) {
  86. if (auto *CE = dyn_cast<CastExpr>(E))
  87. if (CE->getCastKind() == CK_DerivedToBase ||
  88. CE->getCastKind() == CK_UncheckedDerivedToBase ||
  89. CE->getCastKind() == CK_NoOp)
  90. return CE->getSubExpr();
  91. return E;
  92. }
  93. inline Expr *IgnoreImplicitSingleStep(Expr *E) {
  94. Expr *SubE = IgnoreImplicitCastsSingleStep(E);
  95. if (SubE != E)
  96. return SubE;
  97. if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
  98. return MTE->getSubExpr();
  99. if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
  100. return BTE->getSubExpr();
  101. return E;
  102. }
  103. inline Expr *IgnoreElidableImplicitConstructorSingleStep(Expr *E) {
  104. auto *CCE = dyn_cast<CXXConstructExpr>(E);
  105. if (CCE && CCE->isElidable() && !isa<CXXTemporaryObjectExpr>(CCE)) {
  106. unsigned NumArgs = CCE->getNumArgs();
  107. if ((NumArgs == 1 ||
  108. (NumArgs > 1 && CCE->getArg(1)->isDefaultArgument())) &&
  109. !CCE->getArg(0)->isDefaultArgument() && !CCE->isListInitialization())
  110. return CCE->getArg(0);
  111. }
  112. return E;
  113. }
  114. inline Expr *IgnoreImplicitAsWrittenSingleStep(Expr *E) {
  115. if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
  116. return ICE->getSubExprAsWritten();
  117. return IgnoreImplicitSingleStep(E);
  118. }
  119. inline Expr *IgnoreParensOnlySingleStep(Expr *E) {
  120. if (auto *PE = dyn_cast<ParenExpr>(E))
  121. return PE->getSubExpr();
  122. return E;
  123. }
  124. inline Expr *IgnoreParensSingleStep(Expr *E) {
  125. if (auto *PE = dyn_cast<ParenExpr>(E))
  126. return PE->getSubExpr();
  127. if (auto *UO = dyn_cast<UnaryOperator>(E)) {
  128. if (UO->getOpcode() == UO_Extension)
  129. return UO->getSubExpr();
  130. }
  131. else if (auto *GSE = dyn_cast<GenericSelectionExpr>(E)) {
  132. if (!GSE->isResultDependent())
  133. return GSE->getResultExpr();
  134. }
  135. else if (auto *CE = dyn_cast<ChooseExpr>(E)) {
  136. if (!CE->isConditionDependent())
  137. return CE->getChosenSubExpr();
  138. }
  139. return E;
  140. }
  141. } // namespace clang
  142. #endif // LLVM_CLANG_AST_IGNOREEXPR_H
  143. #ifdef __GNUC__
  144. #pragma GCC diagnostic pop
  145. #endif