microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
compiler/qsc/src/compile.rs
106lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use miette::{Diagnostic, Report}; |
| 5 | use qsc_data_structures::language_features::LanguageFeatures; |
| 6 | use qsc_frontend::{ |
| 7 | compile::{CompileUnit, PackageStore, RuntimeCapabilityFlags, SourceMap}, |
| 8 | error::WithSource, |
| 9 | }; |
| 10 | use qsc_hir::hir::PackageId; |
| 11 | use qsc_passes::{run_core_passes, run_default_passes, PackageType}; |
| 12 | use thiserror::Error; |
| 13 | |
| 14 | pub type Error = WithSource<ErrorKind>; |
| 15 | |
| 16 | #[derive(Clone, Debug, Diagnostic, Error)] |
| 17 | #[diagnostic(transparent)] |
| 18 | #[error(transparent)] |
| 19 | /// `ErrorKind` represents the different kinds of errors that can occur in the compiler. |
| 20 | /// Each variant of the enum corresponds to a different stage of the compilation process. |
| 21 | pub enum ErrorKind { |
| 22 | /// `Frontend` variant represents errors that occur during the frontend stage of the compiler. |
| 23 | /// These errors are typically related to syntax and semantic checks. |
| 24 | Frontend(#[from] qsc_frontend::compile::Error), |
| 25 | |
| 26 | /// `Pass` variant represents errors that occur during the `qsc_passes` stage of the compiler. |
| 27 | /// These errors are typically related to optimization, transformation, code generation, passes, |
| 28 | /// and static analysis passes. |
| 29 | Pass(#[from] qsc_passes::Error), |
| 30 | |
| 31 | /// `Lint` variant represents lints generated during the linting stage. These diagnostics are |
| 32 | /// typically emited from the language server and happens after all other compilation passes. |
| 33 | Lint(#[from] qsc_linter::Lint), |
| 34 | } |
| 35 | |
| 36 | #[must_use] |
| 37 | pub fn compile( |
| 38 | store: &PackageStore, |
| 39 | dependencies: &[PackageId], |
| 40 | sources: SourceMap, |
| 41 | package_type: PackageType, |
| 42 | capabilities: RuntimeCapabilityFlags, |
| 43 | language_features: LanguageFeatures, |
| 44 | ) -> (CompileUnit, Vec<Error>) { |
| 45 | let mut unit = qsc_frontend::compile::compile( |
| 46 | store, |
| 47 | dependencies, |
| 48 | sources, |
| 49 | capabilities, |
| 50 | language_features, |
| 51 | ); |
| 52 | let mut errors = Vec::new(); |
| 53 | for error in unit.errors.drain(..) { |
| 54 | errors.push(WithSource::from_map(&unit.sources, error.into())); |
| 55 | } |
| 56 | |
| 57 | if errors.is_empty() { |
| 58 | for error in run_default_passes(store.core(), &mut unit, package_type, capabilities) { |
| 59 | errors.push(WithSource::from_map(&unit.sources, error.into())); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | (unit, errors) |
| 64 | } |
| 65 | |
| 66 | /// Compiles the core library. |
| 67 | /// |
| 68 | /// # Panics |
| 69 | /// |
| 70 | /// Panics if the core library does not compile without errors. |
| 71 | #[must_use] |
| 72 | pub fn core() -> CompileUnit { |
| 73 | let mut unit = qsc_frontend::compile::core(); |
| 74 | let pass_errors = run_core_passes(&mut unit); |
| 75 | if pass_errors.is_empty() { |
| 76 | unit |
| 77 | } else { |
| 78 | for error in pass_errors { |
| 79 | let report = Report::new(WithSource::from_map(&unit.sources, error)); |
| 80 | eprintln!("{report:?}"); |
| 81 | } |
| 82 | |
| 83 | panic!("could not compile core library") |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Compiles the standard library. |
| 88 | /// |
| 89 | /// # Panics |
| 90 | /// |
| 91 | /// Panics if the standard library does not compile without errors. |
| 92 | #[must_use] |
| 93 | pub fn std(store: &PackageStore, capabilities: RuntimeCapabilityFlags) -> CompileUnit { |
| 94 | let mut unit = qsc_frontend::compile::std(store, capabilities); |
| 95 | let pass_errors = run_default_passes(store.core(), &mut unit, PackageType::Lib, capabilities); |
| 96 | if pass_errors.is_empty() { |
| 97 | unit |
| 98 | } else { |
| 99 | for error in pass_errors { |
| 100 | let report = Report::new(WithSource::from_map(&unit.sources, error)); |
| 101 | eprintln!("{report:?}"); |
| 102 | } |
| 103 | |
| 104 | panic!("could not compile standard library") |
| 105 | } |
| 106 | } |
| 107 | |