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" |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
| 21 | #include "clang/AST/ExprObjC.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | using namespace sema; |
| 25 | |
| 26 | void FunctionScopeInfo::Clear() { |
| 27 | HasBranchProtectedScope = false; |
| 28 | HasBranchIntoScope = false; |
| 29 | HasIndirectGoto = false; |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 30 | HasDroppedStmt = false; |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 31 | HasOMPDeclareReductionCombiner = false; |
Erik Pilkington | 5dbe7a9 | 2016-11-09 22:52:23 +0000 | [diff] [blame] | 32 | HasFallthroughStmt = false; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 33 | HasPotentialAvailabilityViolations = false; |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 34 | ObjCShouldCallSuper = false; |
| 35 | ObjCIsDesignatedInit = false; |
| 36 | ObjCWarnForNoDesignatedInitChain = false; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 37 | ObjCIsSecondaryInit = false; |
| 38 | ObjCWarnForNoInitDelegation = false; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 39 | FirstReturnLoc = SourceLocation(); |
Reid Kleckner | e717591 | 2015-02-02 22:15:31 +0000 | [diff] [blame] | 40 | FirstCXXTryLoc = SourceLocation(); |
| 41 | FirstSEHTryLoc = SourceLocation(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 42 | |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 43 | // Coroutine state |
| 44 | FirstCoroutineStmtLoc = SourceLocation(); |
Richard Smith | dcd04d1 | 2015-10-27 07:47:45 +0000 | [diff] [blame] | 45 | CoroutinePromise = nullptr; |
Brian Gesiak | 61f4ac9 | 2018-01-24 22:15:42 +0000 | [diff] [blame] | 46 | CoroutineParameterMoves.clear(); |
Eric Fiselier | 20f25cb | 2017-03-06 23:38:15 +0000 | [diff] [blame] | 47 | NeedsCoroutineSuspends = true; |
| 48 | CoroutineSuspends.first = nullptr; |
| 49 | CoroutineSuspends.second = nullptr; |
Eric Fiselier | cac0a59 | 2017-03-11 02:35:37 +0000 | [diff] [blame] | 50 | |
| 51 | SwitchStack.clear(); |
| 52 | Returns.clear(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 53 | ErrorTrap.reset(); |
| 54 | PossiblyUnreachableDiags.clear(); |
| 55 | WeakObjectUses.clear(); |
Fariborz Jahanian | ef202d9 | 2014-11-18 21:57:54 +0000 | [diff] [blame] | 56 | ModifiedNonNullParams.clear(); |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame^] | 57 | Blocks.clear(); |
| 58 | ByrefBlockVars.clear(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) { |
| 62 | if (PropE->isExplicitProperty()) |
| 63 | return PropE->getExplicitProperty(); |
| 64 | |
| 65 | return PropE->getImplicitPropertyGetter(); |
| 66 | } |
| 67 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 68 | FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy |
| 69 | FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) { |
| 70 | E = E->IgnoreParenCasts(); |
| 71 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 72 | const NamedDecl *D = nullptr; |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 73 | bool IsExact = false; |
| 74 | |
| 75 | switch (E->getStmtClass()) { |
| 76 | case Stmt::DeclRefExprClass: |
| 77 | D = cast<DeclRefExpr>(E)->getDecl(); |
| 78 | IsExact = isa<VarDecl>(D); |
| 79 | break; |
| 80 | case Stmt::MemberExprClass: { |
| 81 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 82 | D = ME->getMemberDecl(); |
| 83 | IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 84 | break; |
| 85 | } |
| 86 | case Stmt::ObjCIvarRefExprClass: { |
| 87 | const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E); |
| 88 | D = IE->getDecl(); |
Anna Zaks | 97c7ce3 | 2012-10-01 20:34:04 +0000 | [diff] [blame] | 89 | IsExact = IE->getBase()->isObjCSelfExpr(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | case Stmt::PseudoObjectExprClass: { |
| 93 | const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E); |
| 94 | const ObjCPropertyRefExpr *BaseProp = |
| 95 | dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm()); |
| 96 | if (BaseProp) { |
| 97 | D = getBestPropertyDecl(BaseProp); |
| 98 | |
Akira Hatanaka | 7e2c82d | 2016-03-22 05:00:21 +0000 | [diff] [blame] | 99 | if (BaseProp->isObjectReceiver()) { |
Akira Hatanaka | 4c62c7c | 2016-03-18 19:03:50 +0000 | [diff] [blame] | 100 | const Expr *DoubleBase = BaseProp->getBase(); |
| 101 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase)) |
| 102 | DoubleBase = OVE->getSourceExpr(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 103 | |
Akira Hatanaka | 4c62c7c | 2016-03-18 19:03:50 +0000 | [diff] [blame] | 104 | IsExact = DoubleBase->isObjCSelfExpr(); |
| 105 | } |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 106 | } |
| 107 | break; |
| 108 | } |
| 109 | default: |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | return BaseInfoTy(D, IsExact); |
| 114 | } |
| 115 | |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 116 | bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const { |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 117 | RecordDecl *RD = nullptr; |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 118 | if (auto *LSI = dyn_cast<LambdaScopeInfo>(this)) |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 119 | RD = LSI->Lambda; |
| 120 | else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this)) |
| 121 | RD = CRSI->TheRecordDecl; |
| 122 | |
| 123 | if (RD) |
| 124 | for (auto *FD : RD->fields()) { |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 125 | if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT) |
| 126 | return true; |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 131 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 132 | const ObjCPropertyRefExpr *PropE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 133 | : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) { |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 134 | |
| 135 | if (PropE->isObjectReceiver()) { |
| 136 | const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase()); |
| 137 | const Expr *E = OVE->getSourceExpr(); |
| 138 | Base = getBaseInfo(E); |
| 139 | } else if (PropE->isClassReceiver()) { |
| 140 | Base.setPointer(PropE->getClassReceiver()); |
| 141 | } else { |
| 142 | assert(PropE->isSuperReceiver()); |
| 143 | } |
| 144 | } |
| 145 | |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 146 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE, |
| 147 | const ObjCPropertyDecl *Prop) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 148 | : Base(nullptr, true), Property(Prop) { |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 149 | if (BaseE) |
| 150 | Base = getBaseInfo(BaseE); |
| 151 | // else, this is a message accessing a property on super. |
| 152 | } |
| 153 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 154 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 155 | const DeclRefExpr *DRE) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 156 | : Base(nullptr, true), Property(DRE->getDecl()) { |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 157 | assert(isa<VarDecl>(Property)); |
| 158 | } |
| 159 | |
| 160 | FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( |
| 161 | const ObjCIvarRefExpr *IvarE) |
| 162 | : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) { |
| 163 | } |
| 164 | |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 165 | void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg, |
| 166 | const ObjCPropertyDecl *Prop) { |
| 167 | assert(Msg && Prop); |
| 168 | WeakUseVector &Uses = |
| 169 | WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)]; |
| 170 | Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0)); |
| 171 | } |
| 172 | |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 173 | void FunctionScopeInfo::markSafeWeakUse(const Expr *E) { |
Jordan Rose | e723a27 | 2012-10-10 16:43:06 +0000 | [diff] [blame] | 174 | E = E->IgnoreParenCasts(); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 175 | |
| 176 | if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) { |
| 177 | markSafeWeakUse(POE->getSyntacticForm()); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) { |
| 182 | markSafeWeakUse(Cond->getTrueExpr()); |
| 183 | markSafeWeakUse(Cond->getFalseExpr()); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (const BinaryConditionalOperator *Cond = |
| 188 | dyn_cast<BinaryConditionalOperator>(E)) { |
| 189 | markSafeWeakUse(Cond->getCommon()); |
| 190 | markSafeWeakUse(Cond->getFalseExpr()); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | // Has this weak object been seen before? |
Akira Hatanaka | 034c633 | 2017-02-01 20:22:26 +0000 | [diff] [blame] | 195 | FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end(); |
Fariborz Jahanian | 9277ff4 | 2014-06-17 23:35:13 +0000 | [diff] [blame] | 196 | if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) { |
Fariborz Jahanian | ac1c512 | 2014-11-21 21:12:11 +0000 | [diff] [blame] | 197 | if (!RefExpr->isObjectReceiver()) |
| 198 | return; |
Fariborz Jahanian | 9277ff4 | 2014-06-17 23:35:13 +0000 | [diff] [blame] | 199 | if (isa<OpaqueValueExpr>(RefExpr->getBase())) |
| 200 | Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr)); |
| 201 | else { |
| 202 | markSafeWeakUse(RefExpr->getBase()); |
| 203 | return; |
| 204 | } |
| 205 | } |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 206 | else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E)) |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 207 | Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE)); |
Akira Hatanaka | 034c633 | 2017-02-01 20:22:26 +0000 | [diff] [blame] | 208 | else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 209 | if (isa<VarDecl>(DRE->getDecl())) |
| 210 | Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE)); |
| 211 | } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) { |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 212 | if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) { |
| 213 | if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) { |
| 214 | Uses = |
| 215 | WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(), |
| 216 | Prop)); |
| 217 | } |
| 218 | } |
| 219 | } |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 220 | else |
| 221 | return; |
| 222 | |
| 223 | if (Uses == WeakObjectUses.end()) |
| 224 | return; |
| 225 | |
| 226 | // Has there been a read from the object using this Expr? |
| 227 | FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse = |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 228 | llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true)); |
Jordan Rose | 62b3798 | 2012-09-28 22:21:39 +0000 | [diff] [blame] | 229 | if (ThisUse == Uses->second.rend()) |
| 230 | return; |
| 231 | |
| 232 | ThisUse->markSafe(); |
| 233 | } |
| 234 | |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 235 | void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, |
| 236 | Expr *&E) const { |
Faisal Vali | 9366a48 | 2013-11-07 16:57:56 +0000 | [diff] [blame] | 237 | assert(Idx < getNumPotentialVariableCaptures() && |
Faisal Vali | ab3d646 | 2013-12-07 20:22:44 +0000 | [diff] [blame] | 238 | "Index of potential capture must be within 0 to less than the " |
| 239 | "number of captures!"); |
Faisal Vali | a17d19f | 2013-11-07 05:17:06 +0000 | [diff] [blame] | 240 | E = PotentiallyCapturingExprs[Idx]; |
| 241 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 242 | VD = dyn_cast<VarDecl>(DRE->getFoundDecl()); |
| 243 | else if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| 244 | VD = dyn_cast<VarDecl>(ME->getMemberDecl()); |
| 245 | else |
| 246 | llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for " |
| 247 | "potential captures"); |
| 248 | assert(VD); |
| 249 | } |
| 250 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 251 | FunctionScopeInfo::~FunctionScopeInfo() { } |
| 252 | BlockScopeInfo::~BlockScopeInfo() { } |
| 253 | CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { } |