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;
40 PossiblyUnreachableDiags.clear();
41 WeakObjectUses.clear();
42 ModifiedNonNullParams.clear();
45 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
46 if (PropE->isExplicitProperty())
47 return PropE->getExplicitProperty();
49 return PropE->getImplicitPropertyGetter();
52 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
53 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
54 E = E->IgnoreParenCasts();
56 const NamedDecl *D = nullptr;
59 switch (E->getStmtClass()) {
60 case Stmt::DeclRefExprClass:
61 D = cast<DeclRefExpr>(E)->getDecl();
62 IsExact = isa<VarDecl>(D);
64 case Stmt::MemberExprClass: {
65 const MemberExpr *ME = cast<MemberExpr>(E);
66 D = ME->getMemberDecl();
67 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
70 case Stmt::ObjCIvarRefExprClass: {
71 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
73 IsExact = IE->getBase()->isObjCSelfExpr();
76 case Stmt::PseudoObjectExprClass: {
77 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
78 const ObjCPropertyRefExpr *BaseProp =
79 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
81 D = getBestPropertyDecl(BaseProp);
83 const Expr *DoubleBase = BaseProp->getBase();
84 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
85 DoubleBase = OVE->getSourceExpr();
87 IsExact = DoubleBase->isObjCSelfExpr();
95 return BaseInfoTy(D, IsExact);
98 bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
99 RecordDecl *RD = nullptr;
100 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
102 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
103 RD = CRSI->TheRecordDecl;
106 for (auto *FD : RD->fields()) {
107 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
113 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
114 const ObjCPropertyRefExpr *PropE)
115 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
117 if (PropE->isObjectReceiver()) {
118 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
119 const Expr *E = OVE->getSourceExpr();
120 Base = getBaseInfo(E);
121 } else if (PropE->isClassReceiver()) {
122 Base.setPointer(PropE->getClassReceiver());
124 assert(PropE->isSuperReceiver());
128 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
129 const ObjCPropertyDecl *Prop)
130 : Base(nullptr, true), Property(Prop) {
132 Base = getBaseInfo(BaseE);
133 // else, this is a message accessing a property on super.
136 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
137 const DeclRefExpr *DRE)
138 : Base(nullptr, true), Property(DRE->getDecl()) {
139 assert(isa<VarDecl>(Property));
142 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
143 const ObjCIvarRefExpr *IvarE)
144 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
147 void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
148 const ObjCPropertyDecl *Prop) {
150 WeakUseVector &Uses =
151 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
152 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
155 void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
156 E = E->IgnoreParenCasts();
158 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
159 markSafeWeakUse(POE->getSyntacticForm());
163 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
164 markSafeWeakUse(Cond->getTrueExpr());
165 markSafeWeakUse(Cond->getFalseExpr());
169 if (const BinaryConditionalOperator *Cond =
170 dyn_cast<BinaryConditionalOperator>(E)) {
171 markSafeWeakUse(Cond->getCommon());
172 markSafeWeakUse(Cond->getFalseExpr());
176 // Has this weak object been seen before?
177 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
178 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
179 if (!RefExpr->isObjectReceiver())
181 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
182 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
184 markSafeWeakUse(RefExpr->getBase());
188 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
189 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
190 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
191 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
192 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
193 Uses = WeakObjectUses.end();
194 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
195 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
197 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
205 if (Uses == WeakObjectUses.end())
208 // Has there been a read from the object using this Expr?
209 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
210 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
211 if (ThisUse == Uses->second.rend())
217 void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
219 assert(Idx < getNumPotentialVariableCaptures() &&
220 "Index of potential capture must be within 0 to less than the "
221 "number of captures!");
222 E = PotentiallyCapturingExprs[Idx];
223 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
224 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
225 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
226 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
228 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
229 "potential captures");
233 FunctionScopeInfo::~FunctionScopeInfo() { }
234 BlockScopeInfo::~BlockScopeInfo() { }
235 LambdaScopeInfo::~LambdaScopeInfo() { }
236 CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }