blob: 8050889d71ae3141c68954d70844f7c7f7ff4e47 [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
43 SwitchStack.clear();
44 Returns.clear();
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;
Richard Smith9f690bd2015-10-27 06:02:45 +000049 CoroutineStmts.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000050 ErrorTrap.reset();
51 PossiblyUnreachableDiags.clear();
52 WeakObjectUses.clear();
Fariborz Jahanianef202d92014-11-18 21:57:54 +000053 ModifiedNonNullParams.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000054}
55
56static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
57 if (PropE->isExplicitProperty())
58 return PropE->getExplicitProperty();
59
60 return PropE->getImplicitPropertyGetter();
61}
62
Jordan Rose62b37982012-09-28 22:21:39 +000063FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
64FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
65 E = E->IgnoreParenCasts();
66
Craig Topperc3ec1492014-05-26 06:22:03 +000067 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000068 bool IsExact = false;
69
70 switch (E->getStmtClass()) {
71 case Stmt::DeclRefExprClass:
72 D = cast<DeclRefExpr>(E)->getDecl();
73 IsExact = isa<VarDecl>(D);
74 break;
75 case Stmt::MemberExprClass: {
76 const MemberExpr *ME = cast<MemberExpr>(E);
77 D = ME->getMemberDecl();
78 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
79 break;
80 }
81 case Stmt::ObjCIvarRefExprClass: {
82 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
83 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000084 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000085 break;
86 }
87 case Stmt::PseudoObjectExprClass: {
88 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
89 const ObjCPropertyRefExpr *BaseProp =
90 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
91 if (BaseProp) {
92 D = getBestPropertyDecl(BaseProp);
93
Akira Hatanaka7e2c82d2016-03-22 05:00:21 +000094 if (BaseProp->isObjectReceiver()) {
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000095 const Expr *DoubleBase = BaseProp->getBase();
96 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
97 DoubleBase = OVE->getSourceExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000098
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000099 IsExact = DoubleBase->isObjCSelfExpr();
100 }
Jordan Rose62b37982012-09-28 22:21:39 +0000101 }
102 break;
103 }
104 default:
105 break;
106 }
107
108 return BaseInfoTy(D, IsExact);
109}
110
Alexey Bataev39c81e22014-08-28 04:28:19 +0000111bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +0000112 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000113 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000114 RD = LSI->Lambda;
115 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
116 RD = CRSI->TheRecordDecl;
117
118 if (RD)
119 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000120 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
121 return true;
122 }
123 return false;
124}
125
Jordan Rose62b37982012-09-28 22:21:39 +0000126FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
127 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000128 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000129
130 if (PropE->isObjectReceiver()) {
131 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
132 const Expr *E = OVE->getSourceExpr();
133 Base = getBaseInfo(E);
134 } else if (PropE->isClassReceiver()) {
135 Base.setPointer(PropE->getClassReceiver());
136 } else {
137 assert(PropE->isSuperReceiver());
138 }
139}
140
Jordan Rose22487652012-10-11 16:06:21 +0000141FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
142 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000143 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000144 if (BaseE)
145 Base = getBaseInfo(BaseE);
146 // else, this is a message accessing a property on super.
147}
148
Jordan Rose62b37982012-09-28 22:21:39 +0000149FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
150 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000151 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000152 assert(isa<VarDecl>(Property));
153}
154
155FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
156 const ObjCIvarRefExpr *IvarE)
157 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
158}
159
Jordan Rose22487652012-10-11 16:06:21 +0000160void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
161 const ObjCPropertyDecl *Prop) {
162 assert(Msg && Prop);
163 WeakUseVector &Uses =
164 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
165 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
166}
167
Jordan Rose62b37982012-09-28 22:21:39 +0000168void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000169 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000170
171 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
172 markSafeWeakUse(POE->getSyntacticForm());
173 return;
174 }
175
176 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
177 markSafeWeakUse(Cond->getTrueExpr());
178 markSafeWeakUse(Cond->getFalseExpr());
179 return;
180 }
181
182 if (const BinaryConditionalOperator *Cond =
183 dyn_cast<BinaryConditionalOperator>(E)) {
184 markSafeWeakUse(Cond->getCommon());
185 markSafeWeakUse(Cond->getFalseExpr());
186 return;
187 }
188
189 // Has this weak object been seen before?
Akira Hatanaka034c6332017-02-01 20:22:26 +0000190 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000191 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000192 if (!RefExpr->isObjectReceiver())
193 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000194 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
195 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
196 else {
197 markSafeWeakUse(RefExpr->getBase());
198 return;
199 }
200 }
Jordan Rose62b37982012-09-28 22:21:39 +0000201 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000202 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Akira Hatanaka034c6332017-02-01 20:22:26 +0000203 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
204 if (isa<VarDecl>(DRE->getDecl()))
205 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
206 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000207 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
208 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
209 Uses =
210 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
211 Prop));
212 }
213 }
214 }
Jordan Rose62b37982012-09-28 22:21:39 +0000215 else
216 return;
217
218 if (Uses == WeakObjectUses.end())
219 return;
220
221 // Has there been a read from the object using this Expr?
222 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
David Majnemerf7e36092016-06-23 00:15:04 +0000223 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
Jordan Rose62b37982012-09-28 22:21:39 +0000224 if (ThisUse == Uses->second.rend())
225 return;
226
227 ThisUse->markSafe();
228}
229
Faisal Valiab3d6462013-12-07 20:22:44 +0000230void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
231 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000232 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000233 "Index of potential capture must be within 0 to less than the "
234 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000235 E = PotentiallyCapturingExprs[Idx];
236 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
237 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
238 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
239 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
240 else
241 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
242 "potential captures");
243 assert(VD);
244}
245
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000246FunctionScopeInfo::~FunctionScopeInfo() { }
247BlockScopeInfo::~BlockScopeInfo() { }
248CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }