blob: b2a26af9b4a578c5fd39ed3fe70f8eddd32fe82a [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
Jordan Rose62b37982012-09-28 22:21:39 +0000115FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
116 const ObjCPropertyRefExpr *PropE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000117 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
Jordan Rose62b37982012-09-28 22:21:39 +0000118
119 if (PropE->isObjectReceiver()) {
120 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
121 const Expr *E = OVE->getSourceExpr();
122 Base = getBaseInfo(E);
123 } else if (PropE->isClassReceiver()) {
124 Base.setPointer(PropE->getClassReceiver());
125 } else {
126 assert(PropE->isSuperReceiver());
127 }
128}
129
Jordan Rose22487652012-10-11 16:06:21 +0000130FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
131 const ObjCPropertyDecl *Prop)
Craig Topperc3ec1492014-05-26 06:22:03 +0000132 : Base(nullptr, true), Property(Prop) {
Jordan Rose22487652012-10-11 16:06:21 +0000133 if (BaseE)
134 Base = getBaseInfo(BaseE);
135 // else, this is a message accessing a property on super.
136}
137
Jordan Rose62b37982012-09-28 22:21:39 +0000138FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
139 const DeclRefExpr *DRE)
Craig Topperc3ec1492014-05-26 06:22:03 +0000140 : Base(nullptr, true), Property(DRE->getDecl()) {
Jordan Rose62b37982012-09-28 22:21:39 +0000141 assert(isa<VarDecl>(Property));
142}
143
144FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
145 const ObjCIvarRefExpr *IvarE)
146 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
147}
148
Jordan Rose22487652012-10-11 16:06:21 +0000149void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
150 const ObjCPropertyDecl *Prop) {
151 assert(Msg && Prop);
152 WeakUseVector &Uses =
153 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
154 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
155}
156
Jordan Rose62b37982012-09-28 22:21:39 +0000157void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000158 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000159
160 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
161 markSafeWeakUse(POE->getSyntacticForm());
162 return;
163 }
164
165 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
166 markSafeWeakUse(Cond->getTrueExpr());
167 markSafeWeakUse(Cond->getFalseExpr());
168 return;
169 }
170
171 if (const BinaryConditionalOperator *Cond =
172 dyn_cast<BinaryConditionalOperator>(E)) {
173 markSafeWeakUse(Cond->getCommon());
174 markSafeWeakUse(Cond->getFalseExpr());
175 return;
176 }
177
178 // Has this weak object been seen before?
Akira Hatanaka034c6332017-02-01 20:22:26 +0000179 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000180 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
Fariborz Jahanianac1c5122014-11-21 21:12:11 +0000181 if (!RefExpr->isObjectReceiver())
182 return;
Fariborz Jahanian9277ff42014-06-17 23:35:13 +0000183 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
184 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
185 else {
186 markSafeWeakUse(RefExpr->getBase());
187 return;
188 }
189 }
Jordan Rose62b37982012-09-28 22:21:39 +0000190 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000191 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Akira Hatanaka034c6332017-02-01 20:22:26 +0000192 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
193 if (isa<VarDecl>(DRE->getDecl()))
194 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
195 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000196 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
197 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
198 Uses =
199 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
200 Prop));
201 }
202 }
203 }
Jordan Rose62b37982012-09-28 22:21:39 +0000204 else
205 return;
206
207 if (Uses == WeakObjectUses.end())
208 return;
209
210 // Has there been a read from the object using this Expr?
211 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
David Majnemerf7e36092016-06-23 00:15:04 +0000212 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
Jordan Rose62b37982012-09-28 22:21:39 +0000213 if (ThisUse == Uses->second.rend())
214 return;
215
216 ThisUse->markSafe();
217}
218
Richard Smith0621a8f2019-05-31 00:45:10 +0000219bool Capture::isInitCapture() const {
220 // Note that a nested capture of an init-capture is not itself an
221 // init-capture.
222 return !isNested() && isVariableCapture() && getVariable()->isInitCapture();
223}
224
225bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
226 for (auto &Cap : Captures)
227 if (Cap.isVLATypeCapture() && Cap.getCapturedVLAType() == VAT)
228 return true;
229 return false;
230}
231
Richard Smith7bf8f6f2019-06-04 17:17:20 +0000232void LambdaScopeInfo::visitPotentialCaptures(
233 llvm::function_ref<void(VarDecl *, Expr *)> Callback) const {
234 for (Expr *E : PotentiallyCapturingExprs) {
235 if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
236 Callback(cast<VarDecl>(DRE->getFoundDecl()), E);
237 } else if (auto *ME = dyn_cast<MemberExpr>(E)) {
238 Callback(cast<VarDecl>(ME->getMemberDecl()), E);
239 } else if (auto *FP = dyn_cast<FunctionParmPackExpr>(E)) {
240 for (VarDecl *VD : *FP)
241 Callback(VD, E);
242 } else {
243 llvm_unreachable("unexpected expression in potential captures list");
244 }
245 }
Faisal Valia17d19f2013-11-07 05:17:06 +0000246}
247
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000248FunctionScopeInfo::~FunctionScopeInfo() { }
249BlockScopeInfo::~BlockScopeInfo() { }
250CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }