ScopeInfo.h revision ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298
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 /// LabelMap - This is a mapping from label identifiers to the LabelDecl for 56 /// it. Forward referenced labels have a LabelDecl created for them with a 57 /// null statement. 58 llvm::DenseMap<IdentifierInfo*, LabelDecl*> 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 /// checkLabelUse - This checks to see if any labels are used without being 96 /// defined, emiting errors and returning true if any are found. This also 97 /// warns about unused labels. 98 bool checkLabelUse(Stmt *Body, Sema &S); 99 100 /// \brief Clear out the information in this function scope, making it 101 /// suitable for reuse. 102 void Clear(); 103 104 static bool classof(const FunctionScopeInfo *FSI) { return true; } 105}; 106 107/// \brief Retains information about a block that is currently being parsed. 108class BlockScopeInfo : public FunctionScopeInfo { 109public: 110 BlockDecl *TheDecl; 111 112 /// TheScope - This is the scope for the block itself, which contains 113 /// arguments etc. 114 Scope *TheScope; 115 116 /// ReturnType - The return type of the block, or null if the block 117 /// signature didn't provide an explicit return type. 118 QualType ReturnType; 119 120 /// BlockType - The function type of the block, if one was given. 121 /// Its return type may be BuiltinType::Dependent. 122 QualType FunctionType; 123 124 /// CaptureMap - A map of captured variables to (index+1) into Captures. 125 llvm::DenseMap<VarDecl*, unsigned> CaptureMap; 126 127 /// Captures - The captured variables. 128 llvm::SmallVector<BlockDecl::Capture, 4> Captures; 129 130 /// CapturesCXXThis - Whether this block captures 'this'. 131 bool CapturesCXXThis; 132 133 BlockScopeInfo(Diagnostic &Diag, Scope *BlockScope, BlockDecl *Block) 134 : FunctionScopeInfo(Diag), TheDecl(Block), TheScope(BlockScope), 135 CapturesCXXThis(false) 136 { 137 IsBlockInfo = true; 138 } 139 140 virtual ~BlockScopeInfo(); 141 142 static bool classof(const FunctionScopeInfo *FSI) { return FSI->IsBlockInfo; } 143 static bool classof(const BlockScopeInfo *BSI) { return true; } 144}; 145 146} 147} 148 149#endif 150