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