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 "clang/AST/StmtObjC.h"
19 #include "llvm/ADT/DenseMap.h"
21 using namespace clang;
23 typedef llvm::DenseMap<Stmt*, Stmt*> MapTy;
25 enum OpaqueValueMode {
30 static void BuildParentMap(MapTy& M, Stmt* S,
31 OpaqueValueMode OVMode = OV_Transparent) {
35 switch (S->getStmtClass()) {
36 case Stmt::PseudoObjectExprClass: {
37 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
38 PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S);
40 // If we are rebuilding the map, clear out any existing state.
41 if (M[POE->getSyntacticForm()])
42 for (Stmt *SubStmt : S->children())
45 M[POE->getSyntacticForm()] = S;
46 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent);
48 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(),
49 E = POE->semantics_end();
52 BuildParentMap(M, *I, OV_Opaque);
56 case Stmt::BinaryConditionalOperatorClass: {
57 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs");
58 BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S);
60 M[BCO->getCommon()] = S;
61 BuildParentMap(M, BCO->getCommon(), OV_Transparent);
63 M[BCO->getCond()] = S;
64 BuildParentMap(M, BCO->getCond(), OV_Opaque);
66 M[BCO->getTrueExpr()] = S;
67 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque);
69 M[BCO->getFalseExpr()] = S;
70 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent);
74 case Stmt::OpaqueValueExprClass: {
75 // FIXME: This isn't correct; it assumes that multiple OpaqueValueExprs
76 // share a single source expression, but in the AST a single
77 // OpaqueValueExpr is shared among multiple parent expressions.
78 // The right thing to do is to give the OpaqueValueExpr its syntactic
79 // parent, then not reassign that when traversing the semantic expressions.
80 OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(S);
81 if (OVMode == OV_Transparent || !M[OVE->getSourceExpr()]) {
82 M[OVE->getSourceExpr()] = S;
83 BuildParentMap(M, OVE->getSourceExpr(), OV_Transparent);
88 for (Stmt *SubStmt : S->children()) {
91 BuildParentMap(M, SubStmt, OVMode);
98 ParentMap::ParentMap(Stmt *S) : Impl(nullptr) {
100 MapTy *M = new MapTy();
101 BuildParentMap(*M, S);
106 ParentMap::~ParentMap() {
107 delete (MapTy*) Impl;
110 void ParentMap::addStmt(Stmt* S) {
112 BuildParentMap(*(MapTy*) Impl, S);
116 void ParentMap::setParent(const Stmt *S, const Stmt *Parent) {
119 MapTy *M = reinterpret_cast<MapTy *>(Impl);
120 M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent)));
123 Stmt* ParentMap::getParent(Stmt* S) const {
124 MapTy* M = (MapTy*) Impl;
125 MapTy::iterator I = M->find(S);
126 return I == M->end() ? nullptr : I->second;
129 Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const {
130 do { S = getParent(S); } while (S && isa<ParenExpr>(S));
134 Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const {
138 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S)));
143 Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const {
146 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S);
151 Stmt *ParentMap::getOuterParenParent(Stmt *S) const {
152 Stmt *Paren = nullptr;
153 while (isa<ParenExpr>(S)) {
160 bool ParentMap::isConsumedExpr(Expr* E) const {
161 Stmt *P = getParent(E);
162 Stmt *DirectChild = E;
164 // Ignore parents that don't guarantee consumption.
165 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) ||
166 isa<ExprWithCleanups>(P))) {
174 switch (P->getStmtClass()) {
177 case Stmt::DeclStmtClass:
179 case Stmt::BinaryOperatorClass: {
180 BinaryOperator *BE = cast<BinaryOperator>(P);
181 // If it is a comma, only the right side is consumed.
182 // If it isn't a comma, both sides are consumed.
183 return BE->getOpcode()!=BO_Comma ||DirectChild==BE->getRHS();
185 case Stmt::ForStmtClass:
186 return DirectChild == cast<ForStmt>(P)->getCond();
187 case Stmt::WhileStmtClass:
188 return DirectChild == cast<WhileStmt>(P)->getCond();
189 case Stmt::DoStmtClass:
190 return DirectChild == cast<DoStmt>(P)->getCond();
191 case Stmt::IfStmtClass:
192 return DirectChild == cast<IfStmt>(P)->getCond();
193 case Stmt::IndirectGotoStmtClass:
194 return DirectChild == cast<IndirectGotoStmt>(P)->getTarget();
195 case Stmt::SwitchStmtClass:
196 return DirectChild == cast<SwitchStmt>(P)->getCond();
197 case Stmt::ObjCForCollectionStmtClass:
198 return DirectChild == cast<ObjCForCollectionStmt>(P)->getCollection();
199 case Stmt::ReturnStmtClass: