1 //===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ParentMap class.
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ParentMap.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/Expr.h"
17 #include "clang/AST/ExprCXX.h"
18 #include "llvm/ADT/DenseMap.h"
20 using namespace clang
;
22 typedef llvm::DenseMap
<Stmt
*, Stmt
*> MapTy
;
24 enum OpaqueValueMode
{
29 static void BuildParentMap(MapTy
& M
, Stmt
* S
,
30 OpaqueValueMode OVMode
= OV_Transparent
) {
32 switch (S
->getStmtClass()) {
33 case Stmt::PseudoObjectExprClass
: {
34 assert(OVMode
== OV_Transparent
&& "Should not appear alongside OVEs");
35 PseudoObjectExpr
*POE
= cast
<PseudoObjectExpr
>(S
);
37 // If we are rebuilding the map, clear out any existing state.
38 if (M
[POE
->getSyntacticForm()])
39 for (Stmt::child_range I
= S
->children(); I
; ++I
)
42 M
[POE
->getSyntacticForm()] = S
;
43 BuildParentMap(M
, POE
->getSyntacticForm(), OV_Transparent
);
45 for (PseudoObjectExpr::semantics_iterator I
= POE
->semantics_begin(),
46 E
= POE
->semantics_end();
49 BuildParentMap(M
, *I
, OV_Opaque
);
53 case Stmt::BinaryConditionalOperatorClass
: {
54 assert(OVMode
== OV_Transparent
&& "Should not appear alongside OVEs");
55 BinaryConditionalOperator
*BCO
= cast
<BinaryConditionalOperator
>(S
);
57 M
[BCO
->getCommon()] = S
;
58 BuildParentMap(M
, BCO
->getCommon(), OV_Transparent
);
60 M
[BCO
->getCond()] = S
;
61 BuildParentMap(M
, BCO
->getCond(), OV_Opaque
);
63 M
[BCO
->getTrueExpr()] = S
;
64 BuildParentMap(M
, BCO
->getTrueExpr(), OV_Opaque
);
66 M
[BCO
->getFalseExpr()] = S
;
67 BuildParentMap(M
, BCO
->getFalseExpr(), OV_Transparent
);
71 case Stmt::OpaqueValueExprClass
: {
72 // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
73 // share a single source expression, but in the AST a single
74 // OpaqueValueExpr is shared among multiple parent expressions.
75 // The right thing to do is to give the OpaqueValueExpr its syntactic
76 // parent, then not reassign that when traversing the semantic expressions.
77 OpaqueValueExpr
*OVE
= cast
<OpaqueValueExpr
>(S
);
78 if (OVMode
== OV_Transparent
|| !M
[OVE
->getSourceExpr()]) {
79 M
[OVE
->getSourceExpr()] = S
;
80 BuildParentMap(M
, OVE
->getSourceExpr(), OV_Transparent
);
85 for (Stmt::child_range I
= S
->children(); I
; ++I
) {
88 BuildParentMap(M
, *I
, OVMode
);
95 ParentMap::ParentMap(Stmt
*S
) : Impl(nullptr) {
97 MapTy
*M
= new MapTy();
98 BuildParentMap(*M
, S
);
103 ParentMap::~ParentMap() {
104 delete (MapTy
*) Impl
;
107 void ParentMap::addStmt(Stmt
* S
) {
109 BuildParentMap(*(MapTy
*) Impl
, S
);
113 void ParentMap::setParent(const Stmt
*S
, const Stmt
*Parent
) {
116 MapTy
*M
= reinterpret_cast<MapTy
*>(Impl
);
117 M
->insert(std::make_pair(const_cast<Stmt
*>(S
), const_cast<Stmt
*>(Parent
)));
120 Stmt
* ParentMap::getParent(Stmt
* S
) const {
121 MapTy
* M
= (MapTy
*) Impl
;
122 MapTy::iterator I
= M
->find(S
);
123 return I
== M
->end() ? nullptr : I
->second
;
126 Stmt
*ParentMap::getParentIgnoreParens(Stmt
*S
) const {
127 do { S
= getParent(S
); } while (S
&& isa
<ParenExpr
>(S
));
131 Stmt
*ParentMap::getParentIgnoreParenCasts(Stmt
*S
) const {
135 while (S
&& (isa
<ParenExpr
>(S
) || isa
<CastExpr
>(S
)));
140 Stmt
*ParentMap::getParentIgnoreParenImpCasts(Stmt
*S
) const {
143 } while (S
&& isa
<Expr
>(S
) && cast
<Expr
>(S
)->IgnoreParenImpCasts() != S
);
148 Stmt
*ParentMap::getOuterParenParent(Stmt
*S
) const {
149 Stmt
*Paren
= nullptr;
150 while (isa
<ParenExpr
>(S
)) {
157 bool ParentMap::isConsumedExpr(Expr
* E
) const {
158 Stmt
*P
= getParent(E
);
159 Stmt
*DirectChild
= E
;
161 // Ignore parents that don't guarantee consumption.
162 while (P
&& (isa
<ParenExpr
>(P
) || isa
<CastExpr
>(P
) ||
163 isa
<ExprWithCleanups
>(P
))) {
171 switch (P
->getStmtClass()) {
174 case Stmt::DeclStmtClass
:
176 case Stmt::BinaryOperatorClass
: {
177 BinaryOperator
*BE
= cast
<BinaryOperator
>(P
);
178 // If it is a comma, only the right side is consumed.
179 // If it isn't a comma, both sides are consumed.
180 return BE
->getOpcode()!=BO_Comma
||DirectChild
==BE
->getRHS();
182 case Stmt::ForStmtClass
:
183 return DirectChild
== cast
<ForStmt
>(P
)->getCond();
184 case Stmt::WhileStmtClass
:
185 return DirectChild
== cast
<WhileStmt
>(P
)->getCond();
186 case Stmt::DoStmtClass
:
187 return DirectChild
== cast
<DoStmt
>(P
)->getCond();
188 case Stmt::IfStmtClass
:
189 return DirectChild
== cast
<IfStmt
>(P
)->getCond();
190 case Stmt::IndirectGotoStmtClass
:
191 return DirectChild
== cast
<IndirectGotoStmt
>(P
)->getTarget();
192 case Stmt::SwitchStmtClass
:
193 return DirectChild
== cast
<SwitchStmt
>(P
)->getCond();
194 case Stmt::ReturnStmtClass
: