ScopeInfo.h revision 781472fe99a120098c631b0cbe33c89f8cef5e70
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"
20781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
21781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace clang {
22781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
23781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass BlockDecl;
24781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass IdentifierInfo;
25781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass LabelStmt;
26781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass ReturnStmt;
27781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass Scope;
28781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass SwitchStmt;
29781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
30781472fe99a120098c631b0cbe33c89f8cef5e70John McCallnamespace sema {
31781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
32781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// \brief Retains information about a function, method, or block that is
33781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// currently being parsed.
34781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass FunctionScopeInfo {
35781472fe99a120098c631b0cbe33c89f8cef5e70John McCallpublic:
36781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
37781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this scope information structure defined information for
38781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// a block.
39781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool IsBlockInfo;
40781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
41781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains a VLA, @try, try, C++
42781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// initializer, or anything else that can't be jumped past.
43781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchProtectedScope;
44781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
45781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any switches or direct gotos.
46781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasBranchIntoScope;
47781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
48781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Whether this function contains any indirect gotos.
49781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool HasIndirectGoto;
50781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
51781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief The number of errors that had occurred before starting this
52781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// function or block.
53781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  unsigned NumErrorsAtStartOfFunction;
54781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
55781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// LabelMap - This is a mapping from label identifiers to the LabelStmt for
56781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// it (which acts like the label decl in some ways).  Forward referenced
57781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// labels have a LabelStmt created for them with a null location & SubStmt.
58781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  llvm::DenseMap<IdentifierInfo*, LabelStmt*> 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
86781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  FunctionScopeInfo(unsigned NumErrors)
87781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    : IsBlockInfo(false),
88781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchProtectedScope(false),
89781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasBranchIntoScope(false),
90781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      HasIndirectGoto(false),
91781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      NumErrorsAtStartOfFunction(NumErrors) { }
92781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
93781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  virtual ~FunctionScopeInfo();
94781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
95781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// \brief Clear out the information in this function scope, making it
96781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// suitable for reuse.
97781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  void Clear(unsigned NumErrors);
98781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
99781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const FunctionScopeInfo *FSI) { return true; }
100781472fe99a120098c631b0cbe33c89f8cef5e70John McCall};
101781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
102781472fe99a120098c631b0cbe33c89f8cef5e70John McCall/// \brief Retains information about a block that is currently being parsed.
103781472fe99a120098c631b0cbe33c89f8cef5e70John McCallclass BlockScopeInfo : public FunctionScopeInfo {
104781472fe99a120098c631b0cbe33c89f8cef5e70John McCallpublic:
105781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  bool hasBlockDeclRefExprs;
106781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
107781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  BlockDecl *TheDecl;
108781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
109781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// TheScope - This is the scope for the block itself, which contains
110781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// arguments etc.
111781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  Scope *TheScope;
112781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
113781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// ReturnType - The return type of the block, or null if the block
114781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// signature didn't provide an explicit return type.
115781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  QualType ReturnType;
116781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
117781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// BlockType - The function type of the block, if one was given.
118781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  /// Its return type may be BuiltinType::Dependent.
119781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  QualType FunctionType;
120781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
121781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  BlockScopeInfo(unsigned NumErrors, Scope *BlockScope, BlockDecl *Block)
122781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    : FunctionScopeInfo(NumErrors), hasBlockDeclRefExprs(false),
123781472fe99a120098c631b0cbe33c89f8cef5e70John McCall      TheDecl(Block), TheScope(BlockScope)
124781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  {
125781472fe99a120098c631b0cbe33c89f8cef5e70John McCall    IsBlockInfo = true;
126781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  }
127781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
128781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  virtual ~BlockScopeInfo();
129781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
130781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const FunctionScopeInfo *FSI) { return FSI->IsBlockInfo; }
131781472fe99a120098c631b0cbe33c89f8cef5e70John McCall  static bool classof(const BlockScopeInfo *BSI) { return true; }
132781472fe99a120098c631b0cbe33c89f8cef5e70John McCall};
133781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
134781472fe99a120098c631b0cbe33c89f8cef5e70John McCall}
135781472fe99a120098c631b0cbe33c89f8cef5e70John McCall}
136781472fe99a120098c631b0cbe33c89f8cef5e70John McCall
137781472fe99a120098c631b0cbe33c89f8cef5e70John McCall#endif
138