blob: 1003d2639c0e0c8dcd572660ad5d19a107985cb2 [file] [log] [blame]
Jordan Rose62b37982012-09-28 22:21:39 +00001//===--- ScopeInfo.cpp - Information about a semantic context -------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jordan Rose62b37982012-09-28 22:21:39 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements FunctionScopeInfo and its subclasses, which contain
10// information about a single function, block, lambda, or method body.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/ScopeInfo.h"
15#include "clang/AST/Decl.h"
Alexey Bataev39c81e22014-08-28 04:28:19 +000016#include "clang/AST/DeclCXX.h"
Jordan Rose62b37982012-09-28 22:21:39 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/Expr.h"
19#include "clang/AST/ExprCXX.h"
20#include "clang/AST/ExprObjC.h"
21
22using namespace clang;
23using namespace sema;
24
25void FunctionScopeInfo::Clear() {
26 HasBranchProtectedScope = false;
27 HasBranchIntoScope = false;
28 HasIndirectGoto = false;
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +000029 HasDroppedStmt = false;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000030 HasOMPDeclareReductionCombiner = false;
Erik Pilkington5dbe7a92016-11-09 22:52:23 +000031 HasFallthroughStmt = 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
Eric Fiseliercac0a592017-03-11 02:35:37 +000042 // Coroutine state
43 FirstCoroutineStmtLoc = SourceLocation();
Richard Smithdcd04d12015-10-27 07:47:45 +000044 CoroutinePromise = nullptr;
Brian Gesiak61f4ac92018-01-24 22:15:42 +000045 CoroutineParameterMoves.clear();
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();
Akira Hatanaka8e57b072018-10-01 21:51:28 +000056 Blocks.clear();
57 ByrefBlockVars.clear();
Jordan Rose62b37982012-09-28 22:21:39 +000058}
59
60static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
61 if (PropE->isExplicitProperty())
62 return PropE->getExplicitProperty();
63
64 return PropE->getImplicitPropertyGetter();
65}
66
Jordan Rose62b37982012-09-28 22:21:39 +000067FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
68FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
69 E = E->IgnoreParenCasts();
70
Craig Topperc3ec1492014-05-26 06:22:03 +000071 const NamedDecl *D = nullptr;
Jordan Rose62b37982012-09-28 22:21:39 +000072 bool IsExact = false;
73
74 switch (E->getStmtClass()) {
75 case Stmt::DeclRefExprClass:
76 D = cast<DeclRefExpr>(E)->getDecl();
77 IsExact = isa<VarDecl>(D);
78 break;
79 case Stmt::MemberExprClass: {
80 const MemberExpr *ME = cast<MemberExpr>(E);
81 D = ME->getMemberDecl();
82 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
83 break;
84 }
85 case Stmt::ObjCIvarRefExprClass: {
86 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
87 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000088 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000089 break;
90 }
91 case Stmt::PseudoObjectExprClass: {
92 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
93 const ObjCPropertyRefExpr *BaseProp =
94 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
95 if (BaseProp) {
96 D = getBestPropertyDecl(BaseProp);
97
Akira Hatanaka7e2c82d2016-03-22 05:00:21 +000098 if (BaseProp->isObjectReceiver()) {
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +000099 const Expr *DoubleBase = BaseProp->getBase();
100 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
101 DoubleBase = OVE->getSourceExpr();
Jordan Rose62b37982012-09-28 22:21:39 +0000102
Akira Hatanaka4c62c7c2016-03-18 19:03:50 +0000103 IsExact = DoubleBase->isObjCSelfExpr();
104 }
Jordan Rose62b37982012-09-28 22:21:39 +0000105 }
106 break;
107 }
108 default:
109 break;
110 }
111
112 return BaseInfoTy(D, IsExact);
113}
114
Alexey Bataev39c81e22014-08-28 04:28:19 +0000115bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
Alexey Bataev330de032014-10-29 12:21:55 +0000116 RecordDecl *RD = nullptr;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000117 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
Alexey Bataev330de032014-10-29 12:21:55 +0000118 RD = LSI->Lambda;
119 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
120 RD = CRSI->TheRecordDecl;
121
122 if (RD)
123 for (auto *FD : RD->fields()) {
Alexey Bataev39c81e22014-08-28 04:28:19 +0000124 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
125 return true;
126 }
127 return false;
128}
129
Jordan Rose62b37982012-09-28 22:21:39 +0000130FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
131 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000132 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000133
134 if (PropE->isObjectReceiver()) {
135 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
136 const Expr *E = OVE->getSourceExpr();
137 Base = getBaseInfo(E);
138 } else if (PropE->isClassReceiver()) {
139 Base.setPointer(PropE->getClassReceiver());
140 } else {
141 assert(PropE->isSuperReceiver());
142 }
143}
144
Jordan Rose22487652012-10-11 16:06:21 +0000145FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
146 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000147 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000148 if (BaseE)
149 Base = getBaseInfo(BaseE);
150 // else, this is a message accessing a property on super.
151}
152
Jordan Rose62b37982012-09-28 22:21:39 +0000153FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
154 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000155 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000156 assert(isa<VarDecl>(Property));
157}
158
159FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
160 const ObjCIvarRefExpr *IvarE)
161 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
162}
163
Jordan Rose22487652012-10-11 16:06:21 +0000164void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
165 const ObjCPropertyDecl *Prop) {
166 assert(Msg && Prop);
167 WeakUseVector &Uses =
168 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
169 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
170}
171
Jordan Rose62b37982012-09-28 22:21:39 +0000172void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000173 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000174
175 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
176 markSafeWeakUse(POE->getSyntacticForm());
177 return;
178 }
179
180 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
181 markSafeWeakUse(Cond->getTrueExpr());
182 markSafeWeakUse(Cond->getFalseExpr());
183 return;
184 }
185
186 if (const BinaryConditionalOperator *Cond =
187 dyn_cast<BinaryConditionalOperator>(E)) {
188 markSafeWeakUse(Cond->getCommon());
189 markSafeWeakUse(Cond->getFalseExpr());
190 return;
191 }
192
193 // Has this weak object been seen before?
Akira Hatanaka034c6332017-02-01 20:22:26 +0000194 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000195 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000196 if (!RefExpr->isObjectReceiver())
197 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000198 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
199 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
200 else {
201 markSafeWeakUse(RefExpr->getBase());
202 return;
203 }
204 }
Jordan Rose62b37982012-09-28 22:21:39 +0000205 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000206 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Akira Hatanaka034c6332017-02-01 20:22:26 +0000207 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
208 if (isa<VarDecl>(DRE->getDecl()))
209 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
210 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000211 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
212 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
213 Uses =
214 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
215 Prop));
216 }
217 }
218 }
Jordan Rose62b37982012-09-28 22:21:39 +0000219 else
220 return;
221
222 if (Uses == WeakObjectUses.end())
223 return;
224
225 // Has there been a read from the object using this Expr?
226 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
David Majnemerf7e36092016-06-23 00:15:04 +0000227 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
Jordan Rose62b37982012-09-28 22:21:39 +0000228 if (ThisUse == Uses->second.rend())
229 return;
230
231 ThisUse->markSafe();
232}
233
Faisal Valiab3d6462013-12-07 20:22:44 +0000234void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
235 Expr *&E) const {
Faisal Vali9366a482013-11-07 16:57:56 +0000236 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valiab3d6462013-12-07 20:22:44 +0000237 "Index of potential capture must be within 0 to less than the "
238 "number of captures!");
Faisal Valia17d19f2013-11-07 05:17:06 +0000239 E = PotentiallyCapturingExprs[Idx];
240 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
241 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
242 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
243 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
244 else
245 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
246 "potential captures");
247 assert(VD);
248}
249
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000250FunctionScopeInfo::~FunctionScopeInfo() { }
251BlockScopeInfo::~BlockScopeInfo() { }
252CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }