ScopeInfo.h revision 337e550218128e7d922c09bb354fbc71de90c568
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 LabelDecl;
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  /// SwitchStack - This is the current set of active switch statements in the
56  /// block.
57  llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
58
59  /// \brief The list of return statements that occur within the function or
60  /// block, if there is any chance of applying the named return value
61  /// optimization.
62  llvm::SmallVector<ReturnStmt*, 4> Returns;
63
64  void setHasBranchIntoScope() {
65    HasBranchIntoScope = true;
66  }
67
68  void setHasBranchProtectedScope() {
69    HasBranchProtectedScope = true;
70  }
71
72  void setHasIndirectGoto() {
73    HasIndirectGoto = true;
74  }
75
76  bool NeedsScopeChecking() const {
77    return HasIndirectGoto ||
78          (HasBranchProtectedScope && HasBranchIntoScope);
79  }
80
81  FunctionScopeInfo(Diagnostic &Diag)
82    : IsBlockInfo(false),
83      HasBranchProtectedScope(false),
84      HasBranchIntoScope(false),
85      HasIndirectGoto(false),
86      ErrorTrap(Diag) { }
87
88  virtual ~FunctionScopeInfo();
89
90  /// \brief Clear out the information in this function scope, making it
91  /// suitable for reuse.
92  void Clear();
93
94  static bool classof(const FunctionScopeInfo *FSI) { return true; }
95};
96
97/// \brief Retains information about a block that is currently being parsed.
98class BlockScopeInfo : public FunctionScopeInfo {
99public:
100  BlockDecl *TheDecl;
101
102  /// TheScope - This is the scope for the block itself, which contains
103  /// arguments etc.
104  Scope *TheScope;
105
106  /// ReturnType - The return type of the block, or null if the block
107  /// signature didn't provide an explicit return type.
108  QualType ReturnType;
109
110  /// BlockType - The function type of the block, if one was given.
111  /// Its return type may be BuiltinType::Dependent.
112  QualType FunctionType;
113
114  /// CaptureMap - A map of captured variables to (index+1) into Captures.
115  llvm::DenseMap<VarDecl*, unsigned> CaptureMap;
116
117  /// Captures - The captured variables.
118  llvm::SmallVector<BlockDecl::Capture, 4> Captures;
119
120  /// CapturesCXXThis - Whether this block captures 'this'.
121  bool CapturesCXXThis;
122
123  BlockScopeInfo(Diagnostic &Diag, Scope *BlockScope, BlockDecl *Block)
124    : FunctionScopeInfo(Diag), TheDecl(Block), TheScope(BlockScope),
125      CapturesCXXThis(false)
126  {
127    IsBlockInfo = true;
128  }
129
130  virtual ~BlockScopeInfo();
131
132  static bool classof(const FunctionScopeInfo *FSI) { return FSI->IsBlockInfo; }
133  static bool classof(const BlockScopeInfo *BSI) { return true; }
134};
135
136}
137}
138
139#endif
140