1//===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
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 the ParentMap class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/ParentMap.h"
14#include "clang/AST/Decl.h"
15#include "clang/AST/Expr.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/AST/StmtObjC.h"
18#include "llvm/ADT/DenseMap.h"
19
20using namespace clang;
21
22typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
23
24enum OpaqueValueMode {
25 OV_Transparent,
26 OV_Opaque
27};
28
29static void BuildParentMap(MapTy& M, Stmt* S,
30 OpaqueValueMode OVMode = OV_Transparent) {
31 if (!S)
32 return;
33
34 switch (S->getStmtClass()) {
35 case Stmt::PseudoObjectExprClass: {
36 PseudoObjectExpr *POE = cast<PseudoObjectExpr>(Val: S);
37
38 if (OVMode == OV_Opaque && M[POE->getSyntacticForm()])
39 break;
40
41 // If we are rebuilding the map, clear out any existing state.
42 if (M[POE->getSyntacticForm()])
43 for (Stmt *SubStmt : S->children())
44 M[SubStmt] = nullptr;
45
46 M[POE->getSyntacticForm()] = S;
47 BuildParentMap(M, S: POE->getSyntacticForm(), OVMode: OV_Transparent);
48
49 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
50 E = POE->semantics_end();
51 I != E; ++I) {
52 M[*I] = S;
53 BuildParentMap(M, S: *I, OVMode: OV_Opaque);
54 }
55 break;
56 }
57 case Stmt::BinaryConditionalOperatorClass: {
58 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
59 BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(Val: S);
60
61 M[BCO->getCommon()] = S;
62 BuildParentMap(M, S: BCO->getCommon(), OVMode: OV_Transparent);
63
64 M[BCO->getCond()] = S;
65 BuildParentMap(M, S: BCO->getCond(), OVMode: OV_Opaque);
66
67 M[BCO->getTrueExpr()] = S;
68 BuildParentMap(M, S: BCO->getTrueExpr(), OVMode: OV_Opaque);
69
70 M[BCO->getFalseExpr()] = S;
71 BuildParentMap(M, S: BCO->getFalseExpr(), OVMode: OV_Transparent);
72
73 break;
74 }
75 case Stmt::OpaqueValueExprClass: {
76 // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
77 // share a single source expression, but in the AST a single
78 // OpaqueValueExpr is shared among multiple parent expressions.
79 // The right thing to do is to give the OpaqueValueExpr its syntactic
80 // parent, then not reassign that when traversing the semantic expressions.
81 OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(Val: S);
82 if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
83 M[OVE->getSourceExpr()] = S;
84 BuildParentMap(M, S: OVE->getSourceExpr(), OVMode: OV_Transparent);
85 }
86 break;
87 }
88 case Stmt::CapturedStmtClass:
89 for (Stmt *SubStmt : S->children()) {
90 if (SubStmt) {
91 M[SubStmt] = S;
92 BuildParentMap(M, S: SubStmt, OVMode);
93 }
94 }
95 if (Stmt *SubStmt = cast<CapturedStmt>(Val: S)->getCapturedStmt()) {
96 M[SubStmt] = S;
97 BuildParentMap(M, S: SubStmt, OVMode);
98 }
99 break;
100 default:
101 for (Stmt *SubStmt : S->children()) {
102 if (SubStmt) {
103 M[SubStmt] = S;
104 BuildParentMap(M, S: SubStmt, OVMode);
105 }
106 }
107 break;
108 }
109}
110
111ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
112 if (S) {
113 MapTy *M = new MapTy();
114 BuildParentMap(M&: *M, S);
115 Impl = M;
116 }
117}
118
119ParentMap::~ParentMap() {
120 delete (MapTy*) Impl;
121}
122
123void ParentMap::addStmt(Stmt* S) {
124 if (S) {
125 BuildParentMap(M&: *(MapTy*) Impl, S);
126 }
127}
128
129void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
130 assert(S);
131 assert(Parent);
132 MapTy *M = reinterpret_cast<MapTy *>(Impl);
133 M->insert(KV: std::make_pair(x: const_cast<Stmt *>(S), y: const_cast<Stmt *>(Parent)));
134}
135
136Stmt* ParentMap::getParent(Stmt* S) const {
137 MapTy* M = (MapTy*) Impl;
138 return M->lookup(Val: S);
139}
140
141Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
142 do {
143 S = getParent(S);
144 } while (isa_and_nonnull<ParenExpr>(Val: S));
145 return S;
146}
147
148Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
149 do {
150 S = getParent(S);
151 }
152 while (S && (isa<ParenExpr>(Val: S) || isa<CastExpr>(Val: S)));
153
154 return S;
155}
156
157Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
158 do {
159 S = getParent(S);
160 } while (isa_and_nonnull<Expr>(Val: S) &&
161 cast<Expr>(Val: S)->IgnoreParenImpCasts() != S);
162
163 return S;
164}
165
166Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
167 Stmt *Paren = nullptr;
168 while (isa<ParenExpr>(Val: S)) {
169 Paren = S;
170 S = getParent(S);
171 };
172 return Paren;
173}
174
175bool ParentMap::isConsumedExpr(Expr* E) const {
176 Stmt *P = getParent(S: E);
177 Stmt *DirectChild = E;
178
179 // Ignore parents that don't guarantee consumption.
180 while (P && (isa<ParenExpr>(Val: P) || isa<CastExpr>(Val: P) ||
181 isa<FullExpr>(Val: P))) {
182 DirectChild = P;
183 P = getParent(S: P);
184 }
185
186 if (!P)
187 return false;
188
189 switch (P->getStmtClass()) {
190 default:
191 return isa<Expr>(Val: P);
192 case Stmt::DeclStmtClass:
193 return true;
194 case Stmt::BinaryOperatorClass: {
195 BinaryOperator *BE = cast<BinaryOperator>(Val: P);
196 // If it is a comma, only the right side is consumed.
197 // If it isn't a comma, both sides are consumed.
198 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
199 }
200 case Stmt::ForStmtClass:
201 return DirectChild == cast<ForStmt>(Val: P)->getCond();
202 case Stmt::WhileStmtClass:
203 return DirectChild == cast<WhileStmt>(Val: P)->getCond();
204 case Stmt::DoStmtClass:
205 return DirectChild == cast<DoStmt>(Val: P)->getCond();
206 case Stmt::IfStmtClass:
207 return DirectChild == cast<IfStmt>(Val: P)->getCond();
208 case Stmt::IndirectGotoStmtClass:
209 return DirectChild == cast<IndirectGotoStmt>(Val: P)->getTarget();
210 case Stmt::SwitchStmtClass:
211 return DirectChild == cast<SwitchStmt>(Val: P)->getCond();
212 case Stmt::ObjCForCollectionStmtClass:
213 return DirectChild == cast<ObjCForCollectionStmt>(Val: P)->getCollection();
214 case Stmt::ReturnStmtClass:
215 return true;
216 }
217}
218
219