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