microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/compiler/qsc_eval/src/debug.rs
75lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use qsc_data_structures::span::Span; |
| 5 | |
| 6 | use qsc_data_structures::functors::FunctorApp; |
| 7 | use qsc_fir::fir::{ExprId, PackageId, StoreItemId}; |
| 8 | |
| 9 | #[derive(Clone, Debug, PartialEq)] |
| 10 | pub struct Frame { |
| 11 | pub span: Span, |
| 12 | pub id: StoreItemId, |
| 13 | pub caller: PackageId, |
| 14 | pub functor: FunctorApp, |
| 15 | pub loop_iterations: Vec<LoopScope>, |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, Default, Clone, PartialEq)] |
| 19 | pub struct LoopScope { |
| 20 | pub loop_expr: ExprId, |
| 21 | pub iteration_count: usize, |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug, Default, Clone, PartialEq)] |
| 25 | pub struct CallStack { |
| 26 | frames: Vec<Frame>, |
| 27 | } |
| 28 | |
| 29 | impl CallStack { |
| 30 | #[must_use] |
| 31 | pub fn is_empty(&self) -> bool { |
| 32 | self.frames.is_empty() |
| 33 | } |
| 34 | |
| 35 | #[must_use] |
| 36 | pub fn len(&self) -> usize { |
| 37 | self.frames.len() |
| 38 | } |
| 39 | |
| 40 | #[must_use] |
| 41 | pub fn to_frames(&self) -> Vec<Frame> { |
| 42 | self.frames.clone() |
| 43 | } |
| 44 | |
| 45 | pub fn push_frame(&mut self, frame: Frame) { |
| 46 | self.frames.push(frame); |
| 47 | } |
| 48 | |
| 49 | pub fn pop_frame(&mut self) -> Option<Frame> { |
| 50 | self.frames.pop() |
| 51 | } |
| 52 | |
| 53 | pub fn push_loop_iteration(&mut self, loop_expr: ExprId) { |
| 54 | if let Some(frame) = self.frames.last_mut() { |
| 55 | frame.loop_iterations.push(LoopScope { |
| 56 | loop_expr, |
| 57 | iteration_count: 0, |
| 58 | }); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn pop_loop_iteration(&mut self) { |
| 63 | if let Some(frame) = self.frames.last_mut() { |
| 64 | frame.loop_iterations.pop(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | pub fn increment_loop_iteration(&mut self) { |
| 69 | if let Some(frame) = self.frames.last_mut() |
| 70 | && let Some(loop_scope) = frame.loop_iterations.last_mut() |
| 71 | { |
| 72 | loop_scope.iteration_count += 1; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |