clang 22.0.0git
IgnoreExpr.h
Go to the documentation of this file.
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
19namespace clang {
20
21/// Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *,
22/// Recursively apply each of the functions to E until reaching a fixed point.
23/// Note that a null E is valid; in this case nothing is done.
24template <typename... FnTys> Expr *IgnoreExprNodes(Expr *E, FnTys &&... Fns) {
25 Expr *LastE = nullptr;
26 while (E != LastE) {
27 LastE = E;
28 ((E = std::forward<FnTys>(Fns)(E)), ...);
29 }
30 return E;
31}
32
33template <typename... FnTys>
34const Expr *IgnoreExprNodes(const Expr *E, FnTys &&...Fns) {
35 return IgnoreExprNodes(const_cast<Expr *>(E), std::forward<FnTys>(Fns)...);
36}
37
39 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
40 return ICE->getSubExpr();
41
42 if (auto *FE = dyn_cast<FullExpr>(E))
43 return FE->getSubExpr();
44
45 return E;
46}
47
49 // FIXME: Skip MaterializeTemporaryExpr and SubstNonTypeTemplateParmExpr in
50 // addition to what IgnoreImpCasts() skips to account for the current
51 // behaviour of IgnoreParenImpCasts().
53 if (SubE != E)
54 return SubE;
55
56 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
57 return MTE->getSubExpr();
58
59 if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
60 return NTTP->getReplacement();
61
62 return E;
63}
64
66 if (auto *CE = dyn_cast<CastExpr>(E))
67 return CE->getSubExpr();
68
69 if (auto *FE = dyn_cast<FullExpr>(E))
70 return FE->getSubExpr();
71
72 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
73 return MTE->getSubExpr();
74
75 if (auto *NTTP = dyn_cast<SubstNonTypeTemplateParmExpr>(E))
76 return NTTP->getReplacement();
77
78 return E;
79}
80
82 // Skip what IgnoreCastsSingleStep skips, except that only
83 // lvalue-to-rvalue casts are skipped.
84 if (auto *CE = dyn_cast<CastExpr>(E))
85 if (CE->getCastKind() != CK_LValueToRValue)
86 return E;
87
88 return IgnoreCastsSingleStep(E);
89}
90
92 if (auto *CE = dyn_cast<CastExpr>(E))
93 if (CE->getCastKind() == CK_DerivedToBase ||
94 CE->getCastKind() == CK_UncheckedDerivedToBase ||
95 CE->getCastKind() == CK_NoOp)
96 return CE->getSubExpr();
97
98 return E;
99}
100
103 if (SubE != E)
104 return SubE;
105
106 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
107 return MTE->getSubExpr();
108
109 if (auto *BTE = dyn_cast<CXXBindTemporaryExpr>(E))
110 return BTE->getSubExpr();
111
112 return E;
113}
114
116 auto *CCE = dyn_cast<CXXConstructExpr>(E);
117 if (CCE && CCE->isElidable() && !isa<CXXTemporaryObjectExpr>(CCE)) {
118 unsigned NumArgs = CCE->getNumArgs();
119 if ((NumArgs == 1 ||
120 (NumArgs > 1 && CCE->getArg(1)->isDefaultArgument())) &&
121 !CCE->getArg(0)->isDefaultArgument() && !CCE->isListInitialization())
122 return CCE->getArg(0);
123 }
124 return E;
125}
126
128 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
129 return ICE->getSubExprAsWritten();
130
131 return IgnoreImplicitSingleStep(E);
132}
133
135 if (auto *PE = dyn_cast<ParenExpr>(E))
136 return PE->getSubExpr();
137 return E;
138}
139
141 if (auto *PE = dyn_cast<ParenExpr>(E))
142 return PE->getSubExpr();
143
144 if (auto *UO = dyn_cast<UnaryOperator>(E)) {
145 if (UO->getOpcode() == UO_Extension)
146 return UO->getSubExpr();
147 }
148
149 else if (auto *GSE = dyn_cast<GenericSelectionExpr>(E)) {
150 if (!GSE->isResultDependent())
151 return GSE->getResultExpr();
152 }
153
154 else if (auto *CE = dyn_cast<ChooseExpr>(E)) {
155 if (!CE->isConditionDependent())
156 return CE->getChosenSubExpr();
157 }
158
159 else if (auto *PE = dyn_cast<PredefinedExpr>(E)) {
160 if (PE->isTransparent() && PE->getFunctionName())
161 return PE->getFunctionName();
162 }
163
164 return E;
165}
166
167} // namespace clang
168
169#endif // LLVM_CLANG_AST_IGNOREEXPR_H
Defines the clang::Expr interface and subclasses for C++ expressions.
This represents one expression.
Definition Expr.h:112
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
Expr * IgnoreElidableImplicitConstructorSingleStep(Expr *E)
Definition IgnoreExpr.h:115
Expr * IgnoreExprNodes(Expr *E, FnTys &&... Fns)
Given an expression E and functions Fn_1,...,Fn_n : Expr * -> Expr *, Recursively apply each of the f...
Definition IgnoreExpr.h:24
Expr * IgnoreImplicitCastsExtraSingleStep(Expr *E)
Definition IgnoreExpr.h:48
Expr * IgnoreImplicitCastsSingleStep(Expr *E)
Definition IgnoreExpr.h:38
Expr * IgnoreImplicitSingleStep(Expr *E)
Definition IgnoreExpr.h:101
Expr * IgnoreParensSingleStep(Expr *E)
Definition IgnoreExpr.h:140
Expr * IgnoreImplicitAsWrittenSingleStep(Expr *E)
Definition IgnoreExpr.h:127
Expr * IgnoreCastsSingleStep(Expr *E)
Definition IgnoreExpr.h:65
Expr * IgnoreLValueCastsSingleStep(Expr *E)
Definition IgnoreExpr.h:81
Expr * IgnoreParensOnlySingleStep(Expr *E)
Definition IgnoreExpr.h:134
Expr * IgnoreBaseCastsSingleStep(Expr *E)
Definition IgnoreExpr.h:91