1//===--- ScopeInfo.cpp - Information about a semantic context -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements FunctionScopeInfo and its subclasses, which contain
10// information about a single function, block, lambda, or method body.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Sema/ScopeInfo.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclCXX.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  HasDroppedStmt = false;
30  HasOMPDeclareReductionCombiner = false;
31  HasFallthroughStmt = false;
32  HasPotentialAvailabilityViolations = false;
33  ObjCShouldCallSuper = false;
34  ObjCIsDesignatedInit = false;
35  ObjCWarnForNoDesignatedInitChain = false;
36  ObjCIsSecondaryInit = false;
37  ObjCWarnForNoInitDelegation = false;
38  FirstReturnLoc = SourceLocation();
39  FirstCXXTryLoc = SourceLocation();
40  FirstSEHTryLoc = SourceLocation();
41
42  // Coroutine state
43  FirstCoroutineStmtLoc = SourceLocation();
44  CoroutinePromise = nullptr;
45  CoroutineParameterMoves.clear();
46  NeedsCoroutineSuspends = true;
47  CoroutineSuspends.first = nullptr;
48  CoroutineSuspends.second = nullptr;
49
50  SwitchStack.clear();
51  Returns.clear();
52  ErrorTrap.reset();
53  PossiblyUnreachableDiags.clear();
54  WeakObjectUses.clear();
55  ModifiedNonNullParams.clear();
56  Blocks.clear();
57  ByrefBlockVars.clear();
58}
59
60static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
61  if (PropE->isExplicitProperty())
62    return PropE->getExplicitProperty();
63
64  return PropE->getImplicitPropertyGetter();
65}
66
67FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
68FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
69  E = E->IgnoreParenCasts();
70
71  const NamedDecl *D = nullptr;
72  bool IsExact = false;
73
74  switch (E->getStmtClass()) {
75  case Stmt::DeclRefExprClass:
76    D = cast<DeclRefExpr>(E)->getDecl();
77    IsExact = isa<VarDecl>(D);
78    break;
79  case Stmt::MemberExprClass: {
80    const MemberExpr *ME = cast<MemberExpr>(E);
81    D = ME->getMemberDecl();
82    IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
83    break;
84  }
85  case Stmt::ObjCIvarRefExprClass: {
86    const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
87    D = IE->getDecl();
88    IsExact = IE->getBase()->isObjCSelfExpr();
89    break;
90  }
91  case Stmt::PseudoObjectExprClass: {
92    const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
93    const ObjCPropertyRefExpr *BaseProp =
94      dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
95    if (BaseProp) {
96      D = getBestPropertyDecl(BaseProp);
97
98      if (BaseProp->isObjectReceiver()) {
99        const Expr *DoubleBase = BaseProp->getBase();
100        if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
101          DoubleBase = OVE->getSourceExpr();
102
103        IsExact = DoubleBase->isObjCSelfExpr();
104      }
105    }
106    break;
107  }
108  default:
109    break;
110  }
111
112  return BaseInfoTy(D, IsExact);
113}
114
115FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
116                                          const ObjCPropertyRefExpr *PropE)
117    : Base(nullptr, true), Property(getBestPropertyDecl(PropE)) {
118
119  if (PropE->isObjectReceiver()) {
120    const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
121    const Expr *E = OVE->getSourceExpr();
122    Base = getBaseInfo(E);
123  } else if (PropE->isClassReceiver()) {
124    Base.setPointer(PropE->getClassReceiver());
125  } else {
126    assert(PropE->isSuperReceiver());
127  }
128}
129
130FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(const Expr *BaseE,
131                                                const ObjCPropertyDecl *Prop)
132    : Base(nullptr, true), Property(Prop) {
133  if (BaseE)
134    Base = getBaseInfo(BaseE);
135  // else, this is a message accessing a property on super.
136}
137
138FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
139                                                      const DeclRefExpr *DRE)
140  : Base(nullptr, true), Property(DRE->getDecl()) {
141  assert(isa<VarDecl>(Property));
142}
143
144FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
145                                                  const ObjCIvarRefExpr *IvarE)
146  : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
147}
148
149void FunctionScopeInfo::recordUseOfWeak(const ObjCMessageExpr *Msg,
150                                        const ObjCPropertyDecl *Prop) {
151  assert(Msg && Prop);
152  WeakUseVector &Uses =
153    WeakObjectUses[WeakObjectProfileTy(Msg->getInstanceReceiver(), Prop)];
154  Uses.push_back(WeakUseTy(Msg, Msg->getNumArgs() == 0));
155}
156
157void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
158  E = E->IgnoreParenCasts();
159
160  if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
161    markSafeWeakUse(POE->getSyntacticForm());
162    return;
163  }
164
165  if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
166    markSafeWeakUse(Cond->getTrueExpr());
167    markSafeWeakUse(Cond->getFalseExpr());
168    return;
169  }
170
171  if (const BinaryConditionalOperator *Cond =
172        dyn_cast<BinaryConditionalOperator>(E)) {
173    markSafeWeakUse(Cond->getCommon());
174    markSafeWeakUse(Cond->getFalseExpr());
175    return;
176  }
177
178  // Has this weak object been seen before?
179  FunctionScopeInfo::WeakObjectUseMap::iterator Uses = WeakObjectUses.end();
180  if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E)) {
181    if (!RefExpr->isObjectReceiver())
182      return;
183    if (isa<OpaqueValueExpr>(RefExpr->getBase()))
184     Uses = WeakObjectUses.find(WeakObjectProfileTy(RefExpr));
185    else {
186      markSafeWeakUse(RefExpr->getBase());
187      return;
188    }
189  }
190  else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
191    Uses = WeakObjectUses.find(WeakObjectProfileTy(IvarE));
192  else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
193    if (isa<VarDecl>(DRE->getDecl()))
194      Uses = WeakObjectUses.find(WeakObjectProfileTy(DRE));
195  } else if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
196    if (const ObjCMethodDecl *MD = MsgE->getMethodDecl()) {
197      if (const ObjCPropertyDecl *Prop = MD->findPropertyDecl()) {
198        Uses =
199          WeakObjectUses.find(WeakObjectProfileTy(MsgE->getInstanceReceiver(),
200                                                  Prop));
201      }
202    }
203  }
204  else
205    return;
206
207  if (Uses == WeakObjectUses.end())
208    return;
209
210  // Has there been a read from the object using this Expr?
211  FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
212      llvm::find(llvm::reverse(Uses->second), WeakUseTy(E, true));
213  if (ThisUse == Uses->second.rend())
214    return;
215
216  ThisUse->markSafe();
217}
218
219bool Capture::isInitCapture() const {
220  // Note that a nested capture of an init-capture is not itself an
221  // init-capture.
222  return !isNested() && isVariableCapture() && getVariable()->isInitCapture();
223}
224
225bool CapturingScopeInfo::isVLATypeCaptured(const VariableArrayType *VAT) const {
226  for (auto &Cap : Captures)
227    if (Cap.isVLATypeCapture() && Cap.getCapturedVLAType() == VAT)
228      return true;
229  return false;
230}
231
232void LambdaScopeInfo::visitPotentialCaptures(
233    llvm::function_ref<void(VarDecl *, Expr *)> Callback) const {
234  for (Expr *E : PotentiallyCapturingExprs) {
235    if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
236      Callback(cast<VarDecl>(DRE->getFoundDecl()), E);
237    } else if (auto *ME = dyn_cast<MemberExpr>(E)) {
238      Callback(cast<VarDecl>(ME->getMemberDecl()), E);
239    } else if (auto *FP = dyn_cast<FunctionParmPackExpr>(E)) {
240      for (VarDecl *VD : *FP)
241        Callback(VD, E);
242    } else {
243      llvm_unreachable("unexpected expression in potential captures list");
244    }
245  }
246}
247
248FunctionScopeInfo::~FunctionScopeInfo() { }
249BlockScopeInfo::~BlockScopeInfo() { }
250CapturedRegionScopeInfo::~CapturedRegionScopeInfo() { }
251