microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
billt/revert-mimalloc

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_hir/src/validate.rs

67lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use crate::{
5 hir::{Block, CallableDecl, Expr, Ident, NodeId, Pat, QubitInit, SpecDecl, Stmt},
6 visit::{self, Visitor},
7};
8use qsc_data_structures::index_map::IndexMap;
9use std::fmt::Display;
10
11#[derive(Default)]
12pub struct Validator {
13 ids: IndexMap<NodeId, ()>,
14}
15
16impl Validator {
17 fn check(&mut self, id: NodeId, node: impl Display) {
18 if id.is_default() {
19 panic!("default node ID should be replaced: {node}")
20 } else if self.ids.contains_key(id) {
21 panic!("duplicate node ID: {node}");
22 } else {
23 self.ids.insert(id, ());
24 }
25 }
26}
27
28impl Visitor<'_> for Validator {
29 fn visit_callable_decl(&mut self, decl: &CallableDecl) {
30 self.check(decl.id, decl);
31 visit::walk_callable_decl(self, decl);
32 }
33
34 fn visit_spec_decl(&mut self, decl: &SpecDecl) {
35 self.check(decl.id, decl);
36 visit::walk_spec_decl(self, decl);
37 }
38
39 fn visit_block(&mut self, block: &Block) {
40 self.check(block.id, block);
41 visit::walk_block(self, block);
42 }
43
44 fn visit_stmt(&mut self, stmt: &Stmt) {
45 self.check(stmt.id, stmt);
46 visit::walk_stmt(self, stmt);
47 }
48
49 fn visit_expr(&mut self, expr: &Expr) {
50 self.check(expr.id, expr);
51 visit::walk_expr(self, expr);
52 }
53
54 fn visit_pat(&mut self, pat: &Pat) {
55 self.check(pat.id, pat);
56 visit::walk_pat(self, pat);
57 }
58
59 fn visit_qubit_init(&mut self, init: &QubitInit) {
60 self.check(init.id, init);
61 visit::walk_qubit_init(self, init);
62 }
63
64 fn visit_ident(&mut self, ident: &Ident) {
65 self.check(ident.id, ident);
66 }
67}
68