blob: b309a36a30a3f250770469a01384d343129af0b3 [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;
Erik Pilkington5dbe7a92016-11-09 22:52:23 +000032 HasFallthroughStmt = false;
Erik Pilkington5cd57172016-08-16 17:44:11 +000033 HasPotentialAvailabilityViolations = false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +000034 ObjCShouldCallSuper = false;
35 ObjCIsDesignatedInit = false;
36 ObjCWarnForNoDesignatedInitChain = false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +000037 ObjCIsSecondaryInit = false;
38 ObjCWarnForNoInitDelegation = false;
Richard Smith9f690bd2015-10-27 06:02:45 +000039 FirstReturnLoc = SourceLocation();
Reid Klecknere7175912015-02-02 22:15:31 +000040 FirstCXXTryLoc = SourceLocation();
41 FirstSEHTryLoc = SourceLocation();
Jordan Rose62b37982012-09-28 22:21:39 +000042
Eric Fiseliercac0a592017-03-11 02:35:37 +000043 // Coroutine state
44 FirstCoroutineStmtLoc = SourceLocation();
Richard Smithdcd04d12015-10-27 07:47:45 +000045 CoroutinePromise = nullptr;
Eric Fiselier20f25cb2017-03-06 23:38:15 +000046 NeedsCoroutineSuspends = true;
47 CoroutineSuspends.first = nullptr;
48 CoroutineSuspends.second = nullptr;
Eric Fiseliercac0a592017-03-11 02:35:37 +000049
50 SwitchStack.clear();
51 Returns.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000052 ErrorTrap.reset();
53 PossiblyUnreachableDiags.clear();
54 WeakObjectUses.clear();
Fariborz Jahanianef202d92014-11-18 21:57:54 +000055 ModifiedNonNullParams.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000056}
57
58static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
59 if (PropE->isExplicitProperty())
60 return PropE->getExplicitProperty();
61
62 return PropE->getImplicitPropertyGetter();
63}
64
Jordan Rose62b37982012-09-28 22:21:39 +000065FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
66FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
67 E = E->IgnoreParenCasts();
68
Craig Topperc3ec1492014-05-26 06:22:03 +000069 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000070 bool IsExact = false;
71
72 switch (E->getStmtClass()) {
73 case Stmt::DeclRefExprClass:
74 D = cast<DeclRefExpr>(E)->getDecl();
75 IsExact = isa<VarDecl>(D);
76 break;
77 case Stmt::MemberExprClass: {
78 const MemberExpr *ME = cast<MemberExpr>(E);
79 D = ME->getMemberDecl();
80 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
81 break;
82 }
83 case Stmt::ObjCIvarRefExprClass: {
84 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
85 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000086 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000087 break;
88 }
89 case Stmt::PseudoObjectExprClass: {
90 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
91 const ObjCPropertyRefExpr *BaseProp =
92 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
93 if (BaseProp) {
94 D = getBestPropertyDecl(BaseProp);
95
Akira Hatanaka7e2c82d2016-03-22 05:00:21 +000096 if (BaseProp->isObjectReceiver()) {
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000097 const Expr *DoubleBase = BaseProp->getBase();
98 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
99 DoubleBase = OVE->getSourceExpr();
Jordan Rose62b37982012-09-28 22:21:39 +0000100
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +0000101 IsExact = DoubleBase->isObjCSelfExpr();
102 }
Jordan Rose62b37982012-09-28 22:21:39 +0000103 }
104 break;
105 }
106 default:
107 break;
108 }
109
110 return BaseInfoTy(D, IsExact);
111}
112
Alexey Bataev39c81e22014-08-28 04:28:19 +0000113bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +0000114 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000115 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000116 RD = LSI->Lambda;
117 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
118 RD = CRSI->TheRecordDecl;
119
120 if (RD)
121 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000122 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
123 return true;
124 }
125 return false;
126}
127
Jordan Rose62b37982012-09-28 22:21:39 +0000128FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
129 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000130 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000131
132 if (PropE->isObjectReceiver()) {
133 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
134 const Expr *E = OVE->getSourceExpr();
135 Base = getBaseInfo(E);
136 } else if (PropE->isClassReceiver()) {
137 Base.setPointer(PropE->getClassReceiver());
138 } else {
139 assert(PropE->isSuperReceiver());
140 }
141}
142
Jordan Rose22487652012-10-11 16:06:21 +0000143FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
144 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000145 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000146 if (BaseE)
147 Base = getBaseInfo(BaseE);
148 // else, this is a message accessing a property on super.
149}
150
Jordan Rose62b37982012-09-28 22:21:39 +0000151FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
152 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000153 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000154 assert(isa<VarDecl>(Property));
155}
156
157FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
158 const ObjCIvarRefExpr *IvarE)
159 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
160}
161
Jordan Rose22487652012-10-11 16:06:21 +0000162void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
163 const ObjCPropertyDecl *Prop) {
164 assert(Msg && Prop);
165 WeakUseVector &Uses =
166 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
167 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
168}
169
Jordan Rose62b37982012-09-28 22:21:39 +0000170void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000171 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000172
173 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
174 markSafeWeakUse(POE->getSyntacticForm());
175 return;
176 }
177
178 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
179 markSafeWeakUse(Cond->getTrueExpr());
180 markSafeWeakUse(Cond->getFalseExpr());
181 return;
182 }
183
184 if (const BinaryConditionalOperator *Cond =
185 dyn_cast<BinaryConditionalOperator>(E)) {
186 markSafeWeakUse(Cond->getCommon());
187 markSafeWeakUse(Cond->getFalseExpr());
188 return;
189 }
190
191 // Has this weak object been seen before?
Akira Hatanaka034c6332017-02-01 20:22:26 +0000192 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000193 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000194 if (!RefExpr->isObjectReceiver())
195 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000196 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
197 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
198 else {
199 markSafeWeakUse(RefExpr->getBase());
200 return;
201 }
202 }
Jordan Rose62b37982012-09-28 22:21:39 +0000203 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000204 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Akira Hatanaka034c6332017-02-01 20:22:26 +0000205 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
206 if (isa<VarDecl>(DRE->getDecl()))
207 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
208 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000209 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
210 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
211 Uses =
212 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
213 Prop));
214 }
215 }
216 }
Jordan Rose62b37982012-09-28 22:21:39 +0000217 else
218 return;
219
220 if (Uses == WeakObjectUses.end())
221 return;
222
223 // Has there been a read from the object using this Expr?
224 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
David Majnemerf7e36092016-06-23 00:15:04 +0000225 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
Jordan Rose62b37982012-09-28 22:21:39 +0000226 if (ThisUse == Uses->second.rend())
227 return;
228
229 ThisUse->markSafe();
230}
231
Faisal Valiab3d6462013-12-07 20:22:44 +0000232void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
233 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000234 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000235 "Index of potential capture must be within 0 to less than the "
236 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000237 E = PotentiallyCapturingExprs[Idx];
238 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
239 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
240 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
241 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
242 else
243 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
244 "potential captures");
245 assert(VD);
246}
247
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000248FunctionScopeInfo::~FunctionScopeInfo() { }
249BlockScopeInfo::~BlockScopeInfo() { }
250CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }