1 //===--- ScopeInfo.cpp - Information about a semantic context -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements FunctionScopeInfo and its subclasses, which contain
11 // information about a single function, block, lambda, or method body.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Sema/ScopeInfo.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/ExprCXX.h"
21 #include "clang/AST/ExprObjC.h"
23 using namespace clang;
26 void FunctionScopeInfo::Clear() {
27 HasBranchProtectedScope = false;
28 HasBranchIntoScope = false;
29 HasIndirectGoto = false;
30 HasDroppedStmt = false;
31 HasOMPDeclareReductionCombiner = false;
32 HasFallthroughStmt = false;
33 HasPotentialAvailabilityViolations = false;
34 ObjCShouldCallSuper = false;
35 ObjCIsDesignatedInit = false;
36 ObjCWarnForNoDesignatedInitChain = false;
37 ObjCIsSecondaryInit = false;
38 ObjCWarnForNoInitDelegation = false;
39 FirstReturnLoc = SourceLocation();
40 FirstCXXTryLoc = SourceLocation();
41 FirstSEHTryLoc = SourceLocation();
44 FirstCoroutineStmtLoc = SourceLocation();
45 CoroutinePromise = nullptr;
46 CoroutineParameterMoves.clear();
47 NeedsCoroutineSuspends = true;
48 CoroutineSuspends.first = nullptr;
49 CoroutineSuspends.second = nullptr;
54 PossiblyUnreachableDiags.clear();
55 WeakObjectUses.clear();
56 ModifiedNonNullParams.clear();
59 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
60 if (PropE->isExplicitProperty())
61 return PropE->getExplicitProperty();
63 return PropE->getImplicitPropertyGetter();
66 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
67 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
68 E = E->IgnoreParenCasts();
70 const NamedDecl *D = nullptr;
73 switch (E->getStmtClass()) {
74 case Stmt::DeclRefExprClass:
75 D = cast<DeclRefExpr>(E)->getDecl();
76 IsExact = isa<VarDecl>(D);
78 case Stmt::MemberExprClass: {
79 const MemberExpr *ME = cast<MemberExpr>(E);
80 D = ME->getMemberDecl();
81 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
84 case Stmt::ObjCIvarRefExprClass: {
85 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
87 IsExact = IE->getBase()->isObjCSelfExpr();
90 case Stmt::PseudoObjectExprClass: {
91 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
92 const ObjCPropertyRefExpr *BaseProp =
93 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
95 D = getBestPropertyDecl(BaseProp);
97 if (BaseProp->isObjectReceiver()) {
98 const Expr *DoubleBase = BaseProp->getBase();
99 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
100 DoubleBase = OVE->getSourceExpr();
102 IsExact = DoubleBase->isObjCSelfExpr();
111 return BaseInfoTy(D, IsExact);
114 bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
115 RecordDecl *RD = nullptr;
116 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
118 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
119 RD = CRSI->TheRecordDecl;
122 for (auto *FD : RD->fields()) {
123 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
129 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
130 const ObjCPropertyRefExpr *PropE)
131 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
133 if (PropE->isObjectReceiver()) {
134 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
135 const Expr *E = OVE->getSourceExpr();
136 Base = getBaseInfo(E);
137 } else if (PropE->isClassReceiver()) {
138 Base.setPointer(PropE->getClassReceiver());
140 assert(PropE->isSuperReceiver());
144 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
145 const ObjCPropertyDecl *Prop)
146 : Base(nullptr, true), Property(Prop) {
148 Base = getBaseInfo(BaseE);
149 // else, this is a message accessing a property on super.
152 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
153 const DeclRefExpr *DRE)
154 : Base(nullptr, true), Property(DRE->getDecl()) {
155 assert(isa<VarDecl>(Property));
158 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
159 const ObjCIvarRefExpr *IvarE)
160 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
163 void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
164 const ObjCPropertyDecl *Prop) {
166 WeakUseVector &Uses =
167 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
168 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
171 void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
172 E = E->IgnoreParenCasts();
174 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
175 markSafeWeakUse(POE->getSyntacticForm());
179 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
180 markSafeWeakUse(Cond->getTrueExpr());
181 markSafeWeakUse(Cond->getFalseExpr());
185 if (const BinaryConditionalOperator *Cond =
186 dyn_cast<BinaryConditionalOperator>(E)) {
187 markSafeWeakUse(Cond->getCommon());
188 markSafeWeakUse(Cond->getFalseExpr());
192 // Has this weak object been seen before?
193 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
194 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
195 if (!RefExpr->isObjectReceiver())
197 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
198 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
200 markSafeWeakUse(RefExpr->getBase());
204 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
205 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
206 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
207 if (isa<VarDecl>(DRE->getDecl()))
208 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
209 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
210 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
211 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
213 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
221 if (Uses == WeakObjectUses.end())
224 // Has there been a read from the object using this Expr?
225 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
226 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
227 if (ThisUse == Uses->second.rend())
233 void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
235 assert(Idx < getNumPotentialVariableCaptures() &&
236 "Index of potential capture must be within 0 to less than the "
237 "number of captures!");
238 E = PotentiallyCapturingExprs[Idx];
239 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
240 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
241 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
242 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
244 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
245 "potential captures");
249 FunctionScopeInfo::~FunctionScopeInfo() { }
250 BlockScopeInfo::~BlockScopeInfo() { }
251 CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }