blob: cf2b5ebe0e5c4b4a899d1c526d5bbddcea046430 [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;
Jordan Rose62b37982012-09-28 22:21:39 +000033
34 SwitchStack.clear();
35 Returns.clear();
36 ErrorTrap.reset();
37 PossiblyUnreachableDiags.clear();
38 WeakObjectUses.clear();
39}
40
41static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
42 if (PropE->isExplicitProperty())
43 return PropE->getExplicitProperty();
44
45 return PropE->getImplicitPropertyGetter();
46}
47
Jordan Rose62b37982012-09-28 22:21:39 +000048FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
49FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
50 E = E->IgnoreParenCasts();
51
52 const NamedDecl *D = 0;
53 bool IsExact = false;
54
55 switch (E->getStmtClass()) {
56 case Stmt::DeclRefExprClass:
57 D = cast<DeclRefExpr>(E)->getDecl();
58 IsExact = isa<VarDecl>(D);
59 break;
60 case Stmt::MemberExprClass: {
61 const MemberExpr *ME = cast<MemberExpr>(E);
62 D = ME->getMemberDecl();
63 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
64 break;
65 }
66 case Stmt::ObjCIvarRefExprClass: {
67 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
68 D = IE->getDecl();
Anna Zaks97c7ce32012-10-01 20:34:04 +000069 IsExact = IE->getBase()->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000070 break;
71 }
72 case Stmt::PseudoObjectExprClass: {
73 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
74 const ObjCPropertyRefExpr *BaseProp =
75 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
76 if (BaseProp) {
77 D = getBestPropertyDecl(BaseProp);
78
79 const Expr *DoubleBase = BaseProp->getBase();
80 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
81 DoubleBase = OVE->getSourceExpr();
82
Anna Zaks97c7ce32012-10-01 20:34:04 +000083 IsExact = DoubleBase->isObjCSelfExpr();
Jordan Rose62b37982012-09-28 22:21:39 +000084 }
85 break;
86 }
87 default:
88 break;
89 }
90
91 return BaseInfoTy(D, IsExact);
92}
93
94
95FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
96 const ObjCPropertyRefExpr *PropE)
97 : Base(0, true), Property(getBestPropertyDecl(PropE)) {
98
99 if (PropE->isObjectReceiver()) {
100 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
101 const Expr *E = OVE->getSourceExpr();
102 Base = getBaseInfo(E);
103 } else if (PropE->isClassReceiver()) {
104 Base.setPointer(PropE->getClassReceiver());
105 } else {
106 assert(PropE->isSuperReceiver());
107 }
108}
109
Jordan Rose22487652012-10-11 16:06:21 +0000110FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
111 const ObjCPropertyDecl *Prop)
112 : Base(0, true), Property(Prop) {
113 if (BaseE)
114 Base = getBaseInfo(BaseE);
115 // else, this is a message accessing a property on super.
116}
117
Jordan Rose62b37982012-09-28 22:21:39 +0000118FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
119 const DeclRefExpr *DRE)
120 : Base(0, true), Property(DRE->getDecl()) {
121 assert(isa<VarDecl>(Property));
122}
123
124FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
125 const ObjCIvarRefExpr *IvarE)
126 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
127}
128
Jordan Rose22487652012-10-11 16:06:21 +0000129void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
130 const ObjCPropertyDecl *Prop) {
131 assert(Msg && Prop);
132 WeakUseVector &Uses =
133 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
134 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
135}
136
Jordan Rose62b37982012-09-28 22:21:39 +0000137void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
Jordan Rosee723a272012-10-10 16:43:06 +0000138 E = E->IgnoreParenCasts();
Jordan Rose62b37982012-09-28 22:21:39 +0000139
140 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
141 markSafeWeakUse(POE->getSyntacticForm());
142 return;
143 }
144
145 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
146 markSafeWeakUse(Cond->getTrueExpr());
147 markSafeWeakUse(Cond->getFalseExpr());
148 return;
149 }
150
151 if (const BinaryConditionalOperator *Cond =
152 dyn_cast<BinaryConditionalOperator>(E)) {
153 markSafeWeakUse(Cond->getCommon());
154 markSafeWeakUse(Cond->getFalseExpr());
155 return;
156 }
157
158 // Has this weak object been seen before?
159 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
160 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000161 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
Jordan Rose62b37982012-09-28 22:21:39 +0000162 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000163 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
Jordan Rose62b37982012-09-28 22:21:39 +0000164 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
Jordan Rose22487652012-10-11 16:06:21 +0000165 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
Jordan Roseb1e3e5f2012-10-11 17:02:00 +0000166 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
Jordan Rose22487652012-10-11 16:06:21 +0000167 Uses = WeakObjectUses.end();
168 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
169 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
170 Uses =
171 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
172 Prop));
173 }
174 }
175 }
Jordan Rose62b37982012-09-28 22:21:39 +0000176 else
177 return;
178
179 if (Uses == WeakObjectUses.end())
180 return;
181
182 // Has there been a read from the object using this Expr?
183 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
184 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
185 if (ThisUse == Uses->second.rend())
186 return;
187
188 ThisUse->markSafe();
189}
190
Faisal Valia17d19f2013-11-07 05:17:06 +0000191void LambdaScopeInfo::getPotentialVariableCapture(unsigned Idx, VarDecl *&VD, Expr *&E) {
Faisal Vali9366a482013-11-07 16:57:56 +0000192 assert(Idx < getNumPotentialVariableCaptures() &&
Faisal Valia17d19f2013-11-07 05:17:06 +0000193 "Index of potential capture must be within 0 to less than the "
194 "number of captures!");
195 E = PotentiallyCapturingExprs[Idx];
196 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
197 VD = dyn_cast<VarDecl>(DRE->getFoundDecl());
198 else if (MemberExpr *ME = dyn_cast<MemberExpr>(E))
199 VD = dyn_cast<VarDecl>(ME->getMemberDecl());
200 else
201 llvm_unreachable("Only DeclRefExprs or MemberExprs should be added for "
202 "potential captures");
203 assert(VD);
204}
205
Jordan Rose62b37982012-09-28 22:21:39 +0000206FunctionScopeInfo::~FunctionScopeInfo() { }
207BlockScopeInfo::~BlockScopeInfo() { }
208LambdaScopeInfo::~LambdaScopeInfo() { }
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000209CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }