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();
58 ByrefBlockVars.clear();
61 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
62 if (PropE->isExplicitProperty())
63 return PropE->getExplicitProperty();
65 return PropE->getImplicitPropertyGetter();
68 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
69 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
70 E = E->IgnoreParenCasts();
72 const NamedDecl *D = nullptr;
75 switch (E->getStmtClass()) {
76 case Stmt::DeclRefExprClass:
77 D = cast<DeclRefExpr>(E)->getDecl();
78 IsExact = isa<VarDecl>(D);
80 case Stmt::MemberExprClass: {
81 const MemberExpr *ME = cast<MemberExpr>(E);
82 D = ME->getMemberDecl();
83 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
86 case Stmt::ObjCIvarRefExprClass: {
87 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
89 IsExact = IE->getBase()->isObjCSelfExpr();
92 case Stmt::PseudoObjectExprClass: {
93 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
94 const ObjCPropertyRefExpr *BaseProp =
95 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
97 D = getBestPropertyDecl(BaseProp);
99 if (BaseProp->isObjectReceiver()) {
100 const Expr *DoubleBase = BaseProp->getBase();
101 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
102 DoubleBase = OVE->getSourceExpr();
104 IsExact = DoubleBase->isObjCSelfExpr();
113 return BaseInfoTy(D, IsExact);
116 bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
117 RecordDecl *RD = nullptr;
118 if (auto *LSI = dyn_cast<LambdaScopeInfo>(this))
120 else if (auto CRSI = dyn_cast<CapturedRegionScopeInfo>(this))
121 RD = CRSI->TheRecordDecl;
124 for (auto *FD : RD->fields()) {
125 if (FD->hasCapturedVLAType() && FD->getCapturedVLAType() == VAT)
131 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
132 const ObjCPropertyRefExpr *PropE)
133 : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
135 if (PropE->isObjectReceiver()) {
136 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
137 const Expr *E = OVE->getSourceExpr();
138 Base = getBaseInfo(E);
139 } else if (PropE->isClassReceiver()) {
140 Base.setPointer(PropE->getClassReceiver());
142 assert(PropE->isSuperReceiver());
146 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
147 const ObjCPropertyDecl *Prop)
148 : Base(nullptr, true), Property(Prop) {
150 Base = getBaseInfo(BaseE);
151 // else, this is a message accessing a property on super.
154 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
155 const DeclRefExpr *DRE)
156 : Base(nullptr, true), Property(DRE->getDecl()) {
157 assert(isa<VarDecl>(Property));
160 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
161 const ObjCIvarRefExpr *IvarE)
162 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
165 void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
166 const ObjCPropertyDecl *Prop) {
168 WeakUseVector &Uses =
169 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
170 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
173 void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
174 E = E->IgnoreParenCasts();
176 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
177 markSafeWeakUse(POE->getSyntacticForm());
181 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
182 markSafeWeakUse(Cond->getTrueExpr());
183 markSafeWeakUse(Cond->getFalseExpr());
187 if (const BinaryConditionalOperator *Cond =
188 dyn_cast<BinaryConditionalOperator>(E)) {
189 markSafeWeakUse(Cond->getCommon());
190 markSafeWeakUse(Cond->getFalseExpr());
194 // Has this weak object been seen before?
195 FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
196 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
197 if (!RefExpr->isObjectReceiver())
199 if (isa<OpaqueValueExpr>(RefExpr->getBase()))
200 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
202 markSafeWeakUse(RefExpr->getBase());
206 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
207 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
208 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
209 if (isa<VarDecl>(DRE->getDecl()))
210 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
211 } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
212 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
213 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
215 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
223 if (Uses == WeakObjectUses.end())
226 // Has there been a read from the object using this Expr?
227 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
228 llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
229 if (ThisUse == Uses->second.rend())
235 void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD,
237 assert(Idx < getNumPotentialVariableCaptures() &&
238 "Index of potential capture must be within 0 to less than the "
239 "number of captures!");
240 E = PotentiallyCapturingExprs[Idx];
241 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
242 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
243 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
244 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
246 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
247 "potential captures");
251 FunctionScopeInfo::~FunctionScopeInfo() { }
252 BlockScopeInfo::~BlockScopeInfo() { }
253 CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }