forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.cc
490 lines (425 loc) · 19.1 KB
/
statement.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// 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/statement.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/declaration.h"
#include "verilog/CST/identifier.h"
#include "verilog/CST/type.h"
#include "verilog/CST/verilog_matchers.h" // IWYU pragma: keep
namespace verilog {
using verible::Symbol;
using verible::SymbolCastToNode;
using verible::SyntaxTreeNode;
std::vector<verible::TreeSearchMatch> FindAllForLoopsInitializations(
const verible::Symbol& root) {
return SearchSyntaxTree(root, NodekForInitialization());
}
std::vector<verible::TreeSearchMatch> FindAllGenerateBlocks(
const verible::Symbol& root) {
return SearchSyntaxTree(root, NodekGenerateBlock());
}
static const SyntaxTreeNode* GetGenericStatementBody(
const SyntaxTreeNode* node) {
if (!node) return node;
// In most controlled constructs, the controlled statement body is
// in tail position. Exceptions include: DoWhile.
return &SymbolCastToNode(*node->children().back());
}
const SyntaxTreeNode* GetIfClauseGenerateBody(const Symbol& if_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(if_clause), NodeEnum::kGenerateIfClause));
if (!body_node) return nullptr;
return GetSubtreeAsNode(*body_node, NodeEnum::kGenerateIfBody, 0);
}
const SyntaxTreeNode* GetElseClauseGenerateBody(const Symbol& else_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(else_clause), NodeEnum::kGenerateElseClause));
if (!body_node) return nullptr;
return GetSubtreeAsNode(*body_node, NodeEnum::kGenerateElseBody, 0);
}
const SyntaxTreeNode* GetLoopGenerateBody(const Symbol& loop) {
return GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(loop), NodeEnum::kLoopGenerateConstruct));
}
const SyntaxTreeNode* GetConditionalGenerateIfClause(
const Symbol& conditional) {
return GetSubtreeAsNode(conditional, NodeEnum::kConditionalGenerateConstruct,
0, NodeEnum::kGenerateIfClause);
}
const SyntaxTreeNode* GetConditionalGenerateElseClause(
const Symbol& conditional) {
const auto* node = MatchNodeEnumOrNull(
SymbolCastToNode(conditional), NodeEnum::kConditionalGenerateConstruct);
if (!node || node->children().size() < 2) return nullptr;
const Symbol* else_ptr = node->children().back().get();
if (else_ptr == nullptr) return nullptr;
return MatchNodeEnumOrNull(SymbolCastToNode(*else_ptr),
NodeEnum::kGenerateElseClause);
}
const SyntaxTreeNode* GetIfClauseStatementBody(const Symbol& if_clause) {
const auto* body_node = GetGenericStatementBody(
MatchNodeEnumOrNull(SymbolCastToNode(if_clause), NodeEnum::kIfClause));
if (!body_node) return nullptr;
return GetSubtreeAsNode(*body_node, NodeEnum::kIfBody, 0);
}
const SyntaxTreeNode* GetElseClauseStatementBody(const Symbol& else_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(else_clause), NodeEnum::kElseClause));
if (!body_node) return nullptr;
return GetSubtreeAsNode(*body_node, NodeEnum::kElseBody, 0);
}
const SyntaxTreeNode* GetConditionalStatementIfClause(
const Symbol& conditional) {
return GetSubtreeAsNode(conditional, NodeEnum::kConditionalStatement, 0,
NodeEnum::kIfClause);
}
const SyntaxTreeNode* GetConditionalStatementElseClause(
const Symbol& conditional) {
const auto* node = MatchNodeEnumOrNull(SymbolCastToNode(conditional),
NodeEnum::kConditionalStatement);
if (!node || node->children().size() < 2) return nullptr;
const Symbol* else_ptr = node->children().back().get();
if (else_ptr == nullptr) return nullptr;
return MatchNodeEnumOrNull(SymbolCastToNode(*else_ptr),
NodeEnum::kElseClause);
}
const SyntaxTreeNode* GetAssertionStatementAssertClause(
const Symbol& assertion_statement) {
return GetSubtreeAsNode(assertion_statement, NodeEnum::kAssertionStatement, 0,
NodeEnum::kAssertionClause);
}
const SyntaxTreeNode* GetAssertionClauseStatementBody(
const Symbol& assertion_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(assertion_clause), NodeEnum::kAssertionClause));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kAssertionBody, 0));
}
const SyntaxTreeNode* GetAssertionStatementElseClause(
const Symbol& assertion_statement) {
const auto* node = MatchNodeEnumOrNull(SymbolCastToNode(assertion_statement),
NodeEnum::kAssertionStatement);
const Symbol* else_ptr = node->children().back().get();
if (else_ptr == nullptr) return nullptr;
return MatchNodeEnumOrNull(SymbolCastToNode(*else_ptr),
NodeEnum::kElseClause);
}
const SyntaxTreeNode* GetAssumeStatementAssumeClause(
const Symbol& assume_statement) {
return GetSubtreeAsNode(assume_statement, NodeEnum::kAssumeStatement, 0,
NodeEnum::kAssumeClause);
}
const SyntaxTreeNode* GetAssumeClauseStatementBody(
const Symbol& assume_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(assume_clause), NodeEnum::kAssumeClause));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kAssumeBody, 0));
}
const SyntaxTreeNode* GetAssumeStatementElseClause(
const Symbol& assume_statement) {
const auto* node = MatchNodeEnumOrNull(SymbolCastToNode(assume_statement),
NodeEnum::kAssumeStatement);
if (!node) return nullptr;
const Symbol* else_ptr = node->children().back().get();
if (else_ptr == nullptr) return nullptr;
return MatchNodeEnumOrNull(SymbolCastToNode(*else_ptr),
NodeEnum::kElseClause);
}
const SyntaxTreeNode* GetCoverStatementBody(const Symbol& cover_statement) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(cover_statement), NodeEnum::kCoverStatement));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kCoverBody, 0));
}
const SyntaxTreeNode* GetWaitStatementBody(const Symbol& wait_statement) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(wait_statement), NodeEnum::kWaitStatement));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kWaitBody, 0));
}
const SyntaxTreeNode* GetAssertPropertyStatementAssertClause(
const Symbol& assert_property_statement) {
return GetSubtreeAsNode(assert_property_statement,
NodeEnum::kAssertPropertyStatement, 0,
NodeEnum::kAssertPropertyClause);
}
const SyntaxTreeNode* GetAssertPropertyStatementBody(
const Symbol& assert_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(assert_clause), NodeEnum::kAssertPropertyClause));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kAssertPropertyBody, 0));
}
const SyntaxTreeNode* GetAssertPropertyStatementElseClause(
const Symbol& assert_property_statement) {
const auto* node =
MatchNodeEnumOrNull(SymbolCastToNode(assert_property_statement),
NodeEnum::kAssertPropertyStatement);
if (!node) return nullptr;
const Symbol* else_ptr = node->children().back().get();
if (else_ptr == nullptr) return nullptr;
return MatchNodeEnumOrNull(SymbolCastToNode(*else_ptr),
NodeEnum::kElseClause);
}
const SyntaxTreeNode* GetAssumePropertyStatementAssumeClause(
const Symbol& assume_property_statement) {
return GetSubtreeAsNode(assume_property_statement,
NodeEnum::kAssumePropertyStatement, 0,
NodeEnum::kAssumePropertyClause);
}
const SyntaxTreeNode* GetAssumePropertyStatementBody(
const Symbol& assume_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(assume_clause), NodeEnum::kAssumePropertyClause));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kAssumePropertyBody, 0));
}
const SyntaxTreeNode* GetAssumePropertyStatementElseClause(
const Symbol& assume_property_statement) {
const auto* node =
MatchNodeEnumOrNull(SymbolCastToNode(assume_property_statement),
NodeEnum::kAssumePropertyStatement);
if (!node) return nullptr;
const Symbol* else_ptr = node->children().back().get();
if (else_ptr == nullptr) return nullptr;
return MatchNodeEnumOrNull(SymbolCastToNode(*else_ptr),
NodeEnum::kElseClause);
}
const SyntaxTreeNode* GetExpectPropertyStatementExpectClause(
const Symbol& expect_property_statement) {
return GetSubtreeAsNode(expect_property_statement,
NodeEnum::kExpectPropertyStatement, 0,
NodeEnum::kExpectPropertyClause);
}
const SyntaxTreeNode* GetExpectPropertyStatementBody(
const Symbol& expect_clause) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(expect_clause), NodeEnum::kExpectPropertyClause));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kExpectPropertyBody, 0));
}
const SyntaxTreeNode* GetExpectPropertyStatementElseClause(
const Symbol& expect_property_statement) {
const auto* node =
MatchNodeEnumOrNull(SymbolCastToNode(expect_property_statement),
NodeEnum::kExpectPropertyStatement);
if (!node) return nullptr;
const Symbol* else_ptr = node->children().back().get();
if (else_ptr == nullptr) return nullptr;
return MatchNodeEnumOrNull(SymbolCastToNode(*else_ptr),
NodeEnum::kElseClause);
}
const SyntaxTreeNode* GetCoverPropertyStatementBody(
const Symbol& cover_property) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(cover_property), NodeEnum::kCoverPropertyStatement));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kCoverPropertyBody, 0));
}
const SyntaxTreeNode* GetCoverSequenceStatementBody(
const Symbol& cover_sequence) {
const auto* body_node = GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(cover_sequence), NodeEnum::kCoverSequenceStatement));
if (!body_node) return nullptr;
return verible::CheckOptionalSymbolAsNode(
GetSubtreeAsSymbol(*body_node, NodeEnum::kCoverSequenceBody, 0));
}
const SyntaxTreeNode* GetLoopStatementBody(const Symbol& loop) {
return GetGenericStatementBody(
MatchNodeEnumOrNull(SymbolCastToNode(loop), NodeEnum::kForLoopStatement));
}
const SyntaxTreeNode* GetDoWhileStatementBody(const Symbol& do_while) {
return GetSubtreeAsNode(SymbolCastToNode(do_while),
NodeEnum::kDoWhileLoopStatement, 1);
}
const SyntaxTreeNode* GetForeverStatementBody(const Symbol& forever) {
return GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(forever), NodeEnum::kForeverLoopStatement));
}
const SyntaxTreeNode* GetForeachStatementBody(const Symbol& foreach) {
return GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(foreach), NodeEnum::kForeachLoopStatement));
}
const SyntaxTreeNode* GetRepeatStatementBody(const Symbol& repeat) {
return GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(repeat), NodeEnum::kRepeatLoopStatement));
}
const SyntaxTreeNode* GetWhileStatementBody(const Symbol& while_stmt) {
return GetGenericStatementBody(MatchNodeEnumOrNull(
SymbolCastToNode(while_stmt), NodeEnum::kWhileLoopStatement));
}
const SyntaxTreeNode* GetProceduralTimingControlStatementBody(
const Symbol& proc_timing_control) {
return GetGenericStatementBody(
MatchNodeEnumOrNull(SymbolCastToNode(proc_timing_control),
NodeEnum::kProceduralTimingControlStatement));
}
const SyntaxTreeNode* GetAnyControlStatementBody(const Symbol& statement) {
switch (NodeEnum(SymbolCastToNode(statement).Tag().tag)) {
// generate
case NodeEnum::kGenerateIfClause:
return GetIfClauseGenerateBody(statement);
case NodeEnum::kGenerateElseClause:
return GetElseClauseGenerateBody(statement);
case NodeEnum::kLoopGenerateConstruct:
return GetLoopGenerateBody(statement);
// statements
case NodeEnum::kIfClause:
return GetIfClauseStatementBody(statement);
case NodeEnum::kElseClause:
return GetElseClauseStatementBody(statement);
case NodeEnum::kForLoopStatement:
return GetLoopStatementBody(statement);
case NodeEnum::kDoWhileLoopStatement:
return GetDoWhileStatementBody(statement);
case NodeEnum::kForeverLoopStatement:
return GetForeverStatementBody(statement);
case NodeEnum::kForeachLoopStatement:
return GetForeachStatementBody(statement);
case NodeEnum::kRepeatLoopStatement:
return GetRepeatStatementBody(statement);
case NodeEnum::kWhileLoopStatement:
return GetWhileStatementBody(statement);
case NodeEnum::kProceduralTimingControlStatement:
return GetProceduralTimingControlStatementBody(statement);
// immediate assertions
case NodeEnum::kAssertionClause:
return GetAssertionClauseStatementBody(statement);
case NodeEnum::kAssumeClause:
return GetAssumeClauseStatementBody(statement);
case NodeEnum::kCoverStatement:
return GetCoverStatementBody(statement);
case NodeEnum::kWaitStatement:
return GetWaitStatementBody(statement);
// concurrent assertions
case NodeEnum::kAssertPropertyClause:
return GetAssertPropertyStatementBody(statement);
case NodeEnum::kAssumePropertyClause:
return GetAssumePropertyStatementBody(statement);
case NodeEnum::kExpectPropertyClause:
return GetExpectPropertyStatementBody(statement);
case NodeEnum::kCoverPropertyStatement:
return GetCoverPropertyStatementBody(statement);
case NodeEnum::kCoverSequenceStatement:
return GetCoverSequenceStatementBody(statement);
default:
return nullptr;
}
}
const SyntaxTreeNode* GetAnyConditionalIfClause(const Symbol& conditional) {
// by IfClause, we main the first clause
switch (NodeEnum(SymbolCastToNode(conditional).Tag().tag)) {
// generate
case NodeEnum::kConditionalGenerateConstruct:
return GetConditionalGenerateIfClause(conditional);
// statement
case NodeEnum::kConditionalStatement:
return GetConditionalStatementIfClause(conditional);
// immediate assertions
case NodeEnum::kAssertionStatement:
return GetAssertionStatementAssertClause(conditional);
case NodeEnum::kAssumeStatement:
return GetAssumeStatementAssumeClause(conditional);
// concurrent assertions
case NodeEnum::kAssertPropertyStatement:
return GetAssertPropertyStatementAssertClause(conditional);
case NodeEnum::kAssumePropertyStatement:
return GetAssumePropertyStatementAssumeClause(conditional);
case NodeEnum::kExpectPropertyStatement:
return GetExpectPropertyStatementExpectClause(conditional);
default:
return nullptr;
}
}
const SyntaxTreeNode* GetAnyConditionalElseClause(const Symbol& conditional) {
switch (NodeEnum(SymbolCastToNode(conditional).Tag().tag)) {
// generate
case NodeEnum::kConditionalGenerateConstruct:
return GetConditionalGenerateElseClause(conditional);
// statement
case NodeEnum::kConditionalStatement:
return GetConditionalStatementElseClause(conditional);
// immediate assertions
case NodeEnum::kAssertionStatement:
return GetAssertionStatementElseClause(conditional);
case NodeEnum::kAssumeStatement:
return GetAssumeStatementElseClause(conditional);
// concurrent assertions
case NodeEnum::kAssertPropertyStatement:
return GetAssertPropertyStatementElseClause(conditional);
case NodeEnum::kAssumePropertyStatement:
return GetAssumePropertyStatementElseClause(conditional);
case NodeEnum::kExpectPropertyStatement:
return GetExpectPropertyStatementElseClause(conditional);
default:
return nullptr;
}
}
// Returns the data type node from for loop initialization.
const verible::SyntaxTreeNode* GetDataTypeFromForInitialization(
const verible::Symbol& for_initialization) {
const auto* data_type = verible::GetSubtreeAsSymbol(
for_initialization, NodeEnum::kForInitialization, 1);
if (data_type == nullptr) {
return nullptr;
}
return &verible::SymbolCastToNode(*data_type);
}
// Returns the variable name leaf from for loop initialization.
const verible::SyntaxTreeLeaf* GetVariableNameFromForInitialization(
const verible::Symbol& for_initialization) {
const Symbol* child = verible::GetSubtreeAsSymbol(
for_initialization, NodeEnum::kForInitialization, 2);
if (child->Kind() == verible::SymbolKind::kLeaf) {
return &SymbolCastToLeaf(*child);
}
const verible::SyntaxTreeNode* lpvalue =
verible::GetSubtreeAsNode(*child, NodeEnum::kLPValue, 0);
return AutoUnwrapIdentifier(*GetUnqualifiedIdFromReferenceCallBase(*lpvalue));
}
// Returns the rhs expression from for loop initialization.
const verible::SyntaxTreeNode* GetExpressionFromForInitialization(
const verible::Symbol& for_initialization) {
return verible::GetSubtreeAsNode(for_initialization,
NodeEnum::kForInitialization, 4,
NodeEnum::kExpression);
}
const verible::SyntaxTreeNode* GetGenerateBlockBegin(
const verible::Symbol& generate_block) {
return verible::GetSubtreeAsNode(generate_block, NodeEnum::kGenerateBlock, 0,
NodeEnum::kBegin);
}
const verible::SyntaxTreeNode* GetGenerateBlockEnd(
const verible::Symbol& generate_block) {
return verible::GetSubtreeAsNode(generate_block, NodeEnum::kGenerateBlock, 2,
NodeEnum::kEnd);
}
} // namespace verilog