forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.h
195 lines (158 loc) · 7.94 KB
/
functions.h
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
188
189
190
191
192
193
194
195
// 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.
#ifndef VERIBLE_VERILOG_CST_FUNCTIONS_H_
#define VERIBLE_VERILOG_CST_FUNCTIONS_H_
// See comment at the top
// verilog/CST/verilog_treebuilder_utils.h that explains use
// of std::forward in Make* helper functions.
#include <utility>
#include <vector>
#include "common/analysis/syntax_tree_search.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/symbol.h"
#include "common/text/tree_utils.h"
#include "verilog/CST/verilog_nonterminals.h"
#include "verilog/CST/verilog_treebuilder_utils.h"
namespace verilog {
// Construct a function header CST node, without the trailing ';'.
template <typename T0, typename T1, typename T2, typename T3, typename T4>
verible::SymbolPtr MakeFunctionHeader(T0&& qualifiers, T1&& function_start,
T2&& lifetime, T3&& return_type_id,
T4&& ports) {
verible::CheckOptionalSymbolAsNode(qualifiers, NodeEnum::kQualifierList);
ExpectString(function_start, "function");
verible::CheckOptionalSymbolAsNode(ports, NodeEnum::kParenGroup);
return verible::MakeTaggedNode(
NodeEnum::kFunctionHeader, std::forward<T0>(qualifiers),
std::forward<T1>(function_start), std::forward<T2>(lifetime),
std::forward<T3>(
return_type_id) /* flattens to separate type and id nodes */,
std::forward<T4>(ports));
}
// Construct a function header CST node, with the trailing ';'.
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5>
verible::SymbolPtr MakeFunctionHeader(T0&& qualifiers, T1&& function_start,
T2&& lifetime, T3&& return_type_id,
T4&& ports, T5&& semicolon) {
ExpectString(semicolon, ";");
return ExtendNode(
MakeFunctionHeader(
std::forward<T0>(qualifiers), std::forward<T1>(function_start),
std::forward<T2>(lifetime),
std::forward<T3>(
return_type_id) /* flattens to separate type and id nodes */,
std::forward<T4>(ports)),
std::forward<T5>(semicolon));
}
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9>
verible::SymbolPtr MakeFunctionDeclaration(T0&& qualifiers, T1&& function_start,
T2&& lifetime, T3&& return_type_id,
T4&& ports, T5&& semicolon,
T6&& function_items, T7&& body,
T8&& function_end, T9&& label) {
ExpectString(function_end, "endfunction");
return verible::MakeTaggedNode(
NodeEnum::kFunctionDeclaration,
MakeFunctionHeader(
std::forward<T0>(qualifiers), std::forward<T1>(function_start),
std::forward<T2>(lifetime),
std::forward<T3>(
return_type_id) /* flattens to separate type and id nodes */,
std::forward<T4>(ports), std::forward<T5>(semicolon)),
std::forward<T6>(function_items), std::forward<T7>(body),
std::forward<T8>(function_end), std::forward<T9>(label));
}
// Find all function declarations, including class method declarations.
std::vector<verible::TreeSearchMatch> FindAllFunctionDeclarations(
const verible::Symbol&);
// Find all function headers (in declarations and prototypes).
std::vector<verible::TreeSearchMatch> FindAllFunctionHeaders(
const verible::Symbol&);
// Find all function prototypes (extern, pure virtual).
std::vector<verible::TreeSearchMatch> FindAllFunctionPrototypes(
const verible::Symbol&);
// Find all function (or Task) calls.
std::vector<verible::TreeSearchMatch> FindAllFunctionOrTaskCalls(
const verible::Symbol&);
// Find all function (or Task) calls extension e.g class_name.function_call().
std::vector<verible::TreeSearchMatch> FindAllFunctionOrTaskCallsExtension(
const verible::Symbol&);
// Find all constructor prototypes.
std::vector<verible::TreeSearchMatch> FindAllConstructorPrototypes(
const verible::Symbol&);
// Returns the function declaration header (return type, id, ports)
const verible::SyntaxTreeNode* GetFunctionHeader(
const verible::Symbol& function_decl);
// Returns the function prototype header (return type, id, ports)
const verible::SyntaxTreeNode* GetFunctionPrototypeHeader(
const verible::Symbol& function_decl);
// FunctionHeader accessors
// Returns the function lifetime of the function header.
const verible::Symbol* GetFunctionHeaderLifetime(
const verible::Symbol& function_header);
// Returns the parenthesis group containing the formal ports list, or nullptr.
const verible::Symbol* GetFunctionHeaderFormalPortsGroup(
const verible::Symbol& function_header);
// Returns the return type of the function header.
const verible::Symbol* GetFunctionHeaderReturnType(
const verible::Symbol& function_header);
// Returns the id of the function header.
const verible::Symbol* GetFunctionHeaderId(
const verible::Symbol& function_header);
// FunctionDeclaration acccessors
// Returns the function lifetime of the node.
const verible::Symbol* GetFunctionLifetime(
const verible::Symbol& function_decl);
// Returns the parenthesis group containing the formal ports list, or nullptr.
const verible::Symbol* GetFunctionFormalPortsGroup(
const verible::Symbol& function_decl);
// Returns the return type of the function declaration.
const verible::Symbol* GetFunctionReturnType(
const verible::Symbol& function_decl);
// Returns the id of the function declaration.
const verible::Symbol* GetFunctionId(const verible::Symbol& function_decl);
// Returns leaf node for function name.
// e.g. function my_fun(); return leaf node for "my_fun".
const verible::SyntaxTreeLeaf* GetFunctionName(const verible::Symbol&);
// Returns local root node from node tagged with kFunctionCall.
const verible::SyntaxTreeNode* GetLocalRootFromFunctionCall(
const verible::Symbol&);
// Return the node spanning identifier for the function call node.
// e.g from "pkg::get()" returns the node spanning "pkg::get".
const verible::SyntaxTreeNode* GetIdentifiersFromFunctionCall(
const verible::Symbol& function_call);
// Returns leaf node for function name in function call.
// e.g my_function(); return leaf node for "my_function".
const verible::SyntaxTreeLeaf* GetFunctionCallName(const verible::Symbol&);
// Returns leaf node for function name in function call extension.
// e.g class_name.my_function(); return leaf node for "my_function".
const verible::SyntaxTreeLeaf* GetFunctionCallNameFromCallExtension(
const verible::Symbol&);
// Returns the function declaration body.
const verible::SyntaxTreeNode* GetFunctionBlockStatementList(
const verible::Symbol&);
// Return the node spanning the paren group of function call.
// e.g my_function(a, b, c) return the node spanning (a, b, c).
const verible::SyntaxTreeNode* GetParenGroupFromCall(const verible::Symbol&);
// Return the node spanning the paren group of function call extension.
// e.g my_class.my_function(a, b, c) return the node spanning (a, b, c).
const verible::SyntaxTreeNode* GetParenGroupFromCallExtension(
const verible::Symbol&);
// Returns leaf node for the "new" keyword of a constructor prototype.
const verible::SyntaxTreeLeaf* GetConstructorPrototypeNewKeyword(
const verible::Symbol&);
} // namespace verilog
#endif // VERIBLE_VERILOG_CST_FUNCTIONS_H_