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/DeclObjC.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/ExprObjC.h"
22 using namespace clang;
25 void FunctionScopeInfo::Clear() {
26 HasBranchProtectedScope = false;
27 HasBranchIntoScope = false;
28 HasIndirectGoto = false;
33 PossiblyUnreachableDiags.clear();
34 WeakObjectUses.clear();
37 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
38 if (PropE->isExplicitProperty())
39 return PropE->getExplicitProperty();
41 return PropE->getImplicitPropertyGetter();
44 FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
45 FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
46 E = E->IgnoreParenCasts();
48 const NamedDecl *D = 0;
51 switch (E->getStmtClass()) {
52 case Stmt::DeclRefExprClass:
53 D = cast<DeclRefExpr>(E)->getDecl();
54 IsExact = isa<VarDecl>(D);
56 case Stmt::MemberExprClass: {
57 const MemberExpr *ME = cast<MemberExpr>(E);
58 D = ME->getMemberDecl();
59 IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
62 case Stmt::ObjCIvarRefExprClass: {
63 const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
65 IsExact = IE->getBase()->isObjCSelfExpr();
68 case Stmt::PseudoObjectExprClass: {
69 const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
70 const ObjCPropertyRefExpr *BaseProp =
71 dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
73 D = getBestPropertyDecl(BaseProp);
75 const Expr *DoubleBase = BaseProp->getBase();
76 if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
77 DoubleBase = OVE->getSourceExpr();
79 IsExact = DoubleBase->isObjCSelfExpr();
87 return BaseInfoTy(D, IsExact);
91 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
92 const ObjCPropertyRefExpr *PropE)
93 : Base(0, true), Property(getBestPropertyDecl(PropE)) {
95 if (PropE->isObjectReceiver()) {
96 const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
97 const Expr *E = OVE->getSourceExpr();
98 Base = getBaseInfo(E);
99 } else if (PropE->isClassReceiver()) {
100 Base.setPointer(PropE->getClassReceiver());
102 assert(PropE->isSuperReceiver());
106 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
107 const ObjCPropertyDecl *Prop)
108 : Base(0, true), Property(Prop) {
110 Base = getBaseInfo(BaseE);
111 // else, this is a message accessing a property on super.
114 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
115 const DeclRefExpr *DRE)
116 : Base(0, true), Property(DRE->getDecl()) {
117 assert(isa<VarDecl>(Property));
120 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
121 const ObjCIvarRefExpr *IvarE)
122 : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
125 void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
126 const ObjCPropertyDecl *Prop) {
128 WeakUseVector &Uses =
129 WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
130 Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
133 void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
134 E = E->IgnoreParenCasts();
136 if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
137 markSafeWeakUse(POE->getSyntacticForm());
141 if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
142 markSafeWeakUse(Cond->getTrueExpr());
143 markSafeWeakUse(Cond->getFalseExpr());
147 if (const BinaryConditionalOperator *Cond =
148 dyn_cast<BinaryConditionalOperator>(E)) {
149 markSafeWeakUse(Cond->getCommon());
150 markSafeWeakUse(Cond->getFalseExpr());
154 // Has this weak object been seen before?
155 FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
156 if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E))
157 Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
158 else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
159 Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
160 else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
161 Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
162 else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
163 Uses = WeakObjectUses.end();
164 if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
165 if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
167 WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
175 if (Uses == WeakObjectUses.end())
178 // Has there been a read from the object using this Expr?
179 FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
180 std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
181 if (ThisUse == Uses->second.rend())
187 FunctionScopeInfo::~FunctionScopeInfo() { }
188 BlockScopeInfo::~BlockScopeInfo() { }
189 LambdaScopeInfo::~LambdaScopeInfo() { }
190 CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }