forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_resolver.cc
171 lines (142 loc) · 5.13 KB
/
scope_resolver.cc
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
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "verilog/tools/kythe/scope_resolver.h"
#include <vector>
#include "absl/strings/string_view.h"
namespace verilog {
namespace kythe {
bool ScopeMemberItem::operator<(const ScopeMemberItem& other) const {
return this->vname < other.vname;
}
void Scope::AddMemberItem(const ScopeMemberItem& member_item) {
members_.insert(member_item);
}
void Scope::AppendScope(const Scope& scope) {
for (const ScopeMemberItem& item : scope.Members()) {
this->AddMemberItem(item);
}
}
const VName* Scope::SearchForDefinition(absl::string_view name) const {
for (const ScopeMemberItem& member_item :
verible::make_range(members_.rbegin(), members_.rend())) {
if (member_item.vname.signature.IsNameEqual(name)) {
return &member_item.vname;
}
}
return nullptr;
}
void Scope::RemoveMember(const ScopeMemberItem& member) {
members_.erase(member);
}
const VName* ScopeContext::SearchForDefinition(absl::string_view name) const {
for (const auto& scope : verible::make_range(rbegin(), rend())) {
const VName* result = scope->SearchForDefinition(name);
if (result != nullptr) {
return result;
}
}
return nullptr;
}
void ScopeResolver::RemoveDefinitionFromCurrentScope(const VName& vname) {
scope_context_.top().RemoveMember(vname);
}
void ScopeResolver::MapSignatureToScope(const Signature& signature,
const Scope& scope) {
scopes_[signature] = scope; // copy-assign
}
void ScopeResolver::AppendScopeToCurrentScope(const Scope& scope) {
scope_context_.top().AppendScope(scope);
}
void ScopeResolver::AddDefinitionToCurrentScope(
const ScopeMemberItem& new_member) {
scope_context_.top().AddMemberItem(new_member);
}
const VName* ScopeResolver::SearchForDefinitionInGlobalScope(
absl::string_view reference_name) const {
const VName* definition =
SearchForDefinitionInScope(global_scope_signature_, reference_name);
if (definition != nullptr) {
return definition;
}
if (previous_file_scope_resolver_ != nullptr) {
return previous_file_scope_resolver_->SearchForDefinitionInGlobalScope(
reference_name);
}
return nullptr;
}
const VName* ScopeResolver::SearchForDefinitionInScopeContext(
absl::string_view reference_name) const {
return scope_context_.SearchForDefinition(reference_name);
}
const VName* ScopeResolver::SearchForDefinitionInCurrentScope(
absl::string_view name) const {
return scope_context_.top().SearchForDefinition(name);
}
const std::vector<std::pair<const VName*, const Scope*>>
ScopeResolver::SearchForDefinitions(
const std::vector<absl::string_view>& names) const {
std::vector<std::pair<const VName*, const Scope*>> definitions;
if (names.empty()) {
return definitions;
}
// Try to find the definition in the scopes of the current file.
const VName* definition = SearchForDefinitionInScopeContext(names[0]);
// Try to find the definition in the previous files' scopes.
if (definition == nullptr && previous_file_scope_resolver_ != nullptr) {
// This is a linear-time search over files.
definition =
previous_file_scope_resolver_->SearchForDefinitionInGlobalScope(
names[0]);
}
if (definition == nullptr) {
return definitions;
}
const Scope* current_scope = SearchForScope(definition->signature);
definitions.push_back({definition, current_scope});
// Iterate over the names and try to find the definition in the current scope.
for (const auto& name : verible::make_range(names.begin() + 1, names.end())) {
if (current_scope == nullptr) {
break;
}
const VName* definition = current_scope->SearchForDefinition(name);
if (definition == nullptr) {
break;
}
current_scope = SearchForScope(definition->signature);
definitions.push_back({definition, current_scope});
}
return definitions;
}
const Scope* ScopeResolver::SearchForScope(const Signature& signature) const {
const auto scope = scopes_.find(signature);
if (scope != scopes_.end()) {
return &scope->second;
}
// Try to find the definition in the previous files' scopes.
// This is a linear-time search over files.
if (previous_file_scope_resolver_ != nullptr) {
return previous_file_scope_resolver_->SearchForScope(signature);
}
return nullptr;
}
const VName* ScopeResolver::SearchForDefinitionInScope(
const Signature& signature, absl::string_view name) const {
const Scope* scope = SearchForScope(signature);
if (scope == nullptr) {
return nullptr;
}
return scope->SearchForDefinition(name);
}
} // namespace kythe
} // namespace verilog