microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/compiler/qsc/src/interpret/debug.rs
112lines · modeblame
e178f8ddIan Davis3 years ago | 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. | |
| 3 | | |
| 4 | #[cfg(test)] | |
| 5 | mod tests; | |
| 6 | | |
0d39fa9dorpuente-MS1 years ago | 7 | use qsc_data_structures::line_column::{Encoding, Position}; |
18718319Ian Davis2 years ago | 8 | use qsc_eval::debug::Frame; |
7db6f1abCésar Zaragoza Cortés2 years ago | 9 | use qsc_fir::fir::{Global, PackageStoreLookup, StoreItemId}; |
e178f8ddIan Davis3 years ago | 10 | use qsc_frontend::compile::PackageStore; |
2b583ddaIan Davis3 years ago | 11 | use qsc_hir::hir; |
e178f8ddIan Davis3 years ago | 12 | use qsc_hir::hir::{Item, ItemKind}; |
18718319Ian Davis2 years ago | 13 | use qsc_lowerer::map_fir_package_to_hir; |
1fcdcc84Stefan J. Wernli1 years ago | 14 | use std::fmt::Write; |
82fc78d1Alex Hansen2 years ago | 15 | use std::rc::Rc; |
e178f8ddIan Davis3 years ago | 16 | |
| 17 | #[must_use] | |
2b583ddaIan Davis3 years ago | 18 | pub(crate) fn format_call_stack( |
e178f8ddIan Davis3 years ago | 19 | store: &PackageStore, |
7db6f1abCésar Zaragoza Cortés2 years ago | 20 | globals: &impl PackageStoreLookup, |
2b583ddaIan Davis3 years ago | 21 | frames: Vec<Frame>, |
e178f8ddIan Davis3 years ago | 22 | error: &dyn std::error::Error, |
| 23 | ) -> String { | |
| 24 | let mut trace = String::new(); | |
1fcdcc84Stefan J. Wernli1 years ago | 25 | writeln!(trace, "Error: {error}").expect("writing to string should succeed"); |
e178f8ddIan Davis3 years ago | 26 | trace.push_str("Call stack:\n"); |
| 27 | | |
2b583ddaIan Davis3 years ago | 28 | let mut frames = frames; |
e178f8ddIan Davis3 years ago | 29 | frames.reverse(); |
| 30 | | |
| 31 | for frame in frames { | |
7db6f1abCésar Zaragoza Cortés2 years ago | 32 | let Some(Global::Callable(call)) = globals.get_global(frame.id) else { |
9cf3b306Stefan J. Wernli2 years ago | 33 | panic!("missing global"); |
| 34 | }; | |
e178f8ddIan Davis3 years ago | 35 | |
| 36 | trace.push_str(" at "); | |
| 37 | if frame.functor.adjoint { | |
| 38 | trace.push_str("Adjoint "); | |
| 39 | } | |
| 40 | if frame.functor.controlled > 0 { | |
1fcdcc84Stefan J. Wernli1 years ago | 41 | write!(trace, "Controlled({}) ", frame.functor.controlled) |
| 42 | .expect("writing to string should succeed"); | |
e178f8ddIan Davis3 years ago | 43 | } |
8a033a27orpuente-MS9 months ago | 44 | if let Some(item) = get_item_parent(store, frame.id) |
| 45 | && let Some(ns) = get_ns_name(&item) | |
| 46 | { | |
| 47 | write!(trace, "{ns}.").expect("writing to string should succeed"); | |
e178f8ddIan Davis3 years ago | 48 | } |
1fcdcc84Stefan J. Wernli1 years ago | 49 | write!(trace, "{}", call.name.name).expect("writing to string should succeed"); |
e178f8ddIan Davis3 years ago | 50 | |
| 51 | let name = get_item_file_name(store, frame.id); | |
65c9d967Mine Starks7 months ago | 52 | let pos = get_position(&frame, store); |
0d39fa9dorpuente-MS1 years ago | 53 | write!( |
| 54 | trace, | |
| 55 | " in {}:{}:{}", | |
| 56 | name.unwrap_or("<expression>".to_string()), | |
a3590c80Copilot1 years ago | 57 | pos.line + 1, |
| 58 | pos.column + 1, | |
0d39fa9dorpuente-MS1 years ago | 59 | ) |
| 60 | .expect("writing to string should succeed"); | |
e178f8ddIan Davis3 years ago | 61 | |
| 62 | trace.push('\n'); | |
| 63 | } | |
| 64 | trace | |
| 65 | } | |
| 66 | | |
| 67 | #[must_use] | |
7db6f1abCésar Zaragoza Cortés2 years ago | 68 | fn get_item_parent(store: &PackageStore, id: StoreItemId) -> Option<Item> { |
2b583ddaIan Davis3 years ago | 69 | let package = map_fir_package_to_hir(id.package); |
| 70 | let item = hir::LocalItemId::from(usize::from(id.item)); | |
| 71 | store.get(package).and_then(|unit| { | |
| 72 | let item = unit.package.items.get(item)?; | |
e178f8ddIan Davis3 years ago | 73 | if let Some(parent) = item.parent { |
| 74 | let parent = unit.package.items.get(parent)?; | |
| 75 | Some(parent.clone()) | |
| 76 | } else { | |
| 77 | None | |
| 78 | } | |
| 79 | }) | |
| 80 | } | |
| 81 | | |
| 82 | #[must_use] | |
7db6f1abCésar Zaragoza Cortés2 years ago | 83 | fn get_item_file_name(store: &PackageStore, id: StoreItemId) -> Option<String> { |
2b583ddaIan Davis3 years ago | 84 | let package = map_fir_package_to_hir(id.package); |
| 85 | let item = hir::LocalItemId::from(usize::from(id.item)); | |
| 86 | store.get(package).and_then(|unit| { | |
| 87 | let item = unit.package.items.get(item)?; | |
91afda4eMine Starks3 years ago | 88 | let source = unit.sources.find_by_offset(item.span.lo); |
| 89 | source.map(|s| s.name.to_string()) | |
e178f8ddIan Davis3 years ago | 90 | }) |
| 91 | } | |
| 92 | | |
| 93 | #[must_use] | |
82fc78d1Alex Hansen2 years ago | 94 | fn get_ns_name(item: &Item) -> Option<Rc<str>> { |
| 95 | let ItemKind::Namespace(ns, _) = &item.kind else { | |
| 96 | return None; | |
| 97 | }; | |
| 98 | Some(ns.name()) | |
e178f8ddIan Davis3 years ago | 99 | } |
0d39fa9dorpuente-MS1 years ago | 100 | |
| 101 | /// Converts the [`Span`] of [`Frame`] into a [`Position`]. | |
65c9d967Mine Starks7 months ago | 102 | fn get_position(frame: &Frame, store: &PackageStore) -> Position { |
0d39fa9dorpuente-MS1 years ago | 103 | let filename = get_item_file_name(store, frame.id).expect("file should exist"); |
| 104 | let package_id = map_fir_package_to_hir(frame.id.package); | |
| 105 | let unit = store.get(package_id).expect("package should exist"); | |
| 106 | let source = unit | |
| 107 | .sources | |
| 108 | .find_by_name(&filename) | |
| 109 | .expect("source should exist"); | |
| 110 | let contents = &source.contents; | |
b5de5837orpuente-MS1 years ago | 111 | Position::from_utf8_byte_offset(Encoding::Utf8, contents, frame.span.lo - source.offset) |
0d39fa9dorpuente-MS1 years ago | 112 | } |