• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "clang/Basic/PartialDiagnostic.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/SetVector.h"
22 
23 namespace clang {
24 
25 class BlockDecl;
26 class IdentifierInfo;
27 class LabelDecl;
28 class ReturnStmt;
29 class Scope;
30 class SwitchStmt;
31 
32 namespace sema {
33 
34 class PossiblyUnreachableDiag {
35 public:
36   PartialDiagnostic PD;
37   SourceLocation Loc;
38   const Stmt *stmt;
39 
PossiblyUnreachableDiag(const PartialDiagnostic & PD,SourceLocation Loc,const Stmt * stmt)40   PossiblyUnreachableDiag(const PartialDiagnostic &PD, SourceLocation Loc,
41                           const Stmt *stmt)
42     : PD(PD), Loc(Loc), stmt(stmt) {}
43 };
44 
45 /// \brief Retains information about a function, method, or block that is
46 /// currently being parsed.
47 class FunctionScopeInfo {
48 public:
49 
50   /// \brief Whether this scope information structure defined information for
51   /// a block.
52   bool IsBlockInfo;
53 
54   /// \brief Whether this function contains a VLA, @try, try, C++
55   /// initializer, or anything else that can't be jumped past.
56   bool HasBranchProtectedScope;
57 
58   /// \brief Whether this function contains any switches or direct gotos.
59   bool HasBranchIntoScope;
60 
61   /// \brief Whether this function contains any indirect gotos.
62   bool HasIndirectGoto;
63 
64   /// \brief Used to determine if errors occurred in this function or block.
65   DiagnosticErrorTrap ErrorTrap;
66 
67   /// SwitchStack - This is the current set of active switch statements in the
68   /// block.
69   llvm::SmallVector<SwitchStmt*, 8> SwitchStack;
70 
71   /// \brief The list of return statements that occur within the function or
72   /// block, if there is any chance of applying the named return value
73   /// optimization.
74   llvm::SmallVector<ReturnStmt*, 4> Returns;
75 
76   /// \brief A list of PartialDiagnostics created but delayed within the
77   /// current function scope.  These diagnostics are vetted for reachability
78   /// prior to being emitted.
79   llvm::SmallVector<PossiblyUnreachableDiag, 4> PossiblyUnreachableDiags;
80 
setHasBranchIntoScope()81   void setHasBranchIntoScope() {
82     HasBranchIntoScope = true;
83   }
84 
setHasBranchProtectedScope()85   void setHasBranchProtectedScope() {
86     HasBranchProtectedScope = true;
87   }
88 
setHasIndirectGoto()89   void setHasIndirectGoto() {
90     HasIndirectGoto = true;
91   }
92 
NeedsScopeChecking()93   bool NeedsScopeChecking() const {
94     return HasIndirectGoto ||
95           (HasBranchProtectedScope && HasBranchIntoScope);
96   }
97 
FunctionScopeInfo(Diagnostic & Diag)98   FunctionScopeInfo(Diagnostic &Diag)
99     : IsBlockInfo(false),
100       HasBranchProtectedScope(false),
101       HasBranchIntoScope(false),
102       HasIndirectGoto(false),
103       ErrorTrap(Diag) { }
104 
105   virtual ~FunctionScopeInfo();
106 
107   /// \brief Clear out the information in this function scope, making it
108   /// suitable for reuse.
109   void Clear();
110 
classof(const FunctionScopeInfo * FSI)111   static bool classof(const FunctionScopeInfo *FSI) { return true; }
112 };
113 
114 /// \brief Retains information about a block that is currently being parsed.
115 class BlockScopeInfo : public FunctionScopeInfo {
116 public:
117   BlockDecl *TheDecl;
118 
119   /// TheScope - This is the scope for the block itself, which contains
120   /// arguments etc.
121   Scope *TheScope;
122 
123   /// ReturnType - The return type of the block, or null if the block
124   /// signature didn't provide an explicit return type.
125   QualType ReturnType;
126 
127   /// BlockType - The function type of the block, if one was given.
128   /// Its return type may be BuiltinType::Dependent.
129   QualType FunctionType;
130 
131   /// CaptureMap - A map of captured variables to (index+1) into Captures.
132   llvm::DenseMap<VarDecl*, unsigned> CaptureMap;
133 
134   /// Captures - The captured variables.
135   llvm::SmallVector<BlockDecl::Capture, 4> Captures;
136 
137   /// CapturesCXXThis - Whether this block captures 'this'.
138   bool CapturesCXXThis;
139 
BlockScopeInfo(Diagnostic & Diag,Scope * BlockScope,BlockDecl * Block)140   BlockScopeInfo(Diagnostic &Diag, Scope *BlockScope, BlockDecl *Block)
141     : FunctionScopeInfo(Diag), TheDecl(Block), TheScope(BlockScope),
142       CapturesCXXThis(false)
143   {
144     IsBlockInfo = true;
145   }
146 
147   virtual ~BlockScopeInfo();
148 
classof(const FunctionScopeInfo * FSI)149   static bool classof(const FunctionScopeInfo *FSI) { return FSI->IsBlockInfo; }
classof(const BlockScopeInfo * BSI)150   static bool classof(const BlockScopeInfo *BSI) { return true; }
151 };
152 
153 }
154 }
155 
156 #endif
157