ParentMap.cpp revision 344779
1//===--- ParentMap.cpp - Mappings from Stmts to their Parents ---*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the ParentMap class. 11// 12//===----------------------------------------------------------------------===// 13 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" 20 21using namespace clang; 22 23typedef llvm::DenseMap<Stmt*, Stmt*> MapTy; 24 25enum OpaqueValueMode { 26 OV_Transparent, 27 OV_Opaque 28}; 29 30static void BuildParentMap(MapTy& M, Stmt* S, 31 OpaqueValueMode OVMode = OV_Transparent) { 32 if (!S) 33 return; 34 35 switch (S->getStmtClass()) { 36 case Stmt::PseudoObjectExprClass: { 37 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); 38 PseudoObjectExpr *POE = cast<PseudoObjectExpr>(S); 39 40 // If we are rebuilding the map, clear out any existing state. 41 if (M[POE->getSyntacticForm()]) 42 for (Stmt *SubStmt : S->children()) 43 M[SubStmt] = nullptr; 44 45 M[POE->getSyntacticForm()] = S; 46 BuildParentMap(M, POE->getSyntacticForm(), OV_Transparent); 47 48 for (PseudoObjectExpr::semantics_iterator I = POE->semantics_begin(), 49 E = POE->semantics_end(); 50 I != E; ++I) { 51 M[*I] = S; 52 BuildParentMap(M, *I, OV_Opaque); 53 } 54 break; 55 } 56 case Stmt::BinaryConditionalOperatorClass: { 57 assert(OVMode == OV_Transparent && "Should not appear alongside OVEs"); 58 BinaryConditionalOperator *BCO = cast<BinaryConditionalOperator>(S); 59 60 M[BCO->getCommon()] = S; 61 BuildParentMap(M, BCO->getCommon(), OV_Transparent); 62 63 M[BCO->getCond()] = S; 64 BuildParentMap(M, BCO->getCond(), OV_Opaque); 65 66 M[BCO->getTrueExpr()] = S; 67 BuildParentMap(M, BCO->getTrueExpr(), OV_Opaque); 68 69 M[BCO->getFalseExpr()] = S; 70 BuildParentMap(M, BCO->getFalseExpr(), OV_Transparent); 71 72 break; 73 } 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); 84 } 85 break; 86 } 87 default: 88 for (Stmt *SubStmt : S->children()) { 89 if (SubStmt) { 90 M[SubStmt] = S; 91 BuildParentMap(M, SubStmt, OVMode); 92 } 93 } 94 break; 95 } 96} 97 98ParentMap::ParentMap(Stmt *S) : Impl(nullptr) { 99 if (S) { 100 MapTy *M = new MapTy(); 101 BuildParentMap(*M, S); 102 Impl = M; 103 } 104} 105 106ParentMap::~ParentMap() { 107 delete (MapTy*) Impl; 108} 109 110void ParentMap::addStmt(Stmt* S) { 111 if (S) { 112 BuildParentMap(*(MapTy*) Impl, S); 113 } 114} 115 116void ParentMap::setParent(const Stmt *S, const Stmt *Parent) { 117 assert(S); 118 assert(Parent); 119 MapTy *M = reinterpret_cast<MapTy *>(Impl); 120 M->insert(std::make_pair(const_cast<Stmt *>(S), const_cast<Stmt *>(Parent))); 121} 122 123Stmt* 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; 127} 128 129Stmt *ParentMap::getParentIgnoreParens(Stmt *S) const { 130 do { S = getParent(S); } while (S && isa<ParenExpr>(S)); 131 return S; 132} 133 134Stmt *ParentMap::getParentIgnoreParenCasts(Stmt *S) const { 135 do { 136 S = getParent(S); 137 } 138 while (S && (isa<ParenExpr>(S) || isa<CastExpr>(S))); 139 140 return S; 141} 142 143Stmt *ParentMap::getParentIgnoreParenImpCasts(Stmt *S) const { 144 do { 145 S = getParent(S); 146 } while (S && isa<Expr>(S) && cast<Expr>(S)->IgnoreParenImpCasts() != S); 147 148 return S; 149} 150 151Stmt *ParentMap::getOuterParenParent(Stmt *S) const { 152 Stmt *Paren = nullptr; 153 while (isa<ParenExpr>(S)) { 154 Paren = S; 155 S = getParent(S); 156 }; 157 return Paren; 158} 159 160bool ParentMap::isConsumedExpr(Expr* E) const { 161 Stmt *P = getParent(E); 162 Stmt *DirectChild = E; 163 164 // Ignore parents that don't guarantee consumption. 165 while (P && (isa<ParenExpr>(P) || isa<CastExpr>(P) || 166 isa<FullExpr>(P))) { 167 DirectChild = P; 168 P = getParent(P); 169 } 170 171 if (!P) 172 return false; 173 174 switch (P->getStmtClass()) { 175 default: 176 return isa<Expr>(P); 177 case Stmt::DeclStmtClass: 178 return true; 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(); 184 } 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: 200 return true; 201 } 202} 203 204