forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport.cc
170 lines (147 loc) · 6.11 KB
/
port.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
// 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/port.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/logging.h"
#include "verilog/CST/identifier.h"
#include "verilog/CST/verilog_matchers.h" // pragma IWYU: keep
namespace verilog {
using verible::Symbol;
using verible::SyntaxTreeLeaf;
std::vector<verible::TreeSearchMatch> FindAllPortDeclarations(
const Symbol &root) {
return SearchSyntaxTree(root, NodekPortDeclaration());
}
std::vector<verible::TreeSearchMatch> FindAllActualNamedPort(
const Symbol &root) {
return SearchSyntaxTree(root, NodekActualNamedPort());
}
std::vector<verible::TreeSearchMatch> FindAllPortReferences(
const verible::Symbol &root) {
return SearchSyntaxTree(root, NodekPort());
}
std::vector<verible::TreeSearchMatch> FindAllTaskFunctionPortDeclarations(
const Symbol &root) {
return SearchSyntaxTree(root, NodekPortItem());
}
const SyntaxTreeLeaf *GetIdentifierFromPortDeclaration(const Symbol &symbol) {
const auto *identifier_symbol =
verible::GetSubtreeAsSymbol(symbol, NodeEnum::kPortDeclaration, 3);
if (!identifier_symbol) return nullptr;
return AutoUnwrapIdentifier(*identifier_symbol);
}
const SyntaxTreeLeaf *GetDirectionFromPortDeclaration(const Symbol &symbol) {
if (const auto *dir_symbol =
GetSubtreeAsSymbol(symbol, NodeEnum::kPortDeclaration, 0)) {
return &SymbolCastToLeaf(*dir_symbol);
}
return nullptr;
}
std::vector<verible::TreeSearchMatch> FindAllModulePortDeclarations(
const verible::Symbol &root) {
return SearchSyntaxTree(root, NodekModulePortDeclaration());
}
const verible::SyntaxTreeLeaf *GetIdentifierFromModulePortDeclaration(
const verible::Symbol &symbol) {
static const char *const TOO_MANY_IDS_ERROR =
"Expected one identifier node in module port declaration, but got ";
auto &node = SymbolCastToNode(symbol);
if (!MatchNodeEnumOrNull(node, NodeEnum::kModulePortDeclaration)) {
return nullptr;
}
auto id_unpacked_dims = FindAllIdentifierUnpackedDimensions(symbol);
if (id_unpacked_dims.empty()) {
auto port_ids = verible::SearchSyntaxTree(symbol, NodekPortIdentifier());
if (port_ids.size() > 1) {
LOG(ERROR) << TOO_MANY_IDS_ERROR << port_ids.size();
}
if (port_ids.empty()) return nullptr;
return GetIdentifier(*port_ids[0].match);
}
if (id_unpacked_dims.size() > 1) {
LOG(ERROR) << TOO_MANY_IDS_ERROR << id_unpacked_dims.size();
}
return GetSymbolIdentifierFromIdentifierUnpackedDimensions(
*id_unpacked_dims.front().match);
}
const verible::SyntaxTreeLeaf *GetDirectionFromModulePortDeclaration(
const verible::Symbol &symbol) {
if (const auto *dir_symbol =
GetSubtreeAsSymbol(symbol, NodeEnum::kModulePortDeclaration, 0)) {
return &SymbolCastToLeaf(*dir_symbol);
}
return nullptr;
}
const verible::SyntaxTreeLeaf *GetIdentifierFromPortReference(
const verible::Symbol &port_reference) {
const auto *identifier_symbol =
verible::GetSubtreeAsSymbol(port_reference, NodeEnum::kPortReference, 0);
if (!identifier_symbol) return nullptr;
return AutoUnwrapIdentifier(*identifier_symbol);
}
const verible::SyntaxTreeNode *GetPortReferenceFromPort(
const verible::Symbol &port) {
return verible::GetSubtreeAsNode(port, NodeEnum::kPort, 0,
NodeEnum::kPortReference);
}
static const verible::SyntaxTreeNode *
GetTypeIdDimensionsFromTaskFunctionPortItem(const Symbol &symbol) {
return verible::GetSubtreeAsNode(
symbol, NodeEnum::kPortItem, 1,
NodeEnum::kDataTypeImplicitBasicIdDimensions);
}
const verible::SyntaxTreeNode *GetUnpackedDimensionsFromTaskFunctionPortItem(
const verible::Symbol &port_item) {
const auto &type_id_dimensions =
GetTypeIdDimensionsFromTaskFunctionPortItem(port_item);
if (!type_id_dimensions) return nullptr;
return verible::GetSubtreeAsNode(*type_id_dimensions,
NodeEnum::kDataTypeImplicitBasicIdDimensions,
2, NodeEnum::kUnpackedDimensions);
}
const Symbol *GetTypeOfTaskFunctionPortItem(const verible::Symbol &symbol) {
const auto &type_id_dimensions =
GetTypeIdDimensionsFromTaskFunctionPortItem(symbol);
if (!type_id_dimensions) return nullptr;
return verible::GetSubtreeAsNode(*type_id_dimensions,
NodeEnum::kDataTypeImplicitBasicIdDimensions,
0, NodeEnum::kDataType);
}
const SyntaxTreeLeaf *GetIdentifierFromTaskFunctionPortItem(
const verible::Symbol &symbol) {
const auto *type_id_dimensions =
GetTypeIdDimensionsFromTaskFunctionPortItem(symbol);
if (!type_id_dimensions) return nullptr;
if (type_id_dimensions->children().size() <= 1) return nullptr;
const auto *port_item = (*type_id_dimensions)[1].get();
return port_item ? AutoUnwrapIdentifier(*port_item) : nullptr;
}
const verible::SyntaxTreeLeaf *GetActualNamedPortName(
const verible::Symbol &actual_named_port) {
return verible::GetSubtreeAsLeaf(actual_named_port,
NodeEnum::kActualNamedPort, 1);
}
const verible::Symbol *GetActualNamedPortParenGroup(
const verible::Symbol &actual_named_port) {
return verible::GetSubtreeAsSymbol(actual_named_port,
NodeEnum::kActualNamedPort, 2);
}
} // namespace verilog