microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billti/bloch

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_fir/src/visit.rs

214lines · 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 exprs.iter().for_each(|e| vis.visit_expr(*e));
135 }
136 ExprKind::ArrayRepeat(item, size) => {
137 vis.visit_expr(*item);
138 vis.visit_expr(*size);
139 }
140 ExprKind::Assign(lhs, rhs)
141 | ExprKind::AssignOp(_, lhs, rhs)
142 | ExprKind::BinOp(_, lhs, rhs) => {
143 vis.visit_expr(*lhs);
144 vis.visit_expr(*rhs);
145 }
146 ExprKind::AssignField(record, _, replace) | ExprKind::UpdateField(record, _, replace) => {
147 vis.visit_expr(*record);
148 vis.visit_expr(*replace);
149 }
150 ExprKind::AssignIndex(array, index, replace) => {
151 vis.visit_expr(*array);
152 vis.visit_expr(*index);
153 vis.visit_expr(*replace);
154 }
155 ExprKind::Block(block) => vis.visit_block(*block),
156 ExprKind::Call(callee, arg) => {
157 vis.visit_expr(*callee);
158 vis.visit_expr(*arg);
159 }
160 ExprKind::Fail(msg) => vis.visit_expr(*msg),
161 ExprKind::Field(record, _) => vis.visit_expr(*record),
162 ExprKind::If(cond, body, otherwise) => {
163 vis.visit_expr(*cond);
164 vis.visit_expr(*body);
165 otherwise.iter().for_each(|e| vis.visit_expr(*e));
166 }
167 ExprKind::Index(array, index) => {
168 vis.visit_expr(*array);
169 vis.visit_expr(*index);
170 }
171 ExprKind::Return(expr) | ExprKind::UnOp(_, expr) => {
172 vis.visit_expr(*expr);
173 }
174 ExprKind::Range(start, step, end) => {
175 start.iter().for_each(|s| vis.visit_expr(*s));
176 step.iter().for_each(|s| vis.visit_expr(*s));
177 end.iter().for_each(|e| vis.visit_expr(*e));
178 }
179 ExprKind::Struct(_, copy, fields) => {
180 copy.iter().for_each(|c| vis.visit_expr(*c));
181 fields
182 .iter()
183 .for_each(|FieldAssign { value, .. }| vis.visit_expr(*value));
184 }
185 ExprKind::String(components) => {
186 for component in components {
187 match component {
188 StringComponent::Expr(expr) => vis.visit_expr(*expr),
189 StringComponent::Lit(_) => {}
190 }
191 }
192 }
193 ExprKind::UpdateIndex(e1, e2, e3) => {
194 vis.visit_expr(*e1);
195 vis.visit_expr(*e2);
196 vis.visit_expr(*e3);
197 }
198 ExprKind::Tuple(exprs) => exprs.iter().for_each(|e| vis.visit_expr(*e)),
199 ExprKind::While(cond, block) => {
200 vis.visit_expr(*cond);
201 vis.visit_block(*block);
202 }
203 ExprKind::Closure(_, _) | ExprKind::Hole | ExprKind::Lit(_) | ExprKind::Var(_, _) => {}
204 }
205}
206
207pub fn walk_pat<'a>(vis: &mut impl Visitor<'a>, pat: PatId) {
208 let pat = vis.get_pat(pat);
209 match &pat.kind {
210 PatKind::Bind(name) => vis.visit_ident(name),
211 PatKind::Discard => {}
212 PatKind::Tuple(pats) => pats.iter().for_each(|p| vis.visit_pat(*p)),
213 }
214}
215