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
use super::ast;
use super::ast::{FillMeta, Statement};
use super::file_definition::FileID;
use crate::file_definition::FileLocation;
use std::collections::{HashMap, HashSet, BTreeMap};
pub type TagInfo = HashSet<String>;
pub type TemplateInfo = HashMap<String, TemplateData>;
type SignalInfo = BTreeMap<String, (usize, TagInfo)>;
type SignalDeclarationOrder = Vec<(String, usize)>;
#[derive(Clone)]
pub struct TemplateData {
file_id: FileID,
name: String,
body: Statement,
num_of_params: usize,
name_of_params: Vec<String>,
param_location: FileLocation,
input_signals: SignalInfo,
output_signals: SignalInfo,
is_parallel: bool,
is_custom_gate: bool,
input_declarations: SignalDeclarationOrder,
output_declarations: SignalDeclarationOrder,
}
impl TemplateData {
#[allow(clippy::too_many_arguments)]
pub fn new(
name: String,
file_id: FileID,
mut body: Statement,
num_of_params: usize,
name_of_params: Vec<String>,
param_location: FileLocation,
elem_id: &mut usize,
is_parallel: bool,
is_custom_gate: bool,
) -> TemplateData {
body.fill(file_id, elem_id);
let mut input_signals = SignalInfo::new();
let mut output_signals = SignalInfo::new();
let mut input_declarations = SignalDeclarationOrder::new();
let mut output_declarations = SignalDeclarationOrder::new();
fill_inputs_and_outputs(
&body,
&mut input_signals,
&mut output_signals,
&mut input_declarations,
&mut output_declarations,
);
TemplateData {
name,
file_id,
body,
num_of_params,
name_of_params,
param_location,
input_signals,
output_signals,
is_parallel,
is_custom_gate,
input_declarations,
output_declarations,
}
}
pub fn get_file_id(&self) -> FileID {
self.file_id
}
pub fn get_body(&self) -> &Statement {
&self.body
}
pub fn get_body_as_vec(&self) -> &Vec<Statement> {
match &self.body {
Statement::Block { stmts, .. } => stmts,
_ => panic!("Function body should be a block"),
}
}
pub fn get_mut_body(&mut self) -> &mut Statement {
&mut self.body
}
pub fn get_mut_body_as_vec(&mut self) -> &mut Vec<Statement> {
match &mut self.body {
Statement::Block { stmts, .. } => stmts,
_ => panic!("Function body should be a block"),
}
}
pub fn get_num_of_params(&self) -> usize {
self.num_of_params
}
pub fn get_param_location(&self) -> FileLocation {
self.param_location.clone()
}
pub fn get_name_of_params(&self) -> &Vec<String> {
&self.name_of_params
}
pub fn get_input_info(&self, name: &str) -> Option<&(usize, TagInfo)> {
self.input_signals.get(name)
}
pub fn get_output_info(&self, name: &str) -> Option<&(usize, TagInfo)> {
self.output_signals.get(name)
}
pub fn get_inputs(&self) -> &SignalInfo {
&self.input_signals
}
pub fn get_outputs(&self) -> &SignalInfo {
&self.output_signals
}
pub fn get_declaration_inputs(&self) -> &SignalDeclarationOrder {
&self.input_declarations
}
pub fn get_declaration_outputs(&self) -> &SignalDeclarationOrder {
&self.output_declarations
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn is_parallel(&self) -> bool {
self.is_parallel
}
pub fn is_custom_gate(&self) -> bool {
self.is_custom_gate
}
}
fn fill_inputs_and_outputs(
template_statement: &Statement,
input_signals: &mut SignalInfo,
output_signals: &mut SignalInfo,
input_declarations: &mut SignalDeclarationOrder,
output_declarations: &mut SignalDeclarationOrder,
) {
match template_statement {
Statement::IfThenElse { if_case, else_case, .. } => {
fill_inputs_and_outputs(
if_case,
input_signals,
output_signals,
input_declarations,
output_declarations,
);
if let Option::Some(else_value) = else_case {
fill_inputs_and_outputs(
else_value,
input_signals,
output_signals,
input_declarations,
output_declarations,
);
}
}
Statement::Block { stmts, .. } => {
for stmt in stmts.iter() {
fill_inputs_and_outputs(
stmt,
input_signals,
output_signals,
input_declarations,
output_declarations,
);
}
}
Statement::While { stmt, .. } => {
fill_inputs_and_outputs(
stmt,
input_signals,
output_signals,
input_declarations,
output_declarations,
);
}
Statement::InitializationBlock { initializations, .. } => {
for initialization in initializations.iter() {
fill_inputs_and_outputs(
initialization,
input_signals,
output_signals,
input_declarations,
output_declarations,
);
}
}
Statement::Declaration {
xtype: ast::VariableType::Signal(stype, tag_list),
name,
dimensions,
..
} => {
let signal_name = name.clone();
let dimensions = dimensions.len();
let mut tag_info = HashSet::new();
for tag in tag_list {
tag_info.insert(tag.clone());
}
match stype {
ast::SignalType::Input => {
input_signals.insert(signal_name.clone(), (dimensions, tag_info));
input_declarations.push((signal_name, dimensions));
}
ast::SignalType::Output => {
output_signals.insert(signal_name.clone(), (dimensions, tag_info));
output_declarations.push((signal_name, dimensions));
}
_ => {} }
}
_ => {}
}
}