forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope_resolver.cc
187 lines (169 loc) · 6.43 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// 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 <optional>
#include <string>
#include <vector>
#include "absl/container/flat_hash_set.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "common/util/logging.h"
#include "verilog/tools/kythe/kythe_facts.h"
namespace verilog {
namespace kythe {
void ScopeResolver::SetCurrentScope(const Signature &scope) {
if (current_scope_ == scope && !current_scope_digest_.rolling_hash.empty()) {
return;
}
current_scope_digest_ = scope.Digest();
current_scope_ = scope;
if (enable_debug_) {
scope_to_string_debug_.emplace(scope.Digest(), scope.ToString());
}
VLOG(2) << "Set scope to: " << ScopeDebug(scope.Digest());
}
void ScopeResolver::RemoveDefinitionFromCurrentScope(const VName &vname) {
absl::string_view name = vname.signature.Names().back();
auto scopes = variable_to_scoped_vname_.find(name);
if (scopes == variable_to_scoped_vname_.end()) {
VLOG(1) << "No definition for '" << name << "'. Nothing to remove.";
return;
}
SignatureDigest current_scope_digest = CurrentScopeDigest();
VLOG(2) << "Remove " << name << " from " << ScopeDebug(current_scope_digest);
for (auto iter = scopes->second.begin(); iter != scopes->second.end();
++iter) {
if (iter->instantiation_scope == current_scope_digest) {
scopes->second.erase(iter);
break;
}
}
auto current_scope_names = scope_to_vnames_.find(current_scope_digest);
if (current_scope_names == scope_to_vnames_.end()) {
return;
}
auto &vnames = current_scope_names->second;
for (auto iter = vnames.begin(); iter != vnames.end(); ++iter) {
if (*iter == vname) {
vnames.erase(iter);
break;
}
}
}
void ScopeResolver::AppendScopeToCurrentScope(
const SignatureDigest &source_scope) {
AppendScopeToScope(source_scope, CurrentScopeDigest());
}
void ScopeResolver::AppendScopeToScope(
const SignatureDigest &source_scope,
const SignatureDigest &destination_scope) {
auto scope_vnames = scope_to_vnames_.find(source_scope);
if (scope_vnames == scope_to_vnames_.end()) {
VLOG(2) << "Can't find scope " << ScopeDebug(source_scope)
<< " to append it to the current scope";
return;
}
if (source_scope == destination_scope) {
// The source and destination scope are equal. Nothing to add.
return;
}
for (const auto &vn : scope_vnames->second) {
const std::optional<ScopedVname> vn_type =
FindScopeAndDefinition(vn.signature.Names().back(), source_scope);
if (!vn_type) {
continue;
}
variable_to_scoped_vname_[vn.signature.Names().back()].insert(
ScopedVname{.type_scope = vn_type->type_scope,
.instantiation_scope = destination_scope,
.vname = vn});
scope_to_vnames_[destination_scope].insert(vn);
}
}
void ScopeResolver::AddDefinitionToCurrentScope(const VName &new_member) {
AddDefinitionToCurrentScope(new_member, new_member.signature.Digest());
}
void ScopeResolver::AddDefinitionToCurrentScope(
const VName &new_member, const SignatureDigest &type_scope) {
// Remove the existing definition -- overwrite it with the new which has
// updated information about types.
RemoveDefinitionFromCurrentScope(new_member);
auto current_scope_digest = CurrentScopeDigest();
variable_to_scoped_vname_[new_member.signature.Names().back()].insert(
ScopedVname{.type_scope = type_scope,
.instantiation_scope = current_scope_digest,
.vname = new_member});
scope_to_vnames_[current_scope_digest].insert(new_member);
}
std::optional<ScopedVname> ScopeResolver::FindScopeAndDefinition(
absl::string_view name, const SignatureDigest &scope_focus) {
VLOG(2) << "Find definition for '" << name << "' within scope "
<< ScopeDebug(scope_focus);
auto scope = variable_to_scoped_vname_.find(name);
if (scope == variable_to_scoped_vname_.end()) {
VLOG(2) << "Failed to find definition for '" << name << "' within scope "
<< ScopeDebug(scope_focus) << " (unregistered name)";
return {};
}
const ScopedVname *match = nullptr;
for (auto &scope_member : scope->second) {
SignatureDigest digest = scope_member.instantiation_scope;
if (scope_focus.rolling_hash.size() < digest.rolling_hash.size() ||
(match != nullptr &&
digest.rolling_hash.size() <
match->instantiation_scope.rolling_hash.size())) {
// Mismatch, or not interesting (worse match).
VLOG(2) << "Scope resolution mismatch for '" << name << "' at scope "
<< ScopeDebug(digest);
continue;
}
if (scope_focus.rolling_hash[digest.rolling_hash.size() - 1] ==
digest.Hash()) {
match = &scope_member;
}
}
if (match != nullptr) {
VLOG(2) << "Found definition for '" << name << "' within scope "
<< ScopeDebug(scope_focus);
return *match;
}
VLOG(2) << "Failed to find definition for '" << name << "' within scope "
<< ScopeDebug(scope_focus);
return {};
}
std::optional<ScopedVname> ScopeResolver::FindScopeAndDefinition(
absl::string_view name) {
return FindScopeAndDefinition(name, CurrentScopeDigest());
}
const absl::flat_hash_set<VName> &ScopeResolver::ListScopeMembers(
const SignatureDigest &scope_digest) const {
const static absl::flat_hash_set<VName> kEmptyMemberList;
auto scope = scope_to_vnames_.find(scope_digest);
if (scope == scope_to_vnames_.end()) {
return kEmptyMemberList;
}
return scope->second;
}
std::string ScopeResolver::ScopeDebug(const SignatureDigest &scope) const {
if (!enable_debug_) {
return "UNKNOWN (debug off)";
}
const auto s = scope_to_string_debug_.find(scope);
if (s == scope_to_string_debug_.end()) {
return absl::StrCat("UNKNOWN ", scope.Hash());
}
return absl::StrCat(s->second, " H: ", scope.Hash());
}
} // namespace kythe
} // namespace verilog