ScopeInfo.h revision 469a1eb996e1cb0be54f9b210f836afbddcbb2cc
1//===--- ScopeInfo.h - Information about a semantic context -----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines FunctionScopeInfo and BlockScopeInfo.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SEMA_SCOPE_INFO_H
15#define LLVM_CLANG_SEMA_SCOPE_INFO_H
16
17#include "clang/AST/Type.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/SetVector.h"
21
22namespace clang {
23
24class BlockDecl;
25class IdentifierInfo;
26class LabelStmt;
27class ReturnStmt;
28class Scope;
29class SwitchStmt;
30
31namespace sema {
32
33/// \brief Retains information about a function, method, or block that is
34/// currently being parsed.
35class FunctionScopeInfo {
36public:
37
38  /// \brief Whether this scope information structure defined information for
39  /// a block.
40  bool IsBlockInfo;
41
42  /// \brief Whether this function contains a VLA, @try, try, C++
43  /// initializer, or anything else that can't be jumped past.
44  bool HasBranchProtectedScope;
45
46  /// \brief Whether this function contains any switches or direct gotos.
47  bool HasBranchIntoScope;
48
49  /// \brief Whether this function contains any indirect gotos.
50  bool HasIndirectGoto;
51
52  /// \brief Used to determine if errors occurred in this function or block.
53  DiagnosticErrorTrap ErrorTrap;
54
55  /// LabelMap - This is a mapping from label identifiers to the LabelStmt for
56  /// it (which acts like the label decl in some ways).  Forward referenced
57  /// labels have a LabelStmt created for them with a null location & SubStmt.
58  llvm::DenseMap<IdentifierInfo*, LabelStmt*> LabelMap;
59
60  /// SwitchStack - This is the current set of active switch statements in the
61  /// block.
62  llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
63
64  /// \brief The list of return statements that occur within the function or
65  /// block, if there is any chance of applying the named return value
66  /// optimization.
67  llvm::SmallVector<ReturnStmt *, 4> Returns;
68
69  void setHasBranchIntoScope() {
70    HasBranchIntoScope = true;
71  }
72
73  void setHasBranchProtectedScope() {
74    HasBranchProtectedScope = true;
75  }
76
77  void setHasIndirectGoto() {
78    HasIndirectGoto = true;
79  }
80
81  bool NeedsScopeChecking() const {
82    return HasIndirectGoto ||
83          (HasBranchProtectedScope && HasBranchIntoScope);
84  }
85
86  FunctionScopeInfo(Diagnostic &Diag)
87    : IsBlockInfo(false),
88      HasBranchProtectedScope(false),
89      HasBranchIntoScope(false),
90      HasIndirectGoto(false),
91      ErrorTrap(Diag) { }
92
93  virtual ~FunctionScopeInfo();
94
95  /// \brief Clear out the information in this function scope, making it
96  /// suitable for reuse.
97  void Clear();
98
99  static bool classof(const FunctionScopeInfo *FSI) { return true; }
100};
101
102/// \brief Retains information about a block that is currently being parsed.
103class BlockScopeInfo : public FunctionScopeInfo {
104public:
105  BlockDecl *TheDecl;
106
107  /// TheScope - This is the scope for the block itself, which contains
108  /// arguments etc.
109  Scope *TheScope;
110
111  /// ReturnType - The return type of the block, or null if the block
112  /// signature didn't provide an explicit return type.
113  QualType ReturnType;
114
115  /// BlockType - The function type of the block, if one was given.
116  /// Its return type may be BuiltinType::Dependent.
117  QualType FunctionType;
118
119  /// Captures - The set of variables captured by this block.
120  llvm::SmallSetVector<VarDecl*, 4> Captures;
121
122  /// CapturesCXXThis - Whether this block captures 'this'.
123  bool CapturesCXXThis;
124
125  BlockScopeInfo(Diagnostic &Diag, Scope *BlockScope, BlockDecl *Block)
126    : FunctionScopeInfo(Diag), TheDecl(Block), TheScope(BlockScope),
127      CapturesCXXThis(false)
128  {
129    IsBlockInfo = true;
130  }
131
132  virtual ~BlockScopeInfo();
133
134  static bool classof(const FunctionScopeInfo *FSI) { return FSI->IsBlockInfo; }
135  static bool classof(const BlockScopeInfo *BSI) { return true; }
136};
137
138}
139}
140
141#endif
142