blob: 76240bcbfc92df39317d42ea50f6209b7ea90af9 [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 Pilkington5cd57172016-08-16 17:44:11 +000032 HasPotentialAvailabilityViolations = false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +000033 ObjCShouldCallSuper = false;
34 ObjCIsDesignatedInit = false;
35 ObjCWarnForNoDesignatedInitChain = false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +000036 ObjCIsSecondaryInit = false;
37 ObjCWarnForNoInitDelegation = false;
Richard Smith9f690bd2015-10-27 06:02:45 +000038 FirstReturnLoc = SourceLocation();
Reid Klecknere7175912015-02-02 22:15:31 +000039 FirstCXXTryLoc = SourceLocation();
40 FirstSEHTryLoc = SourceLocation();
Jordan Rose62b37982012-09-28 22:21:39 +000041
42 SwitchStack.clear();
43 Returns.clear();
Richard Smithdcd04d12015-10-27 07:47:45 +000044 CoroutinePromise = nullptr;
Richard Smith9f690bd2015-10-27 06:02:45 +000045 CoroutineStmts.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000046 ErrorTrap.reset();
47 PossiblyUnreachableDiags.clear();
48 WeakObjectUses.clear();
Fariborz Jahanianef202d92014-11-18 21:57:54 +000049 ModifiedNonNullParams.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000050}
51
52static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
53 if (PropE->isExplicitProperty())
54 return PropE->getExplicitProperty();
55
56 return PropE->getImplicitPropertyGetter();
57}
58
Jordan Rose62b37982012-09-28 22:21:39 +000059FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
60FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
61 E = E->IgnoreParenCasts();
62
Craig Topperc3ec1492014-05-26 06:22:03 +000063 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000064 bool IsExact = false;
65
66 switch (E->getStmtClass()) {
67 case Stmt::DeclRefExprClass:
68 D = cast<DeclRefExpr>(E)->getDecl();
69 IsExact = isa<VarDecl>(D);
70 break;
71 case Stmt::MemberExprClass: {
72 const MemberExpr *ME = cast<MemberExpr>(E);
73 D = ME->getMemberDecl();
74 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
75 break;
76 }
77 case Stmt::ObjCIvarRefExprClass: {
78 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
79 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000080 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000081 break;
82 }
83 case Stmt::PseudoObjectExprClass: {
84 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
85 const ObjCPropertyRefExpr *BaseProp =
86 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
87 if (BaseProp) {
88 D = getBestPropertyDecl(BaseProp);
89
Akira Hatanaka7e2c82d2016-03-22 05:00:21 +000090 if (BaseProp->isObjectReceiver()) {
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000091 const Expr *DoubleBase = BaseProp->getBase();
92 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
93 DoubleBase = OVE->getSourceExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000094
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000095 IsExact = DoubleBase->isObjCSelfExpr();
96 }
Jordan Rose62b37982012-09-28 22:21:39 +000097 }
98 break;
99 }
100 default:
101 break;
102 }
103
104 return BaseInfoTy(D, IsExact);
105}
106
Alexey Bataev39c81e22014-08-28 04:28:19 +0000107bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +0000108 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000109 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000110 RD = LSI->Lambda;
111 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
112 RD = CRSI->TheRecordDecl;
113
114 if (RD)
115 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000116 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
117 return true;
118 }
119 return false;
120}
121
Jordan Rose62b37982012-09-28 22:21:39 +0000122FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
123 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000124 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000125
126 if (PropE->isObjectReceiver()) {
127 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
128 const Expr *E = OVE->getSourceExpr();
129 Base = getBaseInfo(E);
130 } else if (PropE->isClassReceiver()) {
131 Base.setPointer(PropE->getClassReceiver());
132 } else {
133 assert(PropE->isSuperReceiver());
134 }
135}
136
Jordan Rose22487652012-10-11 16:06:21 +0000137FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
138 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000139 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000140 if (BaseE)
141 Base = getBaseInfo(BaseE);
142 // else, this is a message accessing a property on super.
143}
144
Jordan Rose62b37982012-09-28 22:21:39 +0000145FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
146 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000147 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000148 assert(isa<VarDecl>(Property));
149}
150
151FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
152 const ObjCIvarRefExpr *IvarE)
153 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
154}
155
Jordan Rose22487652012-10-11 16:06:21 +0000156void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
157 const ObjCPropertyDecl *Prop) {
158 assert(Msg && Prop);
159 WeakUseVector &Uses =
160 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
161 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
162}
163
Jordan Rose62b37982012-09-28 22:21:39 +0000164void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000165 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000166
167 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
168 markSafeWeakUse(POE->getSyntacticForm());
169 return;
170 }
171
172 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
173 markSafeWeakUse(Cond->getTrueExpr());
174 markSafeWeakUse(Cond->getFalseExpr());
175 return;
176 }
177
178 if (const BinaryConditionalOperator *Cond =
179 dyn_cast<BinaryConditionalOperator>(E)) {
180 markSafeWeakUse(Cond->getCommon());
181 markSafeWeakUse(Cond->getFalseExpr());
182 return;
183 }
184
185 // Has this weak object been seen before?
186 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000187 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000188 if (!RefExpr->isObjectReceiver())
189 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000190 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
191 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
192 else {
193 markSafeWeakUse(RefExpr->getBase());
194 return;
195 }
196 }
Jordan Rose62b37982012-09-28 22:21:39 +0000197 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000198 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Jordan Rose62b37982012-09-28 22:21:39 +0000199 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000200 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
Jordan Roseb1e3e5f2012-10-11 17:02:00 +0000201 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000202 Uses = WeakObjectUses.end();
203 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
204 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
205 Uses =
206 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
207 Prop));
208 }
209 }
210 }
Jordan Rose62b37982012-09-28 22:21:39 +0000211 else
212 return;
213
214 if (Uses == WeakObjectUses.end())
215 return;
216
217 // Has there been a read from the object using this Expr?
218 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
David Majnemerf7e36092016-06-23 00:15:04 +0000219 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
Jordan Rose62b37982012-09-28 22:21:39 +0000220 if (ThisUse == Uses->second.rend())
221 return;
222
223 ThisUse->markSafe();
224}
225
Faisal Valiab3d6462013-12-07 20:22:44 +0000226void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
227 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000228 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000229 "Index of potential capture must be within 0 to less than the "
230 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000231 E = PotentiallyCapturingExprs[Idx];
232 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
233 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
234 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
235 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
236 else
237 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
238 "potential captures");
239 assert(VD);
240}
241
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000242FunctionScopeInfo::~FunctionScopeInfo() { }
243BlockScopeInfo::~BlockScopeInfo() { }
244CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }