blob: ba8c3642bc8cb8d01f8ad1e72151e3441a338b0b [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;
Reid Klecknere7175912015-02-02 22:15:31 +000036 FirstCXXTryLoc = SourceLocation();
37 FirstSEHTryLoc = SourceLocation();
Jordan Rose62b37982012-09-28 22:21:39 +000038
39 SwitchStack.clear();
40 Returns.clear();
41 ErrorTrap.reset();
42 PossiblyUnreachableDiags.clear();
43 WeakObjectUses.clear();
Fariborz Jahanianef202d92014-11-18 21:57:54 +000044 ModifiedNonNullParams.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000045}
46
47static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
48 if (PropE->isExplicitProperty())
49 return PropE->getExplicitProperty();
50
51 return PropE->getImplicitPropertyGetter();
52}
53
Jordan Rose62b37982012-09-28 22:21:39 +000054FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
55FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
56 E = E->IgnoreParenCasts();
57
Craig Topperc3ec1492014-05-26 06:22:03 +000058 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000059 bool IsExact = false;
60
61 switch (E->getStmtClass()) {
62 case Stmt::DeclRefExprClass:
63 D = cast<DeclRefExpr>(E)->getDecl();
64 IsExact = isa<VarDecl>(D);
65 break;
66 case Stmt::MemberExprClass: {
67 const MemberExpr *ME = cast<MemberExpr>(E);
68 D = ME->getMemberDecl();
69 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
70 break;
71 }
72 case Stmt::ObjCIvarRefExprClass: {
73 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
74 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000075 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000076 break;
77 }
78 case Stmt::PseudoObjectExprClass: {
79 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
80 const ObjCPropertyRefExpr *BaseProp =
81 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
82 if (BaseProp) {
83 D = getBestPropertyDecl(BaseProp);
84
85 const Expr *DoubleBase = BaseProp->getBase();
86 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
87 DoubleBase = OVE->getSourceExpr();
88
Anna Zaks97c7ce32012-10-01 20:34:04 +000089 IsExact = DoubleBase->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000090 }
91 break;
92 }
93 default:
94 break;
95 }
96
97 return BaseInfoTy(D, IsExact);
98}
99
Alexey Bataev39c81e22014-08-28 04:28:19 +0000100bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +0000101 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000102 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000103 RD = LSI->Lambda;
104 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
105 RD = CRSI->TheRecordDecl;
106
107 if (RD)
108 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000109 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
110 return true;
111 }
112 return false;
113}
114
Jordan Rose62b37982012-09-28 22:21:39 +0000115FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
116 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000117 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000118
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 Rose22487652012-10-11 16:06:21 +0000130FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
131 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000132 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000133 if (BaseE)
134 Base = getBaseInfo(BaseE);
135 // else, this is a message accessing a property on super.
136}
137
Jordan Rose62b37982012-09-28 22:21:39 +0000138FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
139 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000140 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000141 assert(isa<VarDecl>(Property));
142}
143
144FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
145 const ObjCIvarRefExpr *IvarE)
146 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
147}
148
Jordan Rose22487652012-10-11 16:06:21 +0000149void 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 Rose62b37982012-09-28 22:21:39 +0000157void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000158 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000159
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?
179 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000180 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000181 if (!RefExpr->isObjectReceiver())
182 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000183 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
184 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
185 else {
186 markSafeWeakUse(RefExpr->getBase());
187 return;
188 }
189 }
Jordan Rose62b37982012-09-28 22:21:39 +0000190 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000191 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Jordan Rose62b37982012-09-28 22:21:39 +0000192 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000193 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
Jordan Roseb1e3e5f2012-10-11 17:02:00 +0000194 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000195 Uses = WeakObjectUses.end();
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 Rose62b37982012-09-28 22:21:39 +0000204 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 =
212 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
213 if (ThisUse == Uses->second.rend())
214 return;
215
216 ThisUse->markSafe();
217}
218
Faisal Valiab3d6462013-12-07 20:22:44 +0000219void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
220 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000221 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000222 "Index of potential capture must be within 0 to less than the "
223 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000224 E = PotentiallyCapturingExprs[Idx];
225 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
226 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
227 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
228 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
229 else
230 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
231 "potential captures");
232 assert(VD);
233}
234
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000235FunctionScopeInfo::~FunctionScopeInfo() { }
236BlockScopeInfo::~BlockScopeInfo() { }
237CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }