microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc_fir/src/assigner.rs
74lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use crate::fir::{BlockId, ExprId, LocalVarId, NodeId, PatId, StmtId}; |
| 5 | |
| 6 | #[derive(Debug)] |
| 7 | pub struct Assigner { |
| 8 | next_node: NodeId, |
| 9 | next_block: BlockId, |
| 10 | next_expr: ExprId, |
| 11 | next_pat: PatId, |
| 12 | next_stmt: StmtId, |
| 13 | next_local: LocalVarId, |
| 14 | } |
| 15 | |
| 16 | impl Assigner { |
| 17 | #[must_use] |
| 18 | pub fn new() -> Self { |
| 19 | Self { |
| 20 | next_node: NodeId::FIRST, |
| 21 | next_block: BlockId::default(), |
| 22 | next_expr: ExprId::default(), |
| 23 | next_pat: PatId::default(), |
| 24 | next_stmt: StmtId::default(), |
| 25 | next_local: LocalVarId::default(), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | pub fn next_node(&mut self) -> NodeId { |
| 30 | let id = self.next_node; |
| 31 | self.next_node = id.successor(); |
| 32 | id |
| 33 | } |
| 34 | |
| 35 | pub fn next_block(&mut self) -> BlockId { |
| 36 | let id = self.next_block; |
| 37 | self.next_block = id.successor(); |
| 38 | id |
| 39 | } |
| 40 | |
| 41 | pub fn next_expr(&mut self) -> ExprId { |
| 42 | let id = self.next_expr; |
| 43 | self.next_expr = id.successor(); |
| 44 | id |
| 45 | } |
| 46 | |
| 47 | pub fn next_pat(&mut self) -> PatId { |
| 48 | let id = self.next_pat; |
| 49 | self.next_pat = id.successor(); |
| 50 | id |
| 51 | } |
| 52 | |
| 53 | pub fn next_stmt(&mut self) -> StmtId { |
| 54 | let id = self.next_stmt; |
| 55 | self.next_stmt = id.successor(); |
| 56 | id |
| 57 | } |
| 58 | |
| 59 | pub fn next_local(&mut self) -> LocalVarId { |
| 60 | let id = self.next_local; |
| 61 | self.next_local = id.successor(); |
| 62 | id |
| 63 | } |
| 64 | |
| 65 | pub fn reset_local(&mut self) { |
| 66 | self.next_local = LocalVarId::default(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl Default for Assigner { |
| 71 | fn default() -> Self { |
| 72 | Self::new() |
| 73 | } |
| 74 | } |
| 75 | |