ScopeInfo.h revision 686775deca8b8685eb90801495880e3abdd844c2
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//
10781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//  This file defines FunctionScopeInfo and BlockScopeInfo.
11781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//
12781472fe99a120098c631b0cbe33c89f8cef5e70John McCall//===----------------------------------------------------------------------===//
13781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
14781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#ifndef LLVM_CLANG_SEMA_SCOPE_INFO_H
15781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#define LLVM_CLANG_SEMA_SCOPE_INFO_H
16781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
17781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "clang/AST/Type.h"
18351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek#include "clang/Basic/PartialDiagnostic.h"
19781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "llvm/ADT/DenseMap.h"
20781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "llvm/ADT/SmallVector.h"
21469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall#include "llvm/ADT/SetVector.h"
22781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
23781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace clang {
24781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
25781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass BlockDecl;
26781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass IdentifierInfo;
27ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerclass LabelDecl;
28781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass ReturnStmt;
29781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass Scope;
30781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass SwitchStmt;
31781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
32781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace sema {
33781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
34351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenekclass PossiblyUnreachableDiag {
35351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenekpublic:
36351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  PartialDiagnostic PD;
37351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  SourceLocation Loc;
38351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  const Stmt *stmt;
39351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek
40351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  PossiblyUnreachableDiag(const PartialDiagnostic &PD, SourceLocation Loc,
41351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek                          const Stmt *stmt)
42351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek    : PD(PD), Loc(Loc), stmt(stmt) {}
43351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek};
44351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek
45781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// \brief Retains information about a function, method, or block that is
46781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// currently being parsed.
47781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass FunctionScopeInfo {
48781472fe99a120098c631b0cbe33c89f8cef5e70John McCallpublic:
49781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
50781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this scope information structure defined information for
51781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// a block.
52781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool IsBlockInfo;
53781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
54781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains a VLA, @try, try, C++
55781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// initializer, or anything else that can't be jumped past.
56781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchProtectedScope;
57781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
58781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any switches or direct gotos.
59781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchIntoScope;
60781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
61781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any indirect gotos.
62781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasIndirectGoto;
63781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
648fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  /// \brief Used to determine if errors occurred in this function or block.
658fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  DiagnosticErrorTrap ErrorTrap;
66781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
67781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// SwitchStack - This is the current set of active switch statements in the
68781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// block.
69686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<SwitchStmt*, 8> SwitchStack;
70781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
71781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief The list of return statements that occur within the function or
72781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// block, if there is any chance of applying the named return value
73781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// optimization.
74686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<ReturnStmt*, 4> Returns;
75351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek
76351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  /// \brief A list of PartialDiagnostics created but delayed within the
77351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  /// current function scope.  These diagnostics are vetted for reachability
78351ba91eaa6d30e523587b2d7ed676a5172c6e56Ted Kremenek  /// prior to being emitted.
79686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<PossiblyUnreachableDiag, 4> PossiblyUnreachableDiags;
80781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
81781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasBranchIntoScope() {
82781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasBranchIntoScope = true;
83781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
84781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
85781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasBranchProtectedScope() {
86781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasBranchProtectedScope = true;
87781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
88781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
89781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasIndirectGoto() {
90781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasIndirectGoto = true;
91781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
92781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
93781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool NeedsScopeChecking() const {
94781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    return HasIndirectGoto ||
95781472fe99a120098c631b0cbe33c89f8cef5e70John McCall          (HasBranchProtectedScope && HasBranchIntoScope);
96781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
97781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
988fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopeInfo(Diagnostic &Diag)
99781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    : IsBlockInfo(false),
100781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchProtectedScope(false),
101781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchIntoScope(false),
102781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasIndirectGoto(false),
1038fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis      ErrorTrap(Diag) { }
104781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
105781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  virtual ~FunctionScopeInfo();
106781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
107781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Clear out the information in this function scope, making it
108781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// suitable for reuse.
1098fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  void Clear();
110781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
111781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const FunctionScopeInfo *FSI) { return true; }
112781472fe99a120098c631b0cbe33c89f8cef5e70John McCall};
113781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
114781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// \brief Retains information about a block that is currently being parsed.
115781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass BlockScopeInfo : public FunctionScopeInfo {
116781472fe99a120098c631b0cbe33c89f8cef5e70John McCallpublic:
117781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  BlockDecl *TheDecl;
118781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
119781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// TheScope - This is the scope for the block itself, which contains
120781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// arguments etc.
121781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  Scope *TheScope;
122781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
123781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// ReturnType - The return type of the block, or null if the block
124781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// signature didn't provide an explicit return type.
125781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  QualType ReturnType;
126781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
127781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// BlockType - The function type of the block, if one was given.
128781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// Its return type may be BuiltinType::Dependent.
129781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  QualType FunctionType;
130781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1316b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  /// CaptureMap - A map of captured variables to (index+1) into Captures.
1326b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::DenseMap<VarDecl*, unsigned> CaptureMap;
1336b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
1346b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  /// Captures - The captured variables.
135686775deca8b8685eb90801495880e3abdd844c2Chris Lattner  SmallVector<BlockDecl::Capture, 4> Captures;
136469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
137469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  /// CapturesCXXThis - Whether this block captures 'this'.
138469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  bool CapturesCXXThis;
139469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
1408fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  BlockScopeInfo(Diagnostic &Diag, Scope *BlockScope, BlockDecl *Block)
141469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    : FunctionScopeInfo(Diag), TheDecl(Block), TheScope(BlockScope),
142469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall      CapturesCXXThis(false)
143781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  {
144781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    IsBlockInfo = true;
145781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
146781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
147781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  virtual ~BlockScopeInfo();
148781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
149781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const FunctionScopeInfo *FSI) { return FSI->IsBlockInfo; }
150781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const BlockScopeInfo *BSI) { return true; }
151781472fe99a120098c631b0cbe33c89f8cef5e70John McCall};
152781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
153781472fe99a120098c631b0cbe33c89f8cef5e70John McCall}
154781472fe99a120098c631b0cbe33c89f8cef5e70John McCall}
155781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
156781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#endif
157