From daeaade53ce935f58afec450a3aed1acbd38e17e Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 13 Dec 2022 13:46:31 -0500 Subject: [PATCH] tamer: tamec: Expose ASG context in lowering pipeline The previous commit had the ASG implicitly constructed and then discarded. This will keep it around, which will be necessary not only for imports, but for passing the ASG off to the next phases of lowering. DEV-13429 --- tamer/src/bin/tamec.rs | 68 +++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/tamer/src/bin/tamec.rs b/tamer/src/bin/tamec.rs index 4dfefb69..7d3ea630 100644 --- a/tamer/src/bin/tamec.rs +++ b/tamer/src/bin/tamec.rs @@ -36,7 +36,7 @@ use std::{ use tamer::{ asg::{ air::{AirAggregate, AirToken as Air}, - AsgError, + AsgError, DefaultAsg, }, diagnose::{ AnnotatedSpan, Diagnostic, FsSpanResolver, Reporter, VisualReporter, @@ -46,7 +46,8 @@ use tamer::{ XirfToNirError, }, parse::{ - Lower, ParseError, Parsed, ParsedObject, ParsedResult, UnknownToken, + FinalizeError, Lower, ParseError, Parsed, ParsedObject, ParsedResult, + UnknownToken, }, xir::{ self, @@ -137,6 +138,10 @@ fn compile( .inspect(copy_xml_to(fout, &escaper)) .map(|result| result.map_err(RecoverableError::from)); + // TODO: Determine a good default capacity once we have this populated + // and can come up with some heuristics. + let asg = DefaultAsg::with_capacity(1024, 2048); + let _ = Lower::< ParsedObject, XirToXirf<64, RefinedText>, @@ -145,15 +150,19 @@ fn compile( Lower::, XirfToNir, _>::lower(toks, |nir| { Lower::::lower(nir, |nir| { Lower::::lower(nir, |air| { - Lower::::lower(air, |end| { - end.fold(Ok(()), |x, result| match result { - Ok(_) => x, - Err(e) => { - report_err(&e, reporter, &mut ebuf)?; - x - } - }) - }) + Lower::::lower_with_context( + air, + asg, + |end| { + end.fold(Ok(()), |x, result| match result { + Ok(_) => x, + Err(e) => { + report_err(&e, reporter, &mut ebuf)?; + x + } + }) + }, + ) }) }) }) @@ -274,6 +283,7 @@ pub enum UnrecoverableError { Fmt(fmt::Error), XirWriterError(xir::writer::Error), ErrorsDuringLowering(ErrorCount), + FinalizeError(FinalizeError), } /// Number of errors that occurred during this compilation unit. @@ -331,6 +341,12 @@ impl From for UnrecoverableError { } } +impl From for UnrecoverableError { + fn from(e: FinalizeError) -> Self { + Self::FinalizeError(e) + } +} + impl From> for RecoverableError { fn from(e: ParseError) -> Self { Self::XirParseError(e) @@ -371,13 +387,16 @@ impl From> for RecoverableError { impl Display for UnrecoverableError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use UnrecoverableError::*; + match self { - Self::Io(e) => Display::fmt(e, f), - Self::Fmt(e) => Display::fmt(e, f), - Self::XirWriterError(e) => Display::fmt(e, f), + Io(e) => Display::fmt(e, f), + Fmt(e) => Display::fmt(e, f), + XirWriterError(e) => Display::fmt(e, f), + FinalizeError(e) => Display::fmt(e, f), // TODO: Use formatter for dynamic "error(s)" - Self::ErrorsDuringLowering(err_count) => { + ErrorsDuringLowering(err_count) => { write!(f, "aborting due to previous {err_count} error(s)",) } } @@ -401,11 +420,14 @@ impl Display for RecoverableError { impl Error for UnrecoverableError { fn source(&self) -> Option<&(dyn Error + 'static)> { + use UnrecoverableError::*; + match self { - Self::Io(e) => Some(e), - Self::Fmt(e) => Some(e), - Self::XirWriterError(e) => Some(e), - Self::ErrorsDuringLowering(_) => None, + Io(e) => Some(e), + Fmt(e) => Some(e), + XirWriterError(e) => Some(e), + ErrorsDuringLowering(_) => None, + FinalizeError(e) => Some(e), } } } @@ -427,9 +449,15 @@ impl Error for RecoverableError { impl Diagnostic for UnrecoverableError { fn describe(&self) -> Vec { + use UnrecoverableError::*; + match self { + FinalizeError(e) => e.describe(), + // Fall back to `Display` - _ => vec![], + Io(_) | Fmt(_) | XirWriterError(_) | ErrorsDuringLowering(_) => { + vec![] + } } } }