microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc/src/compile.rs
86lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use miette::{Diagnostic, Report}; |
| 5 | use qsc_frontend::{ |
| 6 | compile::{CompileUnit, PackageStore, RuntimeCapabilityFlags, SourceMap}, |
| 7 | error::WithSource, |
| 8 | }; |
| 9 | use qsc_hir::hir::PackageId; |
| 10 | use qsc_passes::{run_core_passes, run_default_passes, PackageType}; |
| 11 | use thiserror::Error; |
| 12 | |
| 13 | pub type Error = WithSource<ErrorKind>; |
| 14 | |
| 15 | #[derive(Clone, Debug, Diagnostic, Error)] |
| 16 | #[diagnostic(transparent)] |
| 17 | #[error(transparent)] |
| 18 | pub enum ErrorKind { |
| 19 | Frontend(#[from] qsc_frontend::compile::Error), |
| 20 | Pass(#[from] qsc_passes::Error), |
| 21 | } |
| 22 | |
| 23 | #[must_use] |
| 24 | pub fn compile( |
| 25 | store: &PackageStore, |
| 26 | dependencies: &[PackageId], |
| 27 | sources: SourceMap, |
| 28 | package_type: PackageType, |
| 29 | capabilities: RuntimeCapabilityFlags, |
| 30 | ) -> (CompileUnit, Vec<Error>) { |
| 31 | let mut unit = qsc_frontend::compile::compile(store, dependencies, sources, capabilities); |
| 32 | let mut errors = Vec::new(); |
| 33 | for error in unit.errors.drain(..) { |
| 34 | errors.push(WithSource::from_map(&unit.sources, error.into())); |
| 35 | } |
| 36 | |
| 37 | if errors.is_empty() { |
| 38 | for error in run_default_passes(store.core(), &mut unit, package_type, capabilities) { |
| 39 | errors.push(WithSource::from_map(&unit.sources, error.into())); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | (unit, errors) |
| 44 | } |
| 45 | |
| 46 | /// Compiles the core library. |
| 47 | /// |
| 48 | /// # Panics |
| 49 | /// |
| 50 | /// Panics if the core library does not compile without errors. |
| 51 | #[must_use] |
| 52 | pub fn core() -> CompileUnit { |
| 53 | let mut unit = qsc_frontend::compile::core(); |
| 54 | let pass_errors = run_core_passes(&mut unit); |
| 55 | if pass_errors.is_empty() { |
| 56 | unit |
| 57 | } else { |
| 58 | for error in pass_errors { |
| 59 | let report = Report::new(WithSource::from_map(&unit.sources, error)); |
| 60 | eprintln!("{report:?}"); |
| 61 | } |
| 62 | |
| 63 | panic!("could not compile core library") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// Compiles the standard library. |
| 68 | /// |
| 69 | /// # Panics |
| 70 | /// |
| 71 | /// Panics if the standard library does not compile without errors. |
| 72 | #[must_use] |
| 73 | pub fn std(store: &PackageStore, capabilities: RuntimeCapabilityFlags) -> CompileUnit { |
| 74 | let mut unit = qsc_frontend::compile::std(store, capabilities); |
| 75 | let pass_errors = run_default_passes(store.core(), &mut unit, PackageType::Lib, capabilities); |
| 76 | if pass_errors.is_empty() { |
| 77 | unit |
| 78 | } else { |
| 79 | for error in pass_errors { |
| 80 | let report = Report::new(WithSource::from_map(&unit.sources, error)); |
| 81 | eprintln!("{report:?}"); |
| 82 | } |
| 83 | |
| 84 | panic!("could not compile standard library") |
| 85 | } |
| 86 | } |
| 87 | |