forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 2
/
identifier.cc
104 lines (89 loc) · 3.53 KB
/
identifier.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
// 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/CST/identifier.h"
#include <vector>
#include "common/analysis/matcher/matcher.h"
#include "common/analysis/matcher/matcher_builders.h"
#include "common/analysis/syntax_tree_search.h"
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/symbol.h"
#include "common/text/tree_utils.h"
#include "common/util/casts.h"
#include "common/util/logging.h"
#include "verilog/CST/verilog_matchers.h" // IWYU pragma: keep
#include "verilog/parser/verilog_token_classifications.h"
#include "verilog/parser/verilog_token_enum.h"
namespace verilog {
using verible::down_cast;
using verible::SymbolKind;
std::vector<verible::TreeSearchMatch> FindAllIdentifierUnpackedDimensions(
const verible::Symbol& root) {
return verible::SearchSyntaxTree(root, NodekIdentifierUnpackedDimensions());
}
std::vector<verible::TreeSearchMatch> FindAllPortIdentifiers(
const verible::Symbol& root) {
return verible::SearchSyntaxTree(root, NodekPortIdentifier());
}
std::vector<verible::TreeSearchMatch> FindAllUnqualifiedIds(
const verible::Symbol& root) {
return verible::SearchSyntaxTree(root, NodekUnqualifiedId());
}
std::vector<verible::TreeSearchMatch> FindAllQualifiedIds(
const verible::Symbol& root) {
return verible::SearchSyntaxTree(root, NodekQualifiedId());
}
std::vector<verible::TreeSearchMatch> FindAllSymbolIdentifierLeafs(
const verible::Symbol& root) {
return verible::SearchSyntaxTree(root, SymbolIdentifierLeaf());
}
bool IdIsQualified(const verible::Symbol& symbol) {
auto t = symbol.Tag();
if (t.kind != SymbolKind::kNode) return false;
return NodeEnum(t.tag) == NodeEnum::kQualifiedId;
}
const verible::SyntaxTreeLeaf* GetIdentifier(const verible::Symbol& symbol) {
auto t = symbol.Tag();
if (t.kind != SymbolKind::kNode) return nullptr;
if (NodeEnum(t.tag) != NodeEnum::kUnqualifiedId &&
NodeEnum(t.tag) != NodeEnum::kPortIdentifier) {
return nullptr;
}
const auto& node = down_cast<const verible::SyntaxTreeNode&>(symbol);
const auto* leaf = down_cast<const verible::SyntaxTreeLeaf*>(node[0].get());
return leaf;
}
const verible::SyntaxTreeLeaf* AutoUnwrapIdentifier(
const verible::Symbol& symbol) {
// If it's a leaf, then just return that leaf. Otherwise it is an
// kUnqualifiedId
const auto t = symbol.Tag();
if (t.kind == SymbolKind::kLeaf) {
if (IsIdentifierLike(verilog_tokentype(t.tag))) {
return &verible::SymbolCastToLeaf(symbol);
}
return nullptr;
}
CHECK_EQ(NodeEnum(t.tag), NodeEnum::kUnqualifiedId);
return GetIdentifier(symbol);
}
const verible::SyntaxTreeLeaf*
GetSymbolIdentifierFromIdentifierUnpackedDimensions(
const verible::Symbol& identifier_unpacked_dimension) {
const verible::Symbol* child_node =
GetSubtreeAsSymbol(identifier_unpacked_dimension,
NodeEnum::kIdentifierUnpackedDimensions, 0);
return AutoUnwrapIdentifier(*child_node);
}
} // namespace verilog