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 ObjCShouldCallSuper = false;
32 ObjCIsDesignatedInit = false;
33 ObjCWarnForNoDesignatedInitChain = false;
34 ObjCIsSecondaryInit = false;
35 ObjCWarnForNoInitDelegation = false;
36 FirstReturnLoc = SourceLocation();
37 FirstCXXTryLoc = SourceLocation();
38 FirstSEHTryLoc = SourceLocation();
42 CoroutinePromise = nullptr;
43 CoroutineStmts.clear();
45 PossiblyUnreachableDiags.clear();
46 WeakObjectUses.clear();
47 ModifiedNonNullParams.clear();
50 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
51 if (PropE->isExplicitProperty())
52 return PropE->getExplicitProperty();
54 return PropE->getImplicitPropertyGetter();
57 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
58 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
59 E = E->IgnoreParenCasts();
61 const NamedDecl *D = nullptr;
64 switch (E->getStmtClass()) {
65 case Stmt::DeclRefExprClass:
66 D = cast<DeclRefExpr>(E)->getDecl();
67 IsExact = isa<VarDecl>(D);
69 case Stmt::MemberExprClass: {
70 const MemberExpr *ME = cast<MemberExpr>(E);
71 D = ME->getMemberDecl();
72 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
75 case Stmt::ObjCIvarRefExprClass: {
76 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
78 IsExact = IE->getBase()->isObjCSelfExpr();
81 case Stmt::PseudoObjectExprClass: {
82 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
83 const ObjCPropertyRefExpr *BaseProp =
84 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
86 D = getBestPropertyDecl(BaseProp);
88 const Expr *DoubleBase = BaseProp->getBase();
89 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
90 DoubleBase = OVE->getSourceExpr();
92 IsExact = DoubleBase->isObjCSelfExpr();
100 return BaseInfoTy(D, IsExact);
103 bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
104 RecordDecl *RD = nullptr;
105 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
107 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
108 RD = CRSI->TheRecordDecl;
111 for (auto *FD : RD->fields()) {
112 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
118 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
119 const ObjCPropertyRefExpr *PropE)
120 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
122 if (PropE->isObjectReceiver()) {
123 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
124 const Expr *E = OVE->getSourceExpr();
125 Base = getBaseInfo(E);
126 } else if (PropE->isClassReceiver()) {
127 Base.setPointer(PropE->getClassReceiver());
129 assert(PropE->isSuperReceiver());
133 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
134 const ObjCPropertyDecl *Prop)
135 : Base(nullptr, true), Property(Prop) {
137 Base = getBaseInfo(BaseE);
138 // else, this is a message accessing a property on super.
141 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
142 const DeclRefExpr *DRE)
143 : Base(nullptr, true), Property(DRE->getDecl()) {
144 assert(isa<VarDecl>(Property));
147 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
148 const ObjCIvarRefExpr *IvarE)
149 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
152 void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
153 const ObjCPropertyDecl *Prop) {
155 WeakUseVector &Uses =
156 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
157 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
160 void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
161 E = E->IgnoreParenCasts();
163 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
164 markSafeWeakUse(POE->getSyntacticForm());
168 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
169 markSafeWeakUse(Cond->getTrueExpr());
170 markSafeWeakUse(Cond->getFalseExpr());
174 if (const BinaryConditionalOperator *Cond =
175 dyn_cast<BinaryConditionalOperator>(E)) {
176 markSafeWeakUse(Cond->getCommon());
177 markSafeWeakUse(Cond->getFalseExpr());
181 // Has this weak object been seen before?
182 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
183 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
184 if (!RefExpr->isObjectReceiver())
186 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
187 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
189 markSafeWeakUse(RefExpr->getBase());
193 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
194 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
195 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
196 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
197 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
198 Uses = WeakObjectUses.end();
199 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
200 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
202 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
210 if (Uses == WeakObjectUses.end())
213 // Has there been a read from the object using this Expr?
214 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
215 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
216 if (ThisUse == Uses->second.rend())
222 void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
224 assert(Idx < getNumPotentialVariableCaptures() &&
225 "Index of potential capture must be within 0 to less than the "
226 "number of captures!");
227 E = PotentiallyCapturingExprs[Idx];
228 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
229 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
230 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
231 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
233 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
234 "potential captures");
238 FunctionScopeInfo::~FunctionScopeInfo() { }
239 BlockScopeInfo::~BlockScopeInfo() { }
240 CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }