Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 1 | //===--- ScopeInfo.cpp - Information about a semantic context -------------===// |
| 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 implements FunctionScopeInfo and its subclasses, which contain |
| 11 | // information about a single function, block, lambda, or method body. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Sema/ScopeInfo.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/ExprCXX.h" |
| 20 | #include "clang/AST/ExprObjC.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace sema; |
| 24 | |
| 25 | void FunctionScopeInfo::Clear() { |
| 26 | HasBranchProtectedScope = false; |
| 27 | HasBranchIntoScope = false; |
| 28 | HasIndirectGoto = false; |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 29 | HasDroppedStmt = false; |
| 30 | ObjCShouldCallSuper = false; |
| 31 | ObjCIsDesignatedInit = false; |
| 32 | ObjCWarnForNoDesignatedInitChain = false; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 33 | ObjCIsSecondaryInit = false; |
| 34 | ObjCWarnForNoInitDelegation = false; |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 35 | |
| 36 | SwitchStack.clear(); |
| 37 | Returns.clear(); |
| 38 | ErrorTrap.reset(); |
| 39 | PossiblyUnreachableDiags.clear(); |
| 40 | WeakObjectUses.clear(); |
| 41 | } |
| 42 | |
| 43 | static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) { |
| 44 | if (PropE->isExplicitProperty()) |
| 45 | return PropE->getExplicitProperty(); |
| 46 | |
| 47 | return PropE->getImplicitPropertyGetter(); |
| 48 | } |
| 49 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 50 | FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy |
| 51 | FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) { |
| 52 | E = E->IgnoreParenCasts(); |
| 53 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 54 | const NamedDecl *D = nullptr; |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 55 | bool IsExact = false; |
| 56 | |
| 57 | switch (E->getStmtClass()) { |
| 58 | case Stmt::DeclRefExprClass: |
| 59 | D = cast<DeclRefExpr>(E)->getDecl(); |
| 60 | IsExact = isa<VarDecl>(D); |
| 61 | break; |
| 62 | case Stmt::MemberExprClass: { |
| 63 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 64 | D = ME->getMemberDecl(); |
| 65 | IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 66 | break; |
| 67 | } |
| 68 | case Stmt::ObjCIvarRefExprClass: { |
| 69 | const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E); |
| 70 | D = IE->getDecl(); |
Anna Zaks | 97c7ce3 | 2012-10-01 20:34:04 +0000 | [diff] [blame] | 71 | IsExact = IE->getBase()->isObjCSelfExpr(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 72 | break; |
| 73 | } |
| 74 | case Stmt::PseudoObjectExprClass: { |
| 75 | const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E); |
| 76 | const ObjCPropertyRefExpr *BaseProp = |
| 77 | dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm()); |
| 78 | if (BaseProp) { |
| 79 | D = getBestPropertyDecl(BaseProp); |
| 80 | |
| 81 | const Expr *DoubleBase = BaseProp->getBase(); |
| 82 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase)) |
| 83 | DoubleBase = OVE->getSourceExpr(); |
| 84 | |
Anna Zaks | 97c7ce3 | 2012-10-01 20:34:04 +0000 | [diff] [blame] | 85 | IsExact = DoubleBase->isObjCSelfExpr(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 86 | } |
| 87 | break; |
| 88 | } |
| 89 | default: |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | return BaseInfoTy(D, IsExact); |
| 94 | } |
| 95 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 96 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 97 | const ObjCPropertyRefExpr *PropE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 98 | : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) { |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 99 | |
| 100 | if (PropE->isObjectReceiver()) { |
| 101 | const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase()); |
| 102 | const Expr *E = OVE->getSourceExpr(); |
| 103 | Base = getBaseInfo(E); |
| 104 | } else if (PropE->isClassReceiver()) { |
| 105 | Base.setPointer(PropE->getClassReceiver()); |
| 106 | } else { |
| 107 | assert(PropE->isSuperReceiver()); |
| 108 | } |
| 109 | } |
| 110 | |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 111 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE, |
| 112 | const ObjCPropertyDecl *Prop) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 113 | : Base(nullptr, true), Property(Prop) { |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 114 | if (BaseE) |
| 115 | Base = getBaseInfo(BaseE); |
| 116 | // else, this is a message accessing a property on super. |
| 117 | } |
| 118 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 119 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 120 | const DeclRefExpr *DRE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 121 | : Base(nullptr, true), Property(DRE->getDecl()) { |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 122 | assert(isa<VarDecl>(Property)); |
| 123 | } |
| 124 | |
| 125 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 126 | const ObjCIvarRefExpr *IvarE) |
| 127 | : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) { |
| 128 | } |
| 129 | |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 130 | void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg, |
| 131 | const ObjCPropertyDecl *Prop) { |
| 132 | assert(Msg && Prop); |
| 133 | WeakUseVector &Uses = |
| 134 | WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)]; |
| 135 | Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0)); |
| 136 | } |
| 137 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 138 | void FunctionScopeInfo::markSafeWeakUse(const Expr *E) { |
Jordan Rose | e723a27 | 2012-10-10 16:43:06 +0000 | [diff] [blame] | 139 | E = E->IgnoreParenCasts(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 140 | |
| 141 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
| 142 | markSafeWeakUse(POE->getSyntacticForm()); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) { |
| 147 | markSafeWeakUse(Cond->getTrueExpr()); |
| 148 | markSafeWeakUse(Cond->getFalseExpr()); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | if (const BinaryConditionalOperator *Cond = |
| 153 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 154 | markSafeWeakUse(Cond->getCommon()); |
| 155 | markSafeWeakUse(Cond->getFalseExpr()); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | // Has this weak object been seen before? |
| 160 | FunctionScopeInfo::WeakObjectUseMap::iterator Uses; |
Fariborz Jahanian | 9277ff4 | 2014-06-17 23:35:13 +0000 | [diff] [blame^] | 161 | if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) { |
| 162 | if (isa<OpaqueValueExpr>(RefExpr->getBase())) |
| 163 | Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr)); |
| 164 | else { |
| 165 | markSafeWeakUse(RefExpr->getBase()); |
| 166 | return; |
| 167 | } |
| 168 | } |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 169 | else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E)) |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 170 | Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE)); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 171 | else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 172 | Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE)); |
Jordan Rose | b1e3e5f | 2012-10-11 17:02:00 +0000 | [diff] [blame] | 173 | else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) { |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 174 | Uses = WeakObjectUses.end(); |
| 175 | if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) { |
| 176 | if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) { |
| 177 | Uses = |
| 178 | WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(), |
| 179 | Prop)); |
| 180 | } |
| 181 | } |
| 182 | } |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 183 | else |
| 184 | return; |
| 185 | |
| 186 | if (Uses == WeakObjectUses.end()) |
| 187 | return; |
| 188 | |
| 189 | // Has there been a read from the object using this Expr? |
| 190 | FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse = |
| 191 | std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true)); |
| 192 | if (ThisUse == Uses->second.rend()) |
| 193 | return; |
| 194 | |
| 195 | ThisUse->markSafe(); |
| 196 | } |
| 197 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 198 | void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, |
| 199 | Expr *&E) const { |
Faisal Vali | 9366a48 | 2013-11-07 16:57:56 +0000 | [diff] [blame] | 200 | assert(Idx < getNumPotentialVariableCaptures() && |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 201 | "Index of potential capture must be within 0 to less than the " |
| 202 | "number of captures!"); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 203 | E = PotentiallyCapturingExprs[Idx]; |
| 204 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 205 | VD = dyn_cast<VarDecl>(DRE->getFoundDecl()); |
| 206 | else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 207 | VD = dyn_cast<VarDecl>(ME->getMemberDecl()); |
| 208 | else |
| 209 | llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for " |
| 210 | "potential captures"); |
| 211 | assert(VD); |
| 212 | } |
| 213 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 214 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 215 | BlockScopeInfo::~BlockScopeInfo() { } |
| 216 | LambdaScopeInfo::~LambdaScopeInfo() { } |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 217 | CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { } |