forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScope.cpp
169 lines (144 loc) · 5.57 KB
/
Scope.cpp
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
//===--- Scope.cpp - Scope Implementation ---------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements semantic analysis for Swift declarations.
//
//===----------------------------------------------------------------------===//
#include "swift/Parse/Scope.h"
#include "swift/Parse/Parser.h"
#include "llvm/ADT/Twine.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Scope Implementation
//===----------------------------------------------------------------------===//
static bool isResolvableScope(ScopeKind SK) {
switch (SK) {
case ScopeKind::Extension:
case ScopeKind::EnumBody:
case ScopeKind::StructBody:
case ScopeKind::ClassBody:
case ScopeKind::ProtocolBody:
case ScopeKind::TopLevel:
case ScopeKind::InheritanceClause:
return false;
case ScopeKind::FunctionBody:
case ScopeKind::Generics:
case ScopeKind::Brace:
case ScopeKind::ForeachVars:
case ScopeKind::ClosureParams:
case ScopeKind::CaseVars:
case ScopeKind::CatchVars:
case ScopeKind::IfVars:
case ScopeKind::WhileVars:
return true;
}
llvm_unreachable("Unhandled ScopeKind in switch.");
}
Scope::Scope(Parser *P, ScopeKind SC, bool isInactiveConfigBlock)
: SI(P->getScopeInfo()),
HTScope(SI.HT, SI.CurScope ? &SI.CurScope->HTScope : nullptr),
PrevScope(SI.CurScope),
PrevResolvableDepth(SI.ResolvableDepth),
Kind(SC),
IsInactiveConfigBlock(isInactiveConfigBlock) {
assert(PrevScope || Kind == ScopeKind::TopLevel);
if (SI.CurScope) {
Depth = SI.CurScope->Depth + 1;
IsInactiveConfigBlock |= SI.CurScope->IsInactiveConfigBlock;
} else {
Depth = 0;
}
SI.CurScope = this;
if (!isResolvableScope(Kind))
SI.ResolvableDepth = Depth + 1;
}
Scope::Scope(Parser *P, SavedScope &&SS):
SI(P->getScopeInfo()),
HTScope(std::move(SS.HTDetachedScope)),
PrevScope(SI.CurScope),
PrevResolvableDepth(SI.ResolvableDepth),
Depth(SS.Depth),
Kind(SS.Kind),
IsInactiveConfigBlock(SS.IsInactiveConfigBlock) {
SI.CurScope = this;
if (!isResolvableScope(Kind))
SI.ResolvableDepth = Depth + 1;
}
bool Scope::isResolvable() const {
return isResolvableScope(Kind);
}
//===----------------------------------------------------------------------===//
// ScopeInfo Implementation
//===----------------------------------------------------------------------===//
/// checkValidOverload - Check whether it is ok for D1 and D2 to be declared at
/// the same scope. This check is a transitive relationship, so if "D1 is a
/// valid overload of D2" and "D2 is a valid overload of D3" then we know that
/// D1/D3 are valid overloads and we don't have to check all permutations.
static bool checkValidOverload(const ValueDecl *D1, const ValueDecl *D2,
Parser &P) {
// Currently, there is no restriction on overloading.
return false;
}
/// addToScope - Register the specified decl as being in the current lexical
/// scope.
void ScopeInfo::addToScope(ValueDecl *D, Parser &TheParser,
bool diagnoseRedefinitions) {
if (!CurScope->isResolvable())
return;
assert(CurScope->getDepth() >= ResolvableDepth &&
"inserting names into a non-resolvable scope");
// If we have a shadowed variable definition, check to see if we have a
// redefinition: two definitions in the same scope with the same name.
ScopedHTTy::iterator EntryI = HT.begin(CurScope->HTScope, D->getFullName());
// A redefinition is a hit in the scoped table at the same depth.
if (EntryI != HT.end() && EntryI->first == CurScope->getDepth()) {
ValueDecl *PrevDecl = EntryI->second;
// If this is in a resolvable scope, diagnose redefinitions. Later
// phases will handle scopes like module-scope, etc.
if (CurScope->getDepth() >= ResolvableDepth) {
if (diagnoseRedefinitions) {
return TheParser.diagnoseRedefinition(PrevDecl, D);
}
return;
}
// If this is at top-level scope, validate that the members of the overload
// set all agree.
// Check to see if D and PrevDecl are valid in the same overload set.
if (checkValidOverload(D, PrevDecl, TheParser))
return;
// Note: we don't check whether all of the elements of the overload set have
// different argument types. This is checked later.
}
HT.insertIntoScope(CurScope->HTScope,
D->getFullName(),
std::make_pair(CurScope->getDepth(), D));
}
void ScopeInfo::dump() const {
#ifndef NDEBUG
// Dump out the current list of scopes.
if (!CurScope->isResolvable())
return;
assert(CurScope->getDepth() >= ResolvableDepth &&
"Attempting to dump a non-resolvable scope?!");
llvm::dbgs() << "--- Dumping ScopeInfo ---\n";
std::function<void(decltype(HT)::DebugVisitValueTy)> func =
[&](const decltype(HT)::DebugVisitValueTy &iter) -> void {
llvm::dbgs() << "DeclName: " << iter->getKey() << "\n"
<< "KeyScopeID: " << iter->getValue().first << "\n"
<< "Decl: ";
iter->getValue().second->dumpRef(llvm::dbgs());
llvm::dbgs() << "\n";
};
HT.debugVisit(std::move(func));
llvm::dbgs() << "\n";
#endif
}