/
Scope.h
189 lines (152 loc) · 4.96 KB
/
Scope.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//===--- Scope.h - Scope Abstraction ----------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the Sema interface which implement hooks invoked by the
// parser to build the AST.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SEMA_SCOPE_H
#define SWIFT_SEMA_SCOPE_H
#include "swift/AST/Identifier.h"
#include "swift/Basic/TreeScopedHashTable.h"
#include "llvm/ADT/SmallVector.h"
namespace swift {
class ValueDecl;
class Parser;
class Scope;
class SavedScope;
/// ScopeInfo - A single instance of this class is maintained by the Parser to
/// track the current scope.
class ScopeInfo {
friend class Scope;
public:
typedef std::pair<unsigned, ValueDecl*> ValueScopeEntry;
typedef TreeScopedHashTable<DeclName, ValueScopeEntry> ScopedHTTy;
typedef ScopedHTTy::ScopeTy ScopedHTScopeTy;
typedef ScopedHTTy::DetachedScopeTy ScopedHTDetachedScopeTy;
private:
ScopedHTTy HT;
Scope *CurScope = nullptr;
unsigned ResolvableDepth = 0;
public:
ValueDecl *lookupValueName(DeclName Name);
Scope *getCurrentScope() const { return CurScope; }
/// addToScope - Register the specified decl as being in the current lexical
/// scope.
void addToScope(ValueDecl *D, Parser &TheParser);
bool isInactiveConfigBlock() const;
SavedScope saveCurrentScope();
};
enum class ScopeKind {
Extension,
FunctionBody,
Generics,
EnumBody,
StructBody,
ClassBody,
ProtocolBody,
ConstructorBody,
DestructorBody,
Brace,
TopLevel,
ForVars,
ForeachVars,
CaseVars,
CatchVars,
WhileVars,
IfVars,
ActiveConfigBlock,
ClosureParams,
};
/// \brief An opaque object that owns the scope frame. The scope frame can be
/// re-entered later.
class SavedScope {
friend class Scope;
ScopeInfo::ScopedHTDetachedScopeTy HTDetachedScope;
unsigned Depth;
ScopeKind Kind;
bool IsInactiveConfigBlock;
SavedScope() = delete;
SavedScope(const SavedScope &) = delete;
void operator=(const SavedScope &) = delete;
public:
SavedScope(SavedScope &&Other) = default;
SavedScope &operator=(SavedScope &&) = default;
~SavedScope() = default;
SavedScope(ScopeInfo::ScopedHTDetachedScopeTy &&HTDetachedScope,
unsigned Depth, ScopeKind Kind, bool IsInactiveConfigBlock)
: HTDetachedScope(std::move(HTDetachedScope)), Depth(Depth), Kind(Kind),
IsInactiveConfigBlock(IsInactiveConfigBlock) {}
};
/// Scope - This class represents lexical scopes. These objects are created
/// and destroyed as the parser is running, and name lookup happens relative
/// to them.
///
class Scope {
friend class ScopeInfo;
Scope(const Scope&) = delete;
void operator=(const Scope&) = delete;
ScopeInfo &SI;
ScopeInfo::ScopedHTScopeTy HTScope;
Scope *PrevScope;
unsigned PrevResolvableDepth;
unsigned Depth;
ScopeKind Kind;
bool IsInactiveConfigBlock;
/// \brief Save this scope so that it can be re-entered later. Transfers the
/// ownership of the scope frame to returned object.
SavedScope saveScope() {
return SavedScope(HTScope.detach(), Depth, Kind, IsInactiveConfigBlock);
}
unsigned getDepth() const {
return Depth;
}
bool isResolvable() const;
public:
/// \brief Create a lexical scope of the specified kind.
Scope(Parser *P, ScopeKind SC, bool IsInactiveConfigBlock = false);
/// \brief Re-enter the specified scope, transferring the ownership of the
/// scope frame to the new object.
Scope(Parser *P, SavedScope &&SS);
ScopeKind getKind() const { return Kind; }
~Scope() {
// Active config blocks delegate to the enclosing scope, so there's nothing
// to pop off.
assert(SI.CurScope == this && "Scope mismatch");
SI.CurScope = PrevScope;
SI.ResolvableDepth = PrevResolvableDepth;
}
};
inline ValueDecl *ScopeInfo::lookupValueName(DeclName Name) {
// FIXME: this check can go away when SIL parser parses everything in
// a toplevel scope.
if (!CurScope)
return nullptr;
assert(CurScope && "no scope");
// If we found nothing, or we found a decl at the top-level, return nothing.
// We ignore results at the top-level because we may have overloading that
// will be resolved properly by name binding.
std::pair<unsigned, ValueDecl *> Res = HT.lookup(CurScope->HTScope, Name);
if (Res.first < ResolvableDepth)
return 0;
return Res.second;
}
inline bool ScopeInfo::isInactiveConfigBlock() const {
if (!CurScope)
return false;
return CurScope->IsInactiveConfigBlock;
}
inline SavedScope ScopeInfo::saveCurrentScope() {
return CurScope->saveScope();
}
} // end namespace swift
#endif