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