forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.cc
155 lines (133 loc) · 5.98 KB
/
module.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
// 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/module.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/token_info.h"
#include "common/text/tree_utils.h"
#include "verilog/CST/verilog_matchers.h" // IWYU pragma: keep
namespace verilog {
using verible::Symbol;
using verible::SyntaxTreeNode;
using verible::TokenInfo;
std::vector<verible::TreeSearchMatch> FindAllModuleDeclarations(
const Symbol& root) {
return SearchSyntaxTree(root, NodekModuleDeclaration());
}
std::vector<verible::TreeSearchMatch> FindAllModuleHeaders(const Symbol& root) {
return SearchSyntaxTree(root, NodekModuleHeader());
}
std::vector<verible::TreeSearchMatch> FindAllInterfaceDeclarations(
const verible::Symbol& root) {
return SearchSyntaxTree(root, NodekInterfaceDeclaration());
}
std::vector<verible::TreeSearchMatch> FindAllProgramDeclarations(
const verible::Symbol& root) {
return SearchSyntaxTree(root, NodekProgramDeclaration());
}
bool IsModuleOrInterfaceOrProgramDeclaration(
const SyntaxTreeNode& declaration) {
return declaration.MatchesTagAnyOf({NodeEnum::kModuleDeclaration,
NodeEnum::kInterfaceDeclaration,
NodeEnum::kProgramDeclaration});
}
const SyntaxTreeNode* GetModuleHeader(const Symbol& module_declaration) {
if (module_declaration.Kind() != verible::SymbolKind::kNode) return nullptr;
const SyntaxTreeNode& module_node =
verible::SymbolCastToNode(module_declaration);
if (!IsModuleOrInterfaceOrProgramDeclaration(module_node)) return nullptr;
if (module_node.children().empty()) return nullptr;
return &verible::SymbolCastToNode(*module_node[0].get());
}
const SyntaxTreeNode* GetInterfaceHeader(const Symbol& module_symbol) {
return verible::GetSubtreeAsNode(module_symbol,
NodeEnum::kInterfaceDeclaration, 0,
NodeEnum::kModuleHeader);
}
const verible::SyntaxTreeLeaf* GetModuleName(const Symbol& s) {
const auto* header_node = GetModuleHeader(s);
if (!header_node) return nullptr;
return verible::GetSubtreeAsLeaf(*header_node, NodeEnum::kModuleHeader, 2);
}
const TokenInfo* GetInterfaceNameToken(const Symbol& s) {
const auto* header_node = GetInterfaceHeader(s);
if (!header_node) return nullptr;
const verible::SyntaxTreeLeaf* name_leaf =
verible::GetSubtreeAsLeaf(*header_node, NodeEnum::kModuleHeader, 2);
return name_leaf ? &name_leaf->get() : nullptr;
}
const SyntaxTreeNode* GetModulePortParenGroup(
const Symbol& module_declaration) {
const auto* header_node = GetModuleHeader(module_declaration);
if (!header_node) return nullptr;
const auto* ports =
verible::GetSubtreeAsSymbol(*header_node, NodeEnum::kModuleHeader, 5);
return verible::CheckOptionalSymbolAsNode(ports, NodeEnum::kParenGroup);
}
const SyntaxTreeNode* GetModulePortDeclarationList(
const Symbol& module_declaration) {
const auto* paren_group = GetModulePortParenGroup(module_declaration);
if (verible::CheckOptionalSymbolAsNode(paren_group, NodeEnum::kParenGroup) ==
nullptr) {
return nullptr;
}
return verible::GetSubtreeAsNode(*paren_group, NodeEnum::kParenGroup, 1,
NodeEnum::kPortDeclarationList);
}
const verible::SyntaxTreeLeaf* GetModuleEndLabel(
const verible::Symbol& module_declaration) {
const SyntaxTreeNode& module_node =
verible::SymbolCastToNode(module_declaration);
CHECK(IsModuleOrInterfaceOrProgramDeclaration(module_node));
const auto* label_node = module_node[3].get();
if (label_node == nullptr) {
return nullptr;
}
return verible::GetSubtreeAsLeaf(verible::SymbolCastToNode(*label_node),
NodeEnum::kLabel, 1);
}
const verible::SyntaxTreeNode* GetModuleItemList(
const verible::Symbol& module_declaration) {
if (module_declaration.Kind() != verible::SymbolKind::kNode) return nullptr;
const SyntaxTreeNode& module_node =
verible::SymbolCastToNode(module_declaration);
if (!IsModuleOrInterfaceOrProgramDeclaration(module_node)) return nullptr;
if (module_node.children().size() < 2) return nullptr;
verible::Symbol* item = module_node[1].get();
return item ? &verible::SymbolCastToNode(*item) : nullptr;
}
const verible::SyntaxTreeNode* GetParamDeclarationListFromModuleDeclaration(
const verible::Symbol& module_declaration) {
const auto* header_node = GetModuleHeader(module_declaration);
if (!header_node) return nullptr;
const verible::Symbol* param_declaration_list =
verible::GetSubtreeAsSymbol(*header_node, NodeEnum::kModuleHeader, 4);
return verible::CheckOptionalSymbolAsNode(
param_declaration_list, NodeEnum::kFormalParameterListDeclaration);
}
const verible::SyntaxTreeNode* GetParamDeclarationListFromInterfaceDeclaration(
const verible::Symbol& interface_declaration) {
const auto* header_node = GetInterfaceHeader(interface_declaration);
if (!header_node) return nullptr;
const verible::Symbol* param_declaration_list =
verible::GetSubtreeAsSymbol(*header_node, NodeEnum::kModuleHeader, 4);
return verible::CheckOptionalSymbolAsNode(
param_declaration_list, NodeEnum::kFormalParameterListDeclaration);
}
} // namespace verilog