ScopeInfo.h revision ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298
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"
18781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "llvm/ADT/DenseMap.h"
19781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#include "llvm/ADT/SmallVector.h"
20469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall#include "llvm/ADT/SetVector.h"
21781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
22781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace clang {
23781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
24781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass BlockDecl;
25781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass IdentifierInfo;
26ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattnerclass LabelDecl;
27781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass ReturnStmt;
28781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass Scope;
29781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass SwitchStmt;
30781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
31781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace sema {
32781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
33781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// \brief Retains information about a function, method, or block that is
34781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// currently being parsed.
35781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass FunctionScopeInfo {
36781472fe99a120098c631b0cbe33c89f8cef5e70John McCallpublic:
37781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
38781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this scope information structure defined information for
39781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// a block.
40781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool IsBlockInfo;
41781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
42781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains a VLA, @try, try, C++
43781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// initializer, or anything else that can't be jumped past.
44781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchProtectedScope;
45781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
46781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any switches or direct gotos.
47781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchIntoScope;
48781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
49781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any indirect gotos.
50781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasIndirectGoto;
51781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
528fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  /// \brief Used to determine if errors occurred in this function or block.
538fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  DiagnosticErrorTrap ErrorTrap;
54781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
55ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  /// LabelMap - This is a mapping from label identifiers to the LabelDecl for
56ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  /// it.  Forward referenced labels have a LabelDecl created for them with a
57ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  /// null statement.
58ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::DenseMap<IdentifierInfo*, LabelDecl*> LabelMap;
59781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
60781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// SwitchStack - This is the current set of active switch statements in the
61781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// block.
62781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
63781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
64781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief The list of return statements that occur within the function or
65781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// block, if there is any chance of applying the named return value
66781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// optimization.
67781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  llvm::SmallVector<ReturnStmt *, 4> Returns;
68781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
69781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasBranchIntoScope() {
70781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasBranchIntoScope = true;
71781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
72781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
73781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasBranchProtectedScope() {
74781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasBranchProtectedScope = true;
75781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
76781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
77781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void setHasIndirectGoto() {
78781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    HasIndirectGoto = true;
79781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
80781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
81781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool NeedsScopeChecking() const {
82781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    return HasIndirectGoto ||
83781472fe99a120098c631b0cbe33c89f8cef5e70John McCall          (HasBranchProtectedScope && HasBranchIntoScope);
84781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
85781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
868fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  FunctionScopeInfo(Diagnostic &Diag)
87781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    : IsBlockInfo(false),
88781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchProtectedScope(false),
89781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchIntoScope(false),
90781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasIndirectGoto(false),
918fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis      ErrorTrap(Diag) { }
92781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
93781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  virtual ~FunctionScopeInfo();
94781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
95ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  /// checkLabelUse - This checks to see if any labels are used without being
96ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  /// defined, emiting errors and returning true if any are found.  This also
97ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  /// warns about unused labels.
98ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  bool checkLabelUse(Stmt *Body, Sema &S);
99ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner
100781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Clear out the information in this function scope, making it
101781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// suitable for reuse.
1028fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  void Clear();
103781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
104781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const FunctionScopeInfo *FSI) { return true; }
105781472fe99a120098c631b0cbe33c89f8cef5e70John McCall};
106781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
107781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// \brief Retains information about a block that is currently being parsed.
108781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass BlockScopeInfo : public FunctionScopeInfo {
109781472fe99a120098c631b0cbe33c89f8cef5e70John McCallpublic:
110781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  BlockDecl *TheDecl;
111781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
112781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// TheScope - This is the scope for the block itself, which contains
113781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// arguments etc.
114781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  Scope *TheScope;
115781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
116781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// ReturnType - The return type of the block, or null if the block
117781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// signature didn't provide an explicit return type.
118781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  QualType ReturnType;
119781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
120781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// BlockType - The function type of the block, if one was given.
121781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// Its return type may be BuiltinType::Dependent.
122781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  QualType FunctionType;
123781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
1246b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  /// CaptureMap - A map of captured variables to (index+1) into Captures.
1256b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::DenseMap<VarDecl*, unsigned> CaptureMap;
1266b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
1276b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  /// Captures - The captured variables.
1286b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::SmallVector<BlockDecl::Capture, 4> Captures;
129469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
130469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  /// CapturesCXXThis - Whether this block captures 'this'.
131469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall  bool CapturesCXXThis;
132469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall
1338fc32d272bd57b0a59f61c874cb7b56d9005e89eArgyrios Kyrtzidis  BlockScopeInfo(Diagnostic &Diag, Scope *BlockScope, BlockDecl *Block)
134469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall    : FunctionScopeInfo(Diag), TheDecl(Block), TheScope(BlockScope),
135469a1eb996e1cb0be54f9b210f836afbddcbb2ccJohn McCall      CapturesCXXThis(false)
136781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  {
137781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    IsBlockInfo = true;
138781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
139781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
140781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  virtual ~BlockScopeInfo();
141781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
142781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const FunctionScopeInfo *FSI) { return FSI->IsBlockInfo; }
143781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const BlockScopeInfo *BSI) { return true; }
144781472fe99a120098c631b0cbe33c89f8cef5e70John McCall};
145781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
146781472fe99a120098c631b0cbe33c89f8cef5e70John McCall}
147781472fe99a120098c631b0cbe33c89f8cef5e70John McCall}
148781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
149781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#endif
150