microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.25.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/compiler/qsc_fir/src/visit.rs

226lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use crate::fir::{
5 Block, BlockId, CallableDecl, CallableImpl, Expr, ExprId, ExprKind, FieldAssign, Ident, Item,
6 ItemKind, Package, Pat, PatId, PatKind, SpecDecl, SpecImpl, Stmt, StmtId, StmtKind,
7 StringComponent,
8};
9
10pub trait Visitor<'a>: Sized {
11 fn visit_package(&mut self, package: &'a Package, _: &crate::fir::PackageStore) {
12 walk_package(self, package);
13 }
14
15 fn visit_item(&mut self, item: &'a Item) {
16 walk_item(self, item);
17 }
18
19 fn visit_callable_decl(&mut self, decl: &'a CallableDecl) {
20 walk_callable_decl(self, decl);
21 }
22
23 fn visit_callable_impl(&mut self, callable_impl: &'a CallableImpl) {
24 walk_callable_impl(self, callable_impl);
25 }
26
27 fn visit_spec_impl(&mut self, spec_impl: &'a SpecImpl) {
28 walk_spec_impl(self, spec_impl);
29 }
30
31 fn visit_spec_decl(&mut self, decl: &'a SpecDecl) {
32 walk_spec_decl(self, decl);
33 }
34
35 fn visit_block(&mut self, block: BlockId) {
36 walk_block(self, block);
37 }
38
39 fn visit_stmt(&mut self, stmt: StmtId) {
40 walk_stmt(self, stmt);
41 }
42
43 fn visit_expr(&mut self, expr: ExprId) {
44 walk_expr(self, expr);
45 }
46
47 fn visit_pat(&mut self, pat: PatId) {
48 walk_pat(self, pat);
49 }
50
51 fn visit_ident(&mut self, _: &'a Ident) {}
52
53 fn get_block(&self, id: BlockId) -> &'a Block;
54 fn get_expr(&self, id: ExprId) -> &'a Expr;
55 fn get_pat(&self, id: PatId) -> &'a Pat;
56 fn get_stmt(&self, id: StmtId) -> &'a Stmt;
57}
58
59pub fn walk_package<'a>(vis: &mut impl Visitor<'a>, package: &'a Package) {
60 package.items.values().for_each(|i| vis.visit_item(i));
61 package.entry.iter().for_each(|e| vis.visit_expr(*e));
62}
63
64pub fn walk_item<'a>(vis: &mut impl Visitor<'a>, item: &'a Item) {
65 match &item.kind {
66 ItemKind::Callable(decl) => vis.visit_callable_decl(decl),
67 ItemKind::Namespace(name, _) | ItemKind::Ty(name, _) => vis.visit_ident(name),
68 ItemKind::Export(name, _) => {
69 vis.visit_ident(name);
70 }
71 }
72}
73
74pub fn walk_callable_decl<'a>(vis: &mut impl Visitor<'a>, decl: &'a CallableDecl) {
75 vis.visit_ident(&decl.name);
76 vis.visit_pat(decl.input);
77 vis.visit_callable_impl(&decl.implementation);
78}
79
80pub fn walk_callable_impl<'a>(vis: &mut impl Visitor<'a>, callable_impl: &'a CallableImpl) {
81 match callable_impl {
82 CallableImpl::Intrinsic => {}
83 CallableImpl::Spec(spec_impl) => {
84 vis.visit_spec_impl(spec_impl);
85 }
86 CallableImpl::SimulatableIntrinsic(spec_decl) => {
87 vis.visit_spec_decl(spec_decl);
88 }
89 }
90}
91
92pub fn walk_spec_impl<'a>(vis: &mut impl Visitor<'a>, spec_impl: &'a SpecImpl) {
93 vis.visit_spec_decl(&spec_impl.body);
94 spec_impl
95 .adj
96 .iter()
97 .for_each(|spec| vis.visit_spec_decl(spec));
98 spec_impl
99 .ctl
100 .iter()
101 .for_each(|spec| vis.visit_spec_decl(spec));
102 spec_impl
103 .ctl_adj
104 .iter()
105 .for_each(|spec| vis.visit_spec_decl(spec));
106}
107
108pub fn walk_spec_decl<'a>(vis: &mut impl Visitor<'a>, decl: &'a SpecDecl) {
109 decl.input.iter().for_each(|pat| vis.visit_pat(*pat));
110 vis.visit_block(decl.block);
111}
112
113pub fn walk_block<'a>(vis: &mut impl Visitor<'a>, block: BlockId) {
114 let block = vis.get_block(block);
115 block.stmts.iter().for_each(|s| vis.visit_stmt(*s));
116}
117
118pub fn walk_stmt<'a>(vis: &mut impl Visitor<'a>, id: StmtId) {
119 let stmt = vis.get_stmt(id);
120 match &stmt.kind {
121 StmtKind::Item(_) => {}
122 StmtKind::Expr(expr) | StmtKind::Semi(expr) => vis.visit_expr(*expr),
123 StmtKind::Local(_, pat, value) => {
124 vis.visit_pat(*pat);
125 vis.visit_expr(*value);
126 }
127 }
128}
129
130pub fn walk_expr<'a>(vis: &mut impl Visitor<'a>, expr: ExprId) {
131 let expr = vis.get_expr(expr);
132 match &expr.kind {
133 ExprKind::Array(exprs) | ExprKind::ArrayLit(exprs) => {
134 for e in exprs {
135 vis.visit_expr(*e);
136 }
137 }
138 ExprKind::ArrayRepeat(item, size) => {
139 vis.visit_expr(*item);
140 vis.visit_expr(*size);
141 }
142 ExprKind::Assign(lhs, rhs)
143 | ExprKind::AssignOp(_, lhs, rhs)
144 | ExprKind::BinOp(_, lhs, rhs) => {
145 vis.visit_expr(*lhs);
146 vis.visit_expr(*rhs);
147 }
148 ExprKind::AssignField(record, _, replace) | ExprKind::UpdateField(record, _, replace) => {
149 vis.visit_expr(*record);
150 vis.visit_expr(*replace);
151 }
152 ExprKind::AssignIndex(array, index, replace) => {
153 vis.visit_expr(*array);
154 vis.visit_expr(*index);
155 vis.visit_expr(*replace);
156 }
157 ExprKind::Block(block) => vis.visit_block(*block),
158 ExprKind::Call(callee, arg) => {
159 vis.visit_expr(*callee);
160 vis.visit_expr(*arg);
161 }
162 ExprKind::Fail(msg) => vis.visit_expr(*msg),
163 ExprKind::Field(record, _) => vis.visit_expr(*record),
164 ExprKind::If(cond, body, otherwise) => {
165 vis.visit_expr(*cond);
166 vis.visit_expr(*body);
167 if let Some(e) = otherwise {
168 vis.visit_expr(*e);
169 }
170 }
171 ExprKind::Index(array, index) => {
172 vis.visit_expr(*array);
173 vis.visit_expr(*index);
174 }
175 ExprKind::Return(expr) | ExprKind::UnOp(_, expr) => {
176 vis.visit_expr(*expr);
177 }
178 ExprKind::Range(start, step, end) => {
179 if let Some(s) = start {
180 vis.visit_expr(*s);
181 }
182 if let Some(s) = step {
183 vis.visit_expr(*s);
184 }
185 if let Some(e) = end {
186 vis.visit_expr(*e);
187 }
188 }
189 ExprKind::Struct(_, copy, fields) => {
190 if let Some(c) = copy {
191 vis.visit_expr(*c);
192 }
193 for FieldAssign { value, .. } in fields {
194 vis.visit_expr(*value);
195 }
196 }
197 ExprKind::String(components) => {
198 for component in components {
199 match component {
200 StringComponent::Expr(expr) => vis.visit_expr(*expr),
201 StringComponent::Lit(_) => {}
202 }
203 }
204 }
205 ExprKind::UpdateIndex(e1, e2, e3) => {
206 vis.visit_expr(*e1);
207 vis.visit_expr(*e2);
208 vis.visit_expr(*e3);
209 }
210 ExprKind::Tuple(exprs) => exprs.iter().for_each(|e| vis.visit_expr(*e)),
211 ExprKind::While(cond, block) => {
212 vis.visit_expr(*cond);
213 vis.visit_block(*block);
214 }
215 ExprKind::Closure(_, _) | ExprKind::Hole | ExprKind::Lit(_) | ExprKind::Var(_, _) => {}
216 }
217}
218
219pub fn walk_pat<'a>(vis: &mut impl Visitor<'a>, pat: PatId) {
220 let pat = vis.get_pat(pat);
221 match &pat.kind {
222 PatKind::Bind(name) => vis.visit_ident(name),
223 PatKind::Discard => {}
224 PatKind::Tuple(pats) => pats.iter().for_each(|p| vis.visit_pat(*p)),
225 }
226}
227