blob: 037ffd843bb716aca0460252a630d1bdf0246e99 [file] [log] [blame]
Jordan Rose62b37982012-09-28 22:21:39 +00001//===--- 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 Bataev39c81e22014-08-28 04:28:19 +000017#include "clang/AST/DeclCXX.h"
Jordan Rose62b37982012-09-28 22:21:39 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/ExprCXX.h"
21#include "clang/AST/ExprObjC.h"
22
23using namespace clang;
24using namespace sema;
25
26void FunctionScopeInfo::Clear() {
27 HasBranchProtectedScope = false;
28 HasBranchIntoScope = false;
29 HasIndirectGoto = false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +000030 HasDroppedStmt = false;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000031 HasOMPDeclareReductionCombiner = false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +000032 ObjCShouldCallSuper = false;
33 ObjCIsDesignatedInit = false;
34 ObjCWarnForNoDesignatedInitChain = false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +000035 ObjCIsSecondaryInit = false;
36 ObjCWarnForNoInitDelegation = false;
Richard Smith9f690bd2015-10-27 06:02:45 +000037 FirstReturnLoc = SourceLocation();
Reid Klecknere7175912015-02-02 22:15:31 +000038 FirstCXXTryLoc = SourceLocation();
39 FirstSEHTryLoc = SourceLocation();
Jordan Rose62b37982012-09-28 22:21:39 +000040
41 SwitchStack.clear();
42 Returns.clear();
Richard Smithdcd04d12015-10-27 07:47:45 +000043 CoroutinePromise = nullptr;
Richard Smith9f690bd2015-10-27 06:02:45 +000044 CoroutineStmts.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000045 ErrorTrap.reset();
46 PossiblyUnreachableDiags.clear();
47 WeakObjectUses.clear();
Fariborz Jahanianef202d92014-11-18 21:57:54 +000048 ModifiedNonNullParams.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000049}
50
51static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
52 if (PropE->isExplicitProperty())
53 return PropE->getExplicitProperty();
54
55 return PropE->getImplicitPropertyGetter();
56}
57
Jordan Rose62b37982012-09-28 22:21:39 +000058FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
59FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
60 E = E->IgnoreParenCasts();
61
Craig Topperc3ec1492014-05-26 06:22:03 +000062 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000063 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 Zaks97c7ce32012-10-01 20:34:04 +000079 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000080 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
Akira Hatanaka7e2c82d2016-03-22 05:00:21 +000089 if (BaseProp->isObjectReceiver()) {
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000090 const Expr *DoubleBase = BaseProp->getBase();
91 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
92 DoubleBase = OVE->getSourceExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000093
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000094 IsExact = DoubleBase->isObjCSelfExpr();
95 }
Jordan Rose62b37982012-09-28 22:21:39 +000096 }
97 break;
98 }
99 default:
100 break;
101 }
102
103 return BaseInfoTy(D, IsExact);
104}
105
Alexey Bataev39c81e22014-08-28 04:28:19 +0000106bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +0000107 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000108 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000109 RD = LSI->Lambda;
110 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
111 RD = CRSI->TheRecordDecl;
112
113 if (RD)
114 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000115 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
116 return true;
117 }
118 return false;
119}
120
Jordan Rose62b37982012-09-28 22:21:39 +0000121FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
122 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000123 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000124
125 if (PropE->isObjectReceiver()) {
126 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
127 const Expr *E = OVE->getSourceExpr();
128 Base = getBaseInfo(E);
129 } else if (PropE->isClassReceiver()) {
130 Base.setPointer(PropE->getClassReceiver());
131 } else {
132 assert(PropE->isSuperReceiver());
133 }
134}
135
Jordan Rose22487652012-10-11 16:06:21 +0000136FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
137 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000138 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000139 if (BaseE)
140 Base = getBaseInfo(BaseE);
141 // else, this is a message accessing a property on super.
142}
143
Jordan Rose62b37982012-09-28 22:21:39 +0000144FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
145 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000146 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000147 assert(isa<VarDecl>(Property));
148}
149
150FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
151 const ObjCIvarRefExpr *IvarE)
152 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
153}
154
Jordan Rose22487652012-10-11 16:06:21 +0000155void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
156 const ObjCPropertyDecl *Prop) {
157 assert(Msg && Prop);
158 WeakUseVector &Uses =
159 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
160 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
161}
162
Jordan Rose62b37982012-09-28 22:21:39 +0000163void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000164 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000165
166 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
167 markSafeWeakUse(POE->getSyntacticForm());
168 return;
169 }
170
171 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
172 markSafeWeakUse(Cond->getTrueExpr());
173 markSafeWeakUse(Cond->getFalseExpr());
174 return;
175 }
176
177 if (const BinaryConditionalOperator *Cond =
178 dyn_cast<BinaryConditionalOperator>(E)) {
179 markSafeWeakUse(Cond->getCommon());
180 markSafeWeakUse(Cond->getFalseExpr());
181 return;
182 }
183
184 // Has this weak object been seen before?
185 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000186 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000187 if (!RefExpr->isObjectReceiver())
188 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000189 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
190 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
191 else {
192 markSafeWeakUse(RefExpr->getBase());
193 return;
194 }
195 }
Jordan Rose62b37982012-09-28 22:21:39 +0000196 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000197 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Jordan Rose62b37982012-09-28 22:21:39 +0000198 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000199 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
Jordan Roseb1e3e5f2012-10-11 17:02:00 +0000200 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000201 Uses = WeakObjectUses.end();
202 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
203 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
204 Uses =
205 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
206 Prop));
207 }
208 }
209 }
Jordan Rose62b37982012-09-28 22:21:39 +0000210 else
211 return;
212
213 if (Uses == WeakObjectUses.end())
214 return;
215
216 // Has there been a read from the object using this Expr?
217 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
218 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
219 if (ThisUse == Uses->second.rend())
220 return;
221
222 ThisUse->markSafe();
223}
224
Faisal Valiab3d6462013-12-07 20:22:44 +0000225void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
226 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000227 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000228 "Index of potential capture must be within 0 to less than the "
229 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000230 E = PotentiallyCapturingExprs[Idx];
231 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
232 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
233 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
234 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
235 else
236 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
237 "potential captures");
238 assert(VD);
239}
240
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000241FunctionScopeInfo::~FunctionScopeInfo() { }
242BlockScopeInfo::~BlockScopeInfo() { }
243CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }