microsoft/qdk

Public

mirrored from https://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/assigner.rs

92lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use crate::{
5 hir::{Block, CallableDecl, Expr, Ident, LocalItemId, NodeId, Pat, QubitInit, SpecDecl, Stmt},
6 mut_visit::{self, MutVisitor},
7};
8
9/// The [Assigner] tracks the current state of IDs being handed out within a pass of the resolver.
10/// It is used when visiting a package to assign IDs to all elements. Identifiers are resolved and
11/// replaced with canonical IDs in this process. The AST gets all IDs resolved after the symbol resolution
12/// run.
13#[derive(Debug)]
14pub struct Assigner {
15 next_node: NodeId,
16 next_item: LocalItemId,
17}
18
19impl Assigner {
20 #[must_use]
21 pub fn new() -> Self {
22 Self {
23 next_node: NodeId::FIRST,
24 next_item: LocalItemId::default(),
25 }
26 }
27
28 pub fn next_node(&mut self) -> NodeId {
29 let id = self.next_node;
30 self.next_node = id.successor();
31 id
32 }
33
34 pub fn next_item(&mut self) -> LocalItemId {
35 let id = self.next_item;
36 self.next_item = id.successor();
37 id
38 }
39
40 fn assign(&mut self, id: &mut NodeId) {
41 if id.is_default() {
42 *id = self.next_node();
43 }
44 }
45}
46
47impl Default for Assigner {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53impl MutVisitor for Assigner {
54 fn visit_callable_decl(&mut self, decl: &mut CallableDecl) {
55 self.assign(&mut decl.id);
56 mut_visit::walk_callable_decl(self, decl);
57 }
58
59 fn visit_spec_decl(&mut self, decl: &mut SpecDecl) {
60 self.assign(&mut decl.id);
61 mut_visit::walk_spec_decl(self, decl);
62 }
63
64 fn visit_block(&mut self, block: &mut Block) {
65 self.assign(&mut block.id);
66 mut_visit::walk_block(self, block);
67 }
68
69 fn visit_stmt(&mut self, stmt: &mut Stmt) {
70 self.assign(&mut stmt.id);
71 mut_visit::walk_stmt(self, stmt);
72 }
73
74 fn visit_expr(&mut self, expr: &mut Expr) {
75 self.assign(&mut expr.id);
76 mut_visit::walk_expr(self, expr);
77 }
78
79 fn visit_pat(&mut self, pat: &mut Pat) {
80 self.assign(&mut pat.id);
81 mut_visit::walk_pat(self, pat);
82 }
83
84 fn visit_qubit_init(&mut self, init: &mut QubitInit) {
85 self.assign(&mut init.id);
86 mut_visit::walk_qubit_init(self, init);
87 }
88
89 fn visit_ident(&mut self, ident: &mut Ident) {
90 self.assign(&mut ident.id);
91 }
92}