blob: bb9420d1e7e71640d542b6f8e0c2091ae4ffc137 [file] [log] [blame]
Jordan Rosea55d32d2012-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;
29
30 SwitchStack.clear();
31 Returns.clear();
32 ErrorTrap.reset();
33 PossiblyUnreachableDiags.clear();
34 WeakObjectUses.clear();
35}
36
37static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
38 if (PropE->isExplicitProperty())
39 return PropE->getExplicitProperty();
40
41 return PropE->getImplicitPropertyGetter();
42}
43
44static bool isSelfExpr(const Expr *E) {
45 E = E->IgnoreParenImpCasts();
46
47 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
48 if (!DRE)
49 return false;
50
51 const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
52 if (!Param)
53 return false;
54
55 const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
56 if (!M)
57 return false;
58
59 return M->getSelfDecl() == Param;
60}
61
62FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
63FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
64 E = E->IgnoreParenCasts();
65
66 const NamedDecl *D = 0;
67 bool IsExact = false;
68
69 switch (E->getStmtClass()) {
70 case Stmt::DeclRefExprClass:
71 D = cast<DeclRefExpr>(E)->getDecl();
72 IsExact = isa<VarDecl>(D);
73 break;
74 case Stmt::MemberExprClass: {
75 const MemberExpr *ME = cast<MemberExpr>(E);
76 D = ME->getMemberDecl();
77 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
78 break;
79 }
80 case Stmt::ObjCIvarRefExprClass: {
81 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
82 D = IE->getDecl();
83 IsExact = isSelfExpr(IE->getBase());
84 break;
85 }
86 case Stmt::PseudoObjectExprClass: {
87 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
88 const ObjCPropertyRefExpr *BaseProp =
89 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
90 if (BaseProp) {
91 D = getBestPropertyDecl(BaseProp);
92
93 const Expr *DoubleBase = BaseProp->getBase();
94 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
95 DoubleBase = OVE->getSourceExpr();
96
97 IsExact = isSelfExpr(DoubleBase);
98 }
99 break;
100 }
101 default:
102 break;
103 }
104
105 return BaseInfoTy(D, IsExact);
106}
107
108
109FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
110 const ObjCPropertyRefExpr *PropE)
111 : Base(0, true), Property(getBestPropertyDecl(PropE)) {
112
113 if (PropE->isObjectReceiver()) {
114 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
115 const Expr *E = OVE->getSourceExpr();
116 Base = getBaseInfo(E);
117 } else if (PropE->isClassReceiver()) {
118 Base.setPointer(PropE->getClassReceiver());
119 } else {
120 assert(PropE->isSuperReceiver());
121 }
122}
123
124FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
125 const DeclRefExpr *DRE)
126 : Base(0, true), Property(DRE->getDecl()) {
127 assert(isa<VarDecl>(Property));
128}
129
130FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
131 const ObjCIvarRefExpr *IvarE)
132 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
133}
134
135void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
136 E = E->IgnoreParenImpCasts();
137
138 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
139 markSafeWeakUse(POE->getSyntacticForm());
140 return;
141 }
142
143 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
144 markSafeWeakUse(Cond->getTrueExpr());
145 markSafeWeakUse(Cond->getFalseExpr());
146 return;
147 }
148
149 if (const BinaryConditionalOperator *Cond =
150 dyn_cast<BinaryConditionalOperator>(E)) {
151 markSafeWeakUse(Cond->getCommon());
152 markSafeWeakUse(Cond->getFalseExpr());
153 return;
154 }
155
156 // Has this weak object been seen before?
157 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
158 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E))
159 Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(RefExpr));
160 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
161 Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(IvarE));
162 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
163 Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(DRE));
164 else
165 return;
166
167 if (Uses == WeakObjectUses.end())
168 return;
169
170 // Has there been a read from the object using this Expr?
171 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
172 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
173 if (ThisUse == Uses->second.rend())
174 return;
175
176 ThisUse->markSafe();
177}
178
179FunctionScopeInfo::~FunctionScopeInfo() { }
180BlockScopeInfo::~BlockScopeInfo() { }
181LambdaScopeInfo::~LambdaScopeInfo() { }