1781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//===--- ScopeInfo.h - Information about a semantic context -----*- C++ -*-===//
2781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//
3781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//                     The LLVM Compiler Infrastructure
4781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//
5781472fe99a120098c631b0cbe33c89f8cef5e70John McCall// This file is distributed under the University of Illinois Open Source
6781472fe99a120098c631b0cbe33c89f8cef5e70John McCall// License. See LICENSE.TXT for details.
7781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//
8781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//===----------------------------------------------------------------------===//
9781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//
10a55d32d1b8f799bf58c02540983976368c42d895Jordan Rose// This file defines FunctionScopeInfo and its subclasses, which contain
11a55d32d1b8f799bf58c02540983976368c42d895Jordan Rose// information about a single function, block, lambda, or method body.
12781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//
13781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//===----------------------------------------------------------------------===//
14781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
15781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#ifndef LLVM_CLANG_SEMA_SCOPE_INFO_H
16781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#define LLVM_CLANG_SEMA_SCOPE_INFO_H
17781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
18781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/AST/Type.h"
193a2f91280a49f4747063f983dc6a3296bd9359d2Ben Langmuir#include "clang/Basic/CapturedStmt.h"
20351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek#include "clang/Basic/PartialDiagnostic.h"
21c00e4194296e994efab0e4bf64ca66737850bdf0Faisal Vali#include "clang/Sema/Ownership.h"
22781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "llvm/ADT/DenseMap.h"
23c00e4194296e994efab0e4bf64ca66737850bdf0Faisal Vali#include "llvm/ADT/SmallSet.h"
24781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "llvm/ADT/SmallVector.h"
25c00e4194296e994efab0e4bf64ca66737850bdf0Faisal Vali#include <algorithm>
26781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
27781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace clang {
28781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
2958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Roseclass Decl;
30781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass BlockDecl;
316afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Sirajclass CapturedDecl;
32f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorclass CXXMethodDecl;
330d8e9646bc000bab521ce52ed294209a92298cefRichard Smithclass FieldDecl;
347fffce781e6ecbf4058b24df7e5ae3037569aa56Jordan Roseclass ObjCPropertyDecl;
35781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass IdentifierInfo;
368c045ace381972f41d385b0a661ccf172834f459Ben Langmuirclass ImplicitParamDecl;
37ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerclass LabelDecl;
38781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass ReturnStmt;
39781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass Scope;
40781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass SwitchStmt;
41fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Valiclass TemplateTypeParmDecl;
42fad9e13f3cb85198f0ee5af620ba81cd78574faaFaisal Valiclass TemplateParameterList;
43f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregorclass VarDecl;
447a2704800943fbb69207e125d28186278712af36Jordan Roseclass DeclRefExpr;
45c00e4194296e994efab0e4bf64ca66737850bdf0Faisal Valiclass MemberExpr;
467a2704800943fbb69207e125d28186278712af36Jordan Roseclass ObjCIvarRefExpr;
4758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Roseclass ObjCPropertyRefExpr;
487fffce781e6ecbf4058b24df7e5ae3037569aa56Jordan Roseclass ObjCMessageExpr;
49781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
50781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace sema {
51781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
52625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko/// \brief Contains information about the compound statement currently being
53625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko/// parsed.
54625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkoclass CompoundScopeInfo {
55625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenkopublic:
56625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  CompoundScopeInfo()
57625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko    : HasEmptyLoopBodies(false) { }
58625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
59625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  /// \brief Whether this compound stamement contains `for' or `while' loops
60625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  /// with empty bodies.
61625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  bool HasEmptyLoopBodies;
62625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
63625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  void setHasEmptyLoopBodies() {
64625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko    HasEmptyLoopBodies = true;
65625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  }
66625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko};
67625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
68351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenekclass PossiblyUnreachableDiag {
69351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenekpublic:
70351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  PartialDiagnostic PD;
71351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  SourceLocation Loc;
72351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  const Stmt *stmt;
73351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek
74351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  PossiblyUnreachableDiag(const PartialDiagnostic &PD, SourceLocation Loc,
75351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek                          const Stmt *stmt)
76351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek    : PD(PD), Loc(Loc), stmt(stmt) {}
77351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek};
78351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek
79781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// \brief Retains information about a function, method, or block that is
80781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// currently being parsed.
81781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass FunctionScopeInfo {
82ec9ea7200718478e8a976529defbe21942a11c9cEli Friedmanprotected:
83ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  enum ScopeKind {
84ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman    SK_Function,
85ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman    SK_Block,
866afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    SK_Lambda,
876afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    SK_CapturedRegion
88ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  };
89ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman
90781472fe99a120098c631b0cbe33c89f8cef5e70John McCallpublic:
91ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  /// \brief What kind of scope we are describing.
92ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  ///
93ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman  ScopeKind Kind;
94781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
95809d1be9820039b4cf6efa48246a0d70ffa13394James Dennett  /// \brief Whether this function contains a VLA, \@try, try, C++
96781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// initializer, or anything else that can't be jumped past.
97781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchProtectedScope;
98781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
99781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any switches or direct gotos.
100781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchIntoScope;
101781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
102781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any indirect gotos.
103781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasIndirectGoto;
104781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
105820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis  /// \brief Whether a statement was dropped because it was invalid.
106820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis  bool HasDroppedStmt;
107820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis
108535a5d001b2035a7aa68ee368fbe44ec90e33740Jordan Rose  /// A flag that is set when parsing a method that must call super's
109535a5d001b2035a7aa68ee368fbe44ec90e33740Jordan Rose  /// implementation, such as \c -dealloc, \c -finalize, or any method marked
110535a5d001b2035a7aa68ee368fbe44ec90e33740Jordan Rose  /// with \c __attribute__((objc_requires_super)).
111535a5d001b2035a7aa68ee368fbe44ec90e33740Jordan Rose  bool ObjCShouldCallSuper;
11295aac15936e8362aeb4813f95bc255dee6473592Eli Friedman
113651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// True when this is a method marked as a designated initializer.
114651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool ObjCIsDesignatedInit;
115651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// This starts true for a method marked as designated initializer and will
116651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// be set to false if there is an invocation to a designated initializer of
117651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// the super class.
118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool ObjCWarnForNoDesignatedInitChain;
119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
120651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// True when this is an initializer method not marked as a designated
121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// initializer within a class that has at least one initializer marked as a
122651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// designated initializer.
123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool ObjCIsSecondaryInit;
124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// This starts true for a secondary initializer method and will be set to
125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// false if there is an invocation of an initializer on 'self'.
126651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool ObjCWarnForNoInitDelegation;
127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1288fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  /// \brief Used to determine if errors occurred in this function or block.
1298fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  DiagnosticErrorTrap ErrorTrap;
130781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
131781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// SwitchStack - This is the current set of active switch statements in the
132781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// block.
133686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<SwitchStmt*, 8> SwitchStack;
134781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
135781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief The list of return statements that occur within the function or
136781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// block, if there is any chance of applying the named return value
1377dd900ed308506f9cf1cb72c70db1652f94cab37Jordan Rose  /// optimization, or if we need to infer a return type.
138686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<ReturnStmt*, 4> Returns;
139625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
140625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  /// \brief The stack of currently active compound stamement scopes in the
141625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  /// function.
142625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  SmallVector<CompoundScopeInfo, 4> CompoundScopes;
143625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko
144351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  /// \brief A list of PartialDiagnostics created but delayed within the
145351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  /// current function scope.  These diagnostics are vetted for reachability
146351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  /// prior to being emitted.
147686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<PossiblyUnreachableDiag, 4> PossiblyUnreachableDiags;
148781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
14958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rosepublic:
15058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Represents a simple identification of a weak object.
15158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
15258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Part of the implementation of -Wrepeated-use-of-weak.
15358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
15458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// This is used to determine if two weak accesses refer to the same object.
15558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Here are some examples of how various accesses are "profiled":
15658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
15758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Access Expression |     "Base" Decl     |          "Property" Decl
15858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// :---------------: | :-----------------: | :------------------------------:
15958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// self.property     | self (VarDecl)      | property (ObjCPropertyDecl)
16058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// self.implicitProp | self (VarDecl)      | -implicitProp (ObjCMethodDecl)
16158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// self->ivar.prop   | ivar (ObjCIvarDecl) | prop (ObjCPropertyDecl)
16258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// cxxObj.obj.prop   | obj (FieldDecl)     | prop (ObjCPropertyDecl)
16358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// [self foo].prop   | 0 (unknown)         | prop (ObjCPropertyDecl)
16458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// self.prop1.prop2  | prop1 (ObjCPropertyDecl)    | prop2 (ObjCPropertyDecl)
16558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// MyClass.prop      | MyClass (ObjCInterfaceDecl) | -prop (ObjCMethodDecl)
1667a2704800943fbb69207e125d28186278712af36Jordan Rose  /// weakVar           | 0 (known)           | weakVar (VarDecl)
1677a2704800943fbb69207e125d28186278712af36Jordan Rose  /// self->weakIvar    | self (VarDecl)      | weakIvar (ObjCIvarDecl)
16858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
16958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Objects are identified with only two Decls to make it reasonably fast to
17058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// compare them.
17158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  class WeakObjectProfileTy {
17258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// The base object decl, as described in the class documentation.
17358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    ///
17458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// The extra flag is "true" if the Base and Property are enough to uniquely
17558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// identify the object in memory.
17658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    ///
17758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// \sa isExactProfile()
17858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    typedef llvm::PointerIntPair<const NamedDecl *, 1, bool> BaseInfoTy;
17958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    BaseInfoTy Base;
18058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
18158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// The "property" decl, as described in the class documentation.
18258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    ///
18358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// Note that this may not actually be an ObjCPropertyDecl, e.g. in the
18458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// case of "implicit" properties (regular methods accessed via dot syntax).
18558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    const NamedDecl *Property;
18658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
1877a2704800943fbb69207e125d28186278712af36Jordan Rose    /// Used to find the proper base profile for a given base expression.
1887a2704800943fbb69207e125d28186278712af36Jordan Rose    static BaseInfoTy getBaseInfo(const Expr *BaseE);
1897a2704800943fbb69207e125d28186278712af36Jordan Rose
19058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    // For use in DenseMap.
191b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    friend class DenseMapInfo;
19258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    inline WeakObjectProfileTy();
19358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    static inline WeakObjectProfileTy getSentinel();
19458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
19558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  public:
19658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    WeakObjectProfileTy(const ObjCPropertyRefExpr *RE);
1977fffce781e6ecbf4058b24df7e5ae3037569aa56Jordan Rose    WeakObjectProfileTy(const Expr *Base, const ObjCPropertyDecl *Property);
1987a2704800943fbb69207e125d28186278712af36Jordan Rose    WeakObjectProfileTy(const DeclRefExpr *RE);
1997a2704800943fbb69207e125d28186278712af36Jordan Rose    WeakObjectProfileTy(const ObjCIvarRefExpr *RE);
20058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
201c0e44454bd78b8b4f3d70f08cf1edd5466b0c798Jordan Rose    const NamedDecl *getBase() const { return Base.getPointer(); }
20258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    const NamedDecl *getProperty() const { return Property; }
20358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
20458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// Returns true if the object base specifies a known object in memory,
20558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// rather than, say, an instance variable or property of another object.
20658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    ///
20758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// Note that this ignores the effects of aliasing; that is, \c foo.bar is
20858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// considered an exact profile if \c foo is a local variable, even if
20958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// another variable \c foo2 refers to the same object as \c foo.
21058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    ///
21158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// For increased precision, accesses with base variables that are
21258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// properties or ivars of 'self' (e.g. self.prop1.prop2) are considered to
21358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// be exact, though this is not true for arbitrary variables
21458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    /// (foo.prop1.prop2).
21558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    bool isExactProfile() const {
21658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose      return Base.getInt();
21758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    }
21858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
21958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    bool operator==(const WeakObjectProfileTy &Other) const {
22058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose      return Base == Other.Base && Property == Other.Property;
22158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    }
222b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose
223b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    // For use in DenseMap.
224b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    // We can't specialize the usual llvm::DenseMapInfo at the end of the file
225b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    // because by that point the DenseMap in FunctionScopeInfo has already been
226b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    // instantiated.
227b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    class DenseMapInfo {
228b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    public:
229b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      static inline WeakObjectProfileTy getEmptyKey() {
230b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose        return WeakObjectProfileTy();
231b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      }
232b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      static inline WeakObjectProfileTy getTombstoneKey() {
233b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose        return WeakObjectProfileTy::getSentinel();
234b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      }
235b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose
236b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      static unsigned getHashValue(const WeakObjectProfileTy &Val) {
237b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose        typedef std::pair<BaseInfoTy, const NamedDecl *> Pair;
238b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose        return llvm::DenseMapInfo<Pair>::getHashValue(Pair(Val.Base,
239b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose                                                           Val.Property));
240b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      }
241b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose
242b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      static bool isEqual(const WeakObjectProfileTy &LHS,
243b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose                          const WeakObjectProfileTy &RHS) {
244b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose        return LHS == RHS;
245b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose      }
246b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose    };
24758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  };
24858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
24958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Represents a single use of a weak object.
25058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
25158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Stores both the expression and whether the access is potentially unsafe
25258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// (i.e. it could potentially be warned about).
25358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
25458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Part of the implementation of -Wrepeated-use-of-weak.
25558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  class WeakUseTy {
25658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    llvm::PointerIntPair<const Expr *, 1, bool> Rep;
25758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  public:
25858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    WeakUseTy(const Expr *Use, bool IsRead) : Rep(Use, IsRead) {}
25958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
26058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    const Expr *getUseExpr() const { return Rep.getPointer(); }
26158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    bool isUnsafe() const { return Rep.getInt(); }
26258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    void markSafe() { Rep.setInt(false); }
26358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
26458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    bool operator==(const WeakUseTy &Other) const {
26558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose      return Rep == Other.Rep;
26658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    }
26758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  };
26858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
26958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Used to collect uses of a particular weak object in a function body.
27058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
27158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Part of the implementation of -Wrepeated-use-of-weak.
27258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  typedef SmallVector<WeakUseTy, 4> WeakUseVector;
27358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
27458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Used to collect all uses of weak objects in a function body.
27558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
27658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Part of the implementation of -Wrepeated-use-of-weak.
277b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose  typedef llvm::SmallDenseMap<WeakObjectProfileTy, WeakUseVector, 8,
278b6d80fe68d30ebd14b20c96643c252c36041df8dJordan Rose                              WeakObjectProfileTy::DenseMapInfo>
27958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose          WeakObjectUseMap;
28058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
28158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Roseprivate:
28258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Used to collect all uses of weak objects in this function body.
28358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
28458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Part of the implementation of -Wrepeated-use-of-weak.
28558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  WeakObjectUseMap WeakObjectUses;
28658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
28758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rosepublic:
2887a2704800943fbb69207e125d28186278712af36Jordan Rose  /// Record that a weak object was accessed.
28958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
29058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Part of the implementation of -Wrepeated-use-of-weak.
2917a2704800943fbb69207e125d28186278712af36Jordan Rose  template <typename ExprT>
2927a2704800943fbb69207e125d28186278712af36Jordan Rose  inline void recordUseOfWeak(const ExprT *E, bool IsRead = true);
29358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
2947fffce781e6ecbf4058b24df7e5ae3037569aa56Jordan Rose  void recordUseOfWeak(const ObjCMessageExpr *Msg,
2957fffce781e6ecbf4058b24df7e5ae3037569aa56Jordan Rose                       const ObjCPropertyDecl *Prop);
2967fffce781e6ecbf4058b24df7e5ae3037569aa56Jordan Rose
29758b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Record that a given expression is a "safe" access of a weak object (e.g.
29858b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// assigning it to a strong variable.)
29958b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  ///
30058b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  /// Part of the implementation of -Wrepeated-use-of-weak.
30158b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  void markSafeWeakUse(const Expr *E);
30258b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
30358b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  const WeakObjectUseMap &getWeakObjectUses() const {
30458b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose    return WeakObjectUses;
30558b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose  }
30658b6bdcdeb683a3504f2248a409e1f4e85876ceeJordan Rose
307781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasBranchIntoScope() {
308781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasBranchIntoScope = true;
309781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
310781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
311781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasBranchProtectedScope() {
312781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasBranchProtectedScope = true;
313781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
314781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
315781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasIndirectGoto() {
316781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasIndirectGoto = true;
317781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
318781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
319820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis  void setHasDroppedStmt() {
320820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis    HasDroppedStmt = true;
321820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis  }
322820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis
323781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool NeedsScopeChecking() const {
324820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis    return !HasDroppedStmt &&
325820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis        (HasIndirectGoto ||
326820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis          (HasBranchProtectedScope && HasBranchIntoScope));
327781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
328781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
329d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie  FunctionScopeInfo(DiagnosticsEngine &Diag)
330ec9ea7200718478e8a976529defbe21942a11c9cEli Friedman    : Kind(SK_Function),
331781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchProtectedScope(false),
332781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchIntoScope(false),
333781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasIndirectGoto(false),
334820b23dc924a4ae7af07d5a75d6b1d781c267d57Argyrios Kyrtzidis      HasDroppedStmt(false),
335535a5d001b2035a7aa68ee368fbe44ec90e33740Jordan Rose      ObjCShouldCallSuper(false),
336651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ObjCIsDesignatedInit(false),
337651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ObjCWarnForNoDesignatedInitChain(false),
338651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ObjCIsSecondaryInit(false),
339651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ObjCWarnForNoInitDelegation(false),
3408fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis      ErrorTrap(Diag) { }
341781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
342781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  virtual ~FunctionScopeInfo();
343781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
344781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Clear out the information in this function scope, making it
345781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// suitable for reuse.
3468fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  void Clear();
347781472fe99a120098c631b0cbe33c89f8cef5e70John McCall};
348781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
349b69b42c55d56815bab62991bf839cdb41634d3afEli Friedmanclass CapturingScopeInfo : public FunctionScopeInfo {
350b69b42c55d56815bab62991bf839cdb41634d3afEli Friedmanpublic:
351b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman  enum ImplicitCaptureStyle {
3526afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    ImpCap_None, ImpCap_LambdaByval, ImpCap_LambdaByref, ImpCap_Block,
3536afcf8875d4e447645cd7bf3733dd8e2eb8455dcTareq A. Siraj    ImpCap_CapturedRegion
354b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman  };
355b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman
356b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman  ImplicitCaptureStyle ImpCaptureStyle;
357b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman
358b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman  class Capture {
3590d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // There are three categories of capture: capturing 'this', capturing
3600d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // local variables, and C++1y initialized captures (which can have an
3610d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // arbitrary initializer, and don't really capture in the traditional
3620d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // sense at all).
3630d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    //
3640d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // There are three ways to capture a local variable:
3650d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    //  - capture by copy in the C++11 sense,
3660d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    //  - capture by reference in the C++11 sense, and
3670d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    //  - __block capture.
3680d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // Lambdas explicitly specify capture by copy or capture by reference.
3690d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // For blocks, __block capture applies to variables with that annotation,
3700d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // variables of reference type are captured by reference, and other
3710d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    // variables are captured by copy.
372b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman    enum CaptureKind {
37304fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith      Cap_ByCopy, Cap_ByRef, Cap_Block, Cap_This
374b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman    };
375b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman
37604fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith    /// The variable being captured (if we are not capturing 'this') and whether
37704fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith    /// this is a nested capture.
37804fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith    llvm::PointerIntPair<VarDecl*, 1, bool> VarAndNested;
379b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman
38004fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith    /// Expression to initialize a field of the given type, and the kind of
38104fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith    /// capture (if this is a capture and not an init-capture). The expression
38204fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith    /// is only required if we are capturing ByVal and the variable's type has
38304fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith    /// a non-trivial copy constructor.
3840d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    llvm::PointerIntPair<Expr*, 2, CaptureKind> InitExprAndCaptureKind;
3850d8e9646bc000bab521ce52ed294209a92298cefRichard Smith
3860d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    /// \brief The source location at which the first capture occurred.
38793962e5360a43200faa70939571afc4fb9326cf7Douglas Gregor    SourceLocation Loc;
3880d8e9646bc000bab521ce52ed294209a92298cefRichard Smith
389a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    /// \brief The location of the ellipsis that expands a parameter pack.
390a73652465bcc4c0f6cb7d933ad84e002b527a643Douglas Gregor    SourceLocation EllipsisLoc;
3910d8e9646bc000bab521ce52ed294209a92298cefRichard Smith
392999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor    /// \brief The type as it was captured, which is in effect the type of the
393999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor    /// non-static data member that would hold the capture.
394999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor    QualType CaptureType;
3950d8e9646bc000bab521ce52ed294209a92298cefRichard Smith
396b69b42c55d56815bab62991bf839cdb41634d3afEli Friedman  public:
3970d8e9646bc000bab521ce52ed294209a92298cefRichard Smith    Capture(VarDecl *Var, bool Block, bool ByRef, bool IsNested,
3980d8e9646bc000bab521ce52ed294209a92298cefRichard Smith            SourceLocation Loc, SourceLocation EllipsisLoc,
399999713eea940f4e087cc3ac878689c5c5c7a7225Douglas Gregor            QualType CaptureType, Expr *Cpy)
40004fa7a33279808dc3e5117c41b5f84c40eeb7362Richard Smith        : VarAndNested(Var, IsNested),
4010d8e9646bc000bab521ce52ed294209a92298cefRichard Smith          InitExprAndCaptureKind(Cpy, Block ? Cap_Block :
402