xref: /freebsd/contrib/llvm-project/clang/include/clang/AST/IgnoreExpr.h (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1 //===--- IgnoreExpr.h - Ignore intermediate Expressions -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines common functions to ignore intermediate expression nodes
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_AST_IGNOREEXPR_H
14 #define LLVM_CLANG_AST_IGNOREEXPR_H
15 
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 
19 namespace clang {
20 namespace detail {
21 /// Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *,
22 /// Return Fn_n(...(Fn_1(E)))
IgnoreExprNodesImpl(Expr * E)23 inline Expr *IgnoreExprNodesImpl(Expr *E) { return E; }
24 template <typename FnTy, typename... FnTys>
IgnoreExprNodesImpl(Expr * E,FnTy && Fn,FnTys &&...Fns)25 Expr *IgnoreExprNodesImpl(Expr *E, FnTy &&Fn, FnTys &&... Fns) {
26   return IgnoreExprNodesImpl(std::forward<FnTy>(Fn)(E),
27                              std::forward<FnTys>(Fns)...);
28 }
29 } // namespace detail
30 
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.
IgnoreExprNodes(Expr * E,FnTys &&...Fns)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 
43 template <typename... FnTys>
IgnoreExprNodes(const Expr * E,FnTys &&...Fns)44 const Expr *IgnoreExprNodes(const Expr *E, FnTys &&...Fns) {
45   return IgnoreExprNodes(const_cast<Expr *>(E), std::forward<FnTys>(Fns)...);
46 }
47 
IgnoreImplicitCastsSingleStep(Expr * E)48 inline Expr *IgnoreImplicitCastsSingleStep(Expr *E) {
49   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
50     return ICE->getSubExpr();
51 
52   if (auto *FE = dyn_cast<FullExpr>(E))
53     return FE->getSubExpr();
54 
55   return E;
56 }
57 
IgnoreImplicitCastsExtraSingleStep(Expr * E)58 inline Expr *IgnoreImplicitCastsExtraSingleStep(Expr *E) {
59   // FIXME: Skip MaterializeTemporaryExpr and SubstNonTypeTemplateParmExpr in
60   // addition to what IgnoreImpCasts() skips to account for the current
61   // behaviour of IgnoreParenImpCasts().
62   Expr *SubE = IgnoreImplicitCastsSingleStep(E);
63   if (SubE != E)
64     return SubE;
65 
66   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
67     return MTE->getSubExpr();
68 
69   if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
70     return NTTP->getReplacement();
71 
72   return E;
73 }
74 
IgnoreCastsSingleStep(Expr * E)75 inline Expr *IgnoreCastsSingleStep(Expr *E) {
76   if (auto *CE = dyn_cast<CastExpr>(E))
77     return CE->getSubExpr();
78 
79   if (auto *FE = dyn_cast<FullExpr>(E))
80     return FE->getSubExpr();
81 
82   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
83     return MTE->getSubExpr();
84 
85   if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
86     return NTTP->getReplacement();
87 
88   return E;
89 }
90 
IgnoreLValueCastsSingleStep(Expr * E)91 inline Expr *IgnoreLValueCastsSingleStep(Expr *E) {
92   // Skip what IgnoreCastsSingleStep skips, except that only
93   // lvalue-to-rvalue casts are skipped.
94   if (auto *CE = dyn_cast<CastExpr>(E))
95     if (CE->getCastKind() != CK_LValueToRValue)
96       return E;
97 
98   return IgnoreCastsSingleStep(E);
99 }
100 
IgnoreBaseCastsSingleStep(Expr * E)101 inline Expr *IgnoreBaseCastsSingleStep(Expr *E) {
102   if (auto *CE = dyn_cast<CastExpr>(E))
103     if (CE->getCastKind() == CK_DerivedToBase ||
104         CE->getCastKind() == CK_UncheckedDerivedToBase ||
105         CE->getCastKind() == CK_NoOp)
106       return CE->getSubExpr();
107 
108   return E;
109 }
110 
IgnoreImplicitSingleStep(Expr * E)111 inline Expr *IgnoreImplicitSingleStep(Expr *E) {
112   Expr *SubE = IgnoreImplicitCastsSingleStep(E);
113   if (SubE != E)
114     return SubE;
115 
116   if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
117     return MTE->getSubExpr();
118 
119   if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
120     return BTE->getSubExpr();
121 
122   return E;
123 }
124 
IgnoreElidableImplicitConstructorSingleStep(Expr * E)125 inline Expr *IgnoreElidableImplicitConstructorSingleStep(Expr *E) {
126   auto *CCE = dyn_cast<CXXConstructExpr>(E);
127   if (CCE && CCE->isElidable() && !isa<CXXTemporaryObjectExpr>(CCE)) {
128     unsigned NumArgs = CCE->getNumArgs();
129     if ((NumArgs == 1 ||
130          (NumArgs > 1 && CCE->getArg(1)->isDefaultArgument())) &&
131         !CCE->getArg(0)->isDefaultArgument() && !CCE->isListInitialization())
132       return CCE->getArg(0);
133   }
134   return E;
135 }
136 
IgnoreImplicitAsWrittenSingleStep(Expr * E)137 inline Expr *IgnoreImplicitAsWrittenSingleStep(Expr *E) {
138   if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
139     return ICE->getSubExprAsWritten();
140 
141   return IgnoreImplicitSingleStep(E);
142 }
143 
IgnoreParensOnlySingleStep(Expr * E)144 inline Expr *IgnoreParensOnlySingleStep(Expr *E) {
145   if (auto *PE = dyn_cast<ParenExpr>(E))
146     return PE->getSubExpr();
147   return E;
148 }
149 
IgnoreParensSingleStep(Expr * E)150 inline Expr *IgnoreParensSingleStep(Expr *E) {
151   if (auto *PE = dyn_cast<ParenExpr>(E))
152     return PE->getSubExpr();
153 
154   if (auto *UO = dyn_cast<UnaryOperator>(E)) {
155     if (UO->getOpcode() == UO_Extension)
156       return UO->getSubExpr();
157   }
158 
159   else if (auto *GSE = dyn_cast<GenericSelectionExpr>(E)) {
160     if (!GSE->isResultDependent())
161       return GSE->getResultExpr();
162   }
163 
164   else if (auto *CE = dyn_cast<ChooseExpr>(E)) {
165     if (!CE->isConditionDependent())
166       return CE->getChosenSubExpr();
167   }
168 
169   else if (auto *PE = dyn_cast<PredefinedExpr>(E)) {
170     if (PE->isTransparent() && PE->getFunctionName())
171       return PE->getFunctionName();
172   }
173 
174   return E;
175 }
176 
177 } // namespace clang
178 
179 #endif // LLVM_CLANG_AST_IGNOREEXPR_H
180