blob: 6a400a2efe5a1ed1f467da69dc40b4fbe4d2b650 [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;
Richard Smith9f690bd2015-10-27 06:02:45 +000036 FirstReturnLoc = SourceLocation();
Reid Klecknere7175912015-02-02 22:15:31 +000037 FirstCXXTryLoc = SourceLocation();
38 FirstSEHTryLoc = SourceLocation();
Jordan Rose62b37982012-09-28 22:21:39 +000039
40 SwitchStack.clear();
41 Returns.clear();
Richard Smith9f690bd2015-10-27 06:02:45 +000042 CoroutineStmts.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000043 ErrorTrap.reset();
44 PossiblyUnreachableDiags.clear();
45 WeakObjectUses.clear();
Fariborz Jahanianef202d92014-11-18 21:57:54 +000046 ModifiedNonNullParams.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000047}
48
49static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
50 if (PropE->isExplicitProperty())
51 return PropE->getExplicitProperty();
52
53 return PropE->getImplicitPropertyGetter();
54}
55
Jordan Rose62b37982012-09-28 22:21:39 +000056FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
57FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
58 E = E->IgnoreParenCasts();
59
Craig Topperc3ec1492014-05-26 06:22:03 +000060 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000061 bool IsExact = false;
62
63 switch (E->getStmtClass()) {
64 case Stmt::DeclRefExprClass:
65 D = cast<DeclRefExpr>(E)->getDecl();
66 IsExact = isa<VarDecl>(D);
67 break;
68 case Stmt::MemberExprClass: {
69 const MemberExpr *ME = cast<MemberExpr>(E);
70 D = ME->getMemberDecl();
71 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
72 break;
73 }
74 case Stmt::ObjCIvarRefExprClass: {
75 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
76 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000077 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000078 break;
79 }
80 case Stmt::PseudoObjectExprClass: {
81 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
82 const ObjCPropertyRefExpr *BaseProp =
83 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
84 if (BaseProp) {
85 D = getBestPropertyDecl(BaseProp);
86
87 const Expr *DoubleBase = BaseProp->getBase();
88 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
89 DoubleBase = OVE->getSourceExpr();
90
Anna Zaks97c7ce32012-10-01 20:34:04 +000091 IsExact = DoubleBase->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000092 }
93 break;
94 }
95 default:
96 break;
97 }
98
99 return BaseInfoTy(D, IsExact);
100}
101
Alexey Bataev39c81e22014-08-28 04:28:19 +0000102bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +0000103 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000104 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000105 RD = LSI->Lambda;
106 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
107 RD = CRSI->TheRecordDecl;
108
109 if (RD)
110 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000111 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
112 return true;
113 }
114 return false;
115}
116
Jordan Rose62b37982012-09-28 22:21:39 +0000117FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
118 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000119 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000120
121 if (PropE->isObjectReceiver()) {
122 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
123 const Expr *E = OVE->getSourceExpr();
124 Base = getBaseInfo(E);
125 } else if (PropE->isClassReceiver()) {
126 Base.setPointer(PropE->getClassReceiver());
127 } else {
128 assert(PropE->isSuperReceiver());
129 }
130}
131
Jordan Rose22487652012-10-11 16:06:21 +0000132FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
133 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000134 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000135 if (BaseE)
136 Base = getBaseInfo(BaseE);
137 // else, this is a message accessing a property on super.
138}
139
Jordan Rose62b37982012-09-28 22:21:39 +0000140FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
141 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000142 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000143 assert(isa<VarDecl>(Property));
144}
145
146FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
147 const ObjCIvarRefExpr *IvarE)
148 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
149}
150
Jordan Rose22487652012-10-11 16:06:21 +0000151void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
152 const ObjCPropertyDecl *Prop) {
153 assert(Msg && Prop);
154 WeakUseVector &Uses =
155 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
156 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
157}
158
Jordan Rose62b37982012-09-28 22:21:39 +0000159void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000160 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000161
162 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
163 markSafeWeakUse(POE->getSyntacticForm());
164 return;
165 }
166
167 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
168 markSafeWeakUse(Cond->getTrueExpr());
169 markSafeWeakUse(Cond->getFalseExpr());
170 return;
171 }
172
173 if (const BinaryConditionalOperator *Cond =
174 dyn_cast<BinaryConditionalOperator>(E)) {
175 markSafeWeakUse(Cond->getCommon());
176 markSafeWeakUse(Cond->getFalseExpr());
177 return;
178 }
179
180 // Has this weak object been seen before?
181 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000182 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000183 if (!RefExpr->isObjectReceiver())
184 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000185 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
186 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
187 else {
188 markSafeWeakUse(RefExpr->getBase());
189 return;
190 }
191 }
Jordan Rose62b37982012-09-28 22:21:39 +0000192 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000193 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Jordan Rose62b37982012-09-28 22:21:39 +0000194 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000195 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
Jordan Roseb1e3e5f2012-10-11 17:02:00 +0000196 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000197 Uses = WeakObjectUses.end();
198 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
199 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
200 Uses =
201 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
202 Prop));
203 }
204 }
205 }
Jordan Rose62b37982012-09-28 22:21:39 +0000206 else
207 return;
208
209 if (Uses == WeakObjectUses.end())
210 return;
211
212 // Has there been a read from the object using this Expr?
213 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
214 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
215 if (ThisUse == Uses->second.rend())
216 return;
217
218 ThisUse->markSafe();
219}
220
Faisal Valiab3d6462013-12-07 20:22:44 +0000221void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
222 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000223 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000224 "Index of potential capture must be within 0 to less than the "
225 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000226 E = PotentiallyCapturingExprs[Idx];
227 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
228 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
229 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
230 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
231 else
232 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
233 "potential captures");
234 assert(VD);
235}
236
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000237FunctionScopeInfo::~FunctionScopeInfo() { }
238BlockScopeInfo::~BlockScopeInfo() { }
239CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }