blob: 6d5088504c494ac2684e8011c888ffb47476e335 [file] [log] [blame]
Jordan Rose62b37982012-09-28 22:21:39 +00001//===--- ScopeInfo.cpp - Information about a semantic context -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements FunctionScopeInfo and its subclasses, which contain
11// information about a single function, block, lambda, or method body.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Sema/ScopeInfo.h"
16#include "clang/AST/Decl.h"
17#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;
30 ObjCShouldCallSuper = false;
31 ObjCIsDesignatedInit = false;
32 ObjCWarnForNoDesignatedInitChain = false;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +000033 ObjCIsSecondaryInit = false;
34 ObjCWarnForNoInitDelegation = false;
Jordan Rose62b37982012-09-28 22:21:39 +000035
36 SwitchStack.clear();
37 Returns.clear();
38 ErrorTrap.reset();
39 PossiblyUnreachableDiags.clear();
40 WeakObjectUses.clear();
41}
42
43static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
44 if (PropE->isExplicitProperty())
45 return PropE->getExplicitProperty();
46
47 return PropE->getImplicitPropertyGetter();
48}
49
Jordan Rose62b37982012-09-28 22:21:39 +000050FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
51FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
52 E = E->IgnoreParenCasts();
53
54 const NamedDecl *D = 0;
55 bool IsExact = false;
56
57 switch (E->getStmtClass()) {
58 case Stmt::DeclRefExprClass:
59 D = cast<DeclRefExpr>(E)->getDecl();
60 IsExact = isa<VarDecl>(D);
61 break;
62 case Stmt::MemberExprClass: {
63 const MemberExpr *ME = cast<MemberExpr>(E);
64 D = ME->getMemberDecl();
65 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
66 break;
67 }
68 case Stmt::ObjCIvarRefExprClass: {
69 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
70 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000071 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000072 break;
73 }
74 case Stmt::PseudoObjectExprClass: {
75 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
76 const ObjCPropertyRefExpr *BaseProp =
77 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
78 if (BaseProp) {
79 D = getBestPropertyDecl(BaseProp);
80
81 const Expr *DoubleBase = BaseProp->getBase();
82 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
83 DoubleBase = OVE->getSourceExpr();
84
Anna Zaks97c7ce32012-10-01 20:34:04 +000085 IsExact = DoubleBase->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000086 }
87 break;
88 }
89 default:
90 break;
91 }
92
93 return BaseInfoTy(D, IsExact);
94}
95
96
97FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
98 const ObjCPropertyRefExpr *PropE)
99 : Base(0, true), Property(getBestPropertyDecl(PropE)) {
100
101 if (PropE->isObjectReceiver()) {
102 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
103 const Expr *E = OVE->getSourceExpr();
104 Base = getBaseInfo(E);
105 } else if (PropE->isClassReceiver()) {
106 Base.setPointer(PropE->getClassReceiver());
107 } else {
108 assert(PropE->isSuperReceiver());
109 }
110}
111
Jordan Rose22487652012-10-11 16:06:21 +0000112FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
113 const ObjCPropertyDecl *Prop)
114 : Base(0, true), Property(Prop) {
115 if (BaseE)
116 Base = getBaseInfo(BaseE);
117 // else, this is a message accessing a property on super.
118}
119
Jordan Rose62b37982012-09-28 22:21:39 +0000120FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
121 const DeclRefExpr *DRE)
122 : Base(0, true), Property(DRE->getDecl()) {
123 assert(isa<VarDecl>(Property));
124}
125
126FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
127 const ObjCIvarRefExpr *IvarE)
128 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
129}
130
Jordan Rose22487652012-10-11 16:06:21 +0000131void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
132 const ObjCPropertyDecl *Prop) {
133 assert(Msg && Prop);
134 WeakUseVector &Uses =
135 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
136 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
137}
138
Jordan Rose62b37982012-09-28 22:21:39 +0000139void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000140 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000141
142 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
143 markSafeWeakUse(POE->getSyntacticForm());
144 return;
145 }
146
147 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
148 markSafeWeakUse(Cond->getTrueExpr());
149 markSafeWeakUse(Cond->getFalseExpr());
150 return;
151 }
152
153 if (const BinaryConditionalOperator *Cond =
154 dyn_cast<BinaryConditionalOperator>(E)) {
155 markSafeWeakUse(Cond->getCommon());
156 markSafeWeakUse(Cond->getFalseExpr());
157 return;
158 }
159
160 // Has this weak object been seen before?
161 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
162 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000163 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
Jordan Rose62b37982012-09-28 22:21:39 +0000164 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000165 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Jordan Rose62b37982012-09-28 22:21:39 +0000166 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000167 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
Jordan Roseb1e3e5f2012-10-11 17:02:00 +0000168 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000169 Uses = WeakObjectUses.end();
170 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
171 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
172 Uses =
173 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
174 Prop));
175 }
176 }
177 }
Jordan Rose62b37982012-09-28 22:21:39 +0000178 else
179 return;
180
181 if (Uses == WeakObjectUses.end())
182 return;
183
184 // Has there been a read from the object using this Expr?
185 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
186 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
187 if (ThisUse == Uses->second.rend())
188 return;
189
190 ThisUse->markSafe();
191}
192
Faisal Valia17d19f2013-11-07 05:17:06 +0000193void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, Expr *&E) {
Faisal Vali9366a482013-11-07 16:57:56 +0000194 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valia17d19f2013-11-07 05:17:06 +0000195 "Index of potential capture must be within 0 to less than the "
196 "number of captures!");
197 E = PotentiallyCapturingExprs[Idx];
198 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
199 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
200 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
201 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
202 else
203 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
204 "potential captures");
205 assert(VD);
206}
207
Jordan Rose62b37982012-09-28 22:21:39 +0000208FunctionScopeInfo::~FunctionScopeInfo() { }
209BlockScopeInfo::~BlockScopeInfo() { }
210LambdaScopeInfo::~LambdaScopeInfo() { }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000211CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }