blob: c4bf67b76cda53df19ae9ee01c9bd79a55874b73 [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;
31 ObjCShouldCallSuper = false;
32 ObjCIsDesignatedInit = false;
33 ObjCWarnForNoDesignatedInitChain = false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +000034 ObjCIsSecondaryInit = false;
35 ObjCWarnForNoInitDelegation = false;
Jordan Rose62b37982012-09-28 22:21:39 +000036
37 SwitchStack.clear();
38 Returns.clear();
39 ErrorTrap.reset();
40 PossiblyUnreachableDiags.clear();
41 WeakObjectUses.clear();
42}
43
44static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
45 if (PropE->isExplicitProperty())
46 return PropE->getExplicitProperty();
47
48 return PropE->getImplicitPropertyGetter();
49}
50
Jordan Rose62b37982012-09-28 22:21:39 +000051FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
52FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
53 E = E->IgnoreParenCasts();
54
Craig Topperc3ec1492014-05-26 06:22:03 +000055 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000056 bool IsExact = false;
57
58 switch (E->getStmtClass()) {
59 case Stmt::DeclRefExprClass:
60 D = cast<DeclRefExpr>(E)->getDecl();
61 IsExact = isa<VarDecl>(D);
62 break;
63 case Stmt::MemberExprClass: {
64 const MemberExpr *ME = cast<MemberExpr>(E);
65 D = ME->getMemberDecl();
66 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
67 break;
68 }
69 case Stmt::ObjCIvarRefExprClass: {
70 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
71 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000072 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000073 break;
74 }
75 case Stmt::PseudoObjectExprClass: {
76 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
77 const ObjCPropertyRefExpr *BaseProp =
78 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
79 if (BaseProp) {
80 D = getBestPropertyDecl(BaseProp);
81
82 const Expr *DoubleBase = BaseProp->getBase();
83 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
84 DoubleBase = OVE->getSourceExpr();
85
Anna Zaks97c7ce32012-10-01 20:34:04 +000086 IsExact = DoubleBase->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000087 }
88 break;
89 }
90 default:
91 break;
92 }
93
94 return BaseInfoTy(D, IsExact);
95}
96
Alexey Bataev39c81e22014-08-28 04:28:19 +000097bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +000098 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +000099 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000100 RD = LSI->Lambda;
101 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
102 RD = CRSI->TheRecordDecl;
103
104 if (RD)
105 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000106 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
107 return true;
108 }
109 return false;
110}
111
Jordan Rose62b37982012-09-28 22:21:39 +0000112FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
113 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000114 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000115
116 if (PropE->isObjectReceiver()) {
117 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
118 const Expr *E = OVE->getSourceExpr();
119 Base = getBaseInfo(E);
120 } else if (PropE->isClassReceiver()) {
121 Base.setPointer(PropE->getClassReceiver());
122 } else {
123 assert(PropE->isSuperReceiver());
124 }
125}
126
Jordan Rose22487652012-10-11 16:06:21 +0000127FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
128 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000129 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000130 if (BaseE)
131 Base = getBaseInfo(BaseE);
132 // else, this is a message accessing a property on super.
133}
134
Jordan Rose62b37982012-09-28 22:21:39 +0000135FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
136 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000137 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000138 assert(isa<VarDecl>(Property));
139}
140
141FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
142 const ObjCIvarRefExpr *IvarE)
143 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
144}
145
Jordan Rose22487652012-10-11 16:06:21 +0000146void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
147 const ObjCPropertyDecl *Prop) {
148 assert(Msg && Prop);
149 WeakUseVector &Uses =
150 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
151 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
152}
153
Jordan Rose62b37982012-09-28 22:21:39 +0000154void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000155 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000156
157 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
158 markSafeWeakUse(POE->getSyntacticForm());
159 return;
160 }
161
162 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
163 markSafeWeakUse(Cond->getTrueExpr());
164 markSafeWeakUse(Cond->getFalseExpr());
165 return;
166 }
167
168 if (const BinaryConditionalOperator *Cond =
169 dyn_cast<BinaryConditionalOperator>(E)) {
170 markSafeWeakUse(Cond->getCommon());
171 markSafeWeakUse(Cond->getFalseExpr());
172 return;
173 }
174
175 // Has this weak object been seen before?
176 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000177 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
178 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
179 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
180 else {
181 markSafeWeakUse(RefExpr->getBase());
182 return;
183 }
184 }
Jordan Rose62b37982012-09-28 22:21:39 +0000185 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000186 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Jordan Rose62b37982012-09-28 22:21:39 +0000187 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000188 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
Jordan Roseb1e3e5f2012-10-11 17:02:00 +0000189 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000190 Uses = WeakObjectUses.end();
191 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
192 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
193 Uses =
194 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
195 Prop));
196 }
197 }
198 }
Jordan Rose62b37982012-09-28 22:21:39 +0000199 else
200 return;
201
202 if (Uses == WeakObjectUses.end())
203 return;
204
205 // Has there been a read from the object using this Expr?
206 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
207 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
208 if (ThisUse == Uses->second.rend())
209 return;
210
211 ThisUse->markSafe();
212}
213
Faisal Valiab3d6462013-12-07 20:22:44 +0000214void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
215 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000216 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000217 "Index of potential capture must be within 0 to less than the "
218 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000219 E = PotentiallyCapturingExprs[Idx];
220 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
221 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
222 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
223 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
224 else
225 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
226 "potential captures");
227 assert(VD);
228}
229
Jordan Rose62b37982012-09-28 22:21:39 +0000230FunctionScopeInfo::~FunctionScopeInfo() { }
231BlockScopeInfo::~BlockScopeInfo() { }
232LambdaScopeInfo::~LambdaScopeInfo() { }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000233CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }