Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: make SourceMap static + module import improvements #960

Merged
merged 7 commits into from
Sep 8, 2023
21 changes: 7 additions & 14 deletions compiler/hash-ast-desugaring/src/desugaring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hash_utils::thin_vec::{thin_vec, ThinVec};

use crate::visitor::AstDesugaring;

impl<'s> AstDesugaring<'s> {
impl AstDesugaring {
/// This function is responsible for converting a [ForLoopBlock] into a
/// simpler [ast::LoopBlock]. This is not obvious from the function
/// definition because it accepts a [ast::AstNode<ast::Block>], not
Expand Down Expand Up @@ -39,12 +39,9 @@ impl<'s> AstDesugaring<'s> {
// Since this function expects it to be a for-loop block, we match it and unwrap
let block = match node {
ast::Block::For(body) => body,
block => panic_on_span!(
parent_span,
self.source_map,
"lowering: expected for-loop, got {}",
block.as_str()
),
block => {
panic_on_span!(parent_span, "lowering: expected for-loop, got {}", block.as_str())
}
};

let ast::ForLoopBlock { pat, iterator, for_body } = block;
Expand Down Expand Up @@ -189,7 +186,6 @@ impl<'s> AstDesugaring<'s> {
ast::Block::While(body) => body,
block => panic_on_span!(
parent_span,
self.source_map,
"lowering: expected while-block, got {}",
block.as_str()
),
Expand Down Expand Up @@ -365,12 +361,9 @@ impl<'s> AstDesugaring<'s> {
// Since this function expects it to be a for-loop block, we match it and unwrap
let block = match node {
ast::Block::If(body) => body,
block => panic_on_span!(
parent_span,
self.source_map,
"lowering: expected if-block, got {}",
block.as_str()
),
block => {
panic_on_span!(parent_span, "lowering: expected if-block, got {}", block.as_str())
}
};

// We don't case about 'clauses' because we can use the visitor to transform
Expand Down
5 changes: 2 additions & 3 deletions compiler/hash-ast-desugaring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ impl<Ctx: AstDesugaringCtxQuery> CompilerStage<Ctx> for AstDesugaringPass {
let AstDesugaringCtx { workspace, pool } = &mut ctx.data();

let node_map = &mut workspace.node_map;
let source_map = &workspace.source_map;
let source_stage_info = &mut workspace.source_stage_info;

pool.scope(|scope| {
// De-sugar the target if it isn't already de-sugared
if !source_stage_info.get(entry_point).is_desugared() && entry_point.is_interactive() {
let source = node_map.get_interactive_block_mut(entry_point.into());
let mut desugarer = AstDesugaring::new(source_map);
let mut desugarer = AstDesugaring::new();

desugarer.visit_body_block(source.node_ref_mut()).unwrap();
}
Expand All @@ -102,7 +101,7 @@ impl<Ctx: AstDesugaringCtxQuery> CompilerStage<Ctx> for AstDesugaringPass {
// investigating this in the future.
for expr in module.node_mut().contents.iter_mut() {
scope.spawn(move |_| {
let mut desugarer = AstDesugaring::new(source_map);
let mut desugarer = AstDesugaring::new();
desugarer.visit_expr(expr.ast_ref_mut()).unwrap()
})
}
Expand Down
16 changes: 6 additions & 10 deletions compiler/hash-ast-desugaring/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ use hash_ast::{
ast_visitor_mut_default_impl,
visitor::{walk_mut, AstVisitorMut},
};
use hash_source::SourceMap;

#[derive(Debug)]
pub struct AstDesugaring<'s> {
pub(crate) source_map: &'s SourceMap,
}
pub struct AstDesugaring {}

impl<'s> AstDesugaring<'s> {
/// Create a new [AstDesugaring]. Contains the [SourceMap] and the
/// current id of the source in reference.
pub fn new(source_map: &'s SourceMap) -> Self {
Self { source_map }
impl AstDesugaring {
/// Create a new [AstDesugaring].
pub fn new() -> Self {
Self {}
}
}

impl<'s> AstVisitorMut for AstDesugaring<'s> {
impl AstVisitorMut for AstDesugaring {
type Error = Infallible;
ast_visitor_mut_default_impl!(hiding: Block);

Expand Down
9 changes: 3 additions & 6 deletions compiler/hash-ast-expand/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl AstExpander<'_> {
if should_dump {
let mode = self.settings.ast_settings.dump_mode;
let character_set = self.settings.character_set;
dump_ast(subject, mode, character_set, self.sources, &mut self.stdout).unwrap();
dump_ast(subject, mode, character_set, &mut self.stdout).unwrap();
}
}

Expand Down Expand Up @@ -162,11 +162,8 @@ impl AstExpander<'_> {

// If we can't convert this into an attribute value, then we
// can't properly check the invocation.
let attr_value = match AttrValueKind::try_from_expr(
arg.value.body(),
self.sources,
ptr_size,
) {
let attr_value = match AttrValueKind::try_from_expr(arg.value.body(), ptr_size)
{
Ok(Some(value)) => value,
Ok(None) => {
let expr_kind = AttrTarget::classify_expr(arg.value.body());
Expand Down
6 changes: 1 addition & 5 deletions compiler/hash-ast-expand/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use hash_ast_utils::attr::AttrNode;
use hash_attrs::checks::AttrChecker;
use hash_pipeline::{interface::CompilerOutputStream, settings::CompilerSettings};
use hash_reporting::diagnostic::DiagnosticsMut;
use hash_source::{SourceId, SourceMap};
use hash_source::SourceId;
use hash_target::data_layout::TargetDataLayout;
use hash_utils::crossbeam_channel::Sender;

Expand All @@ -44,8 +44,6 @@ pub struct AstExpander<'ctx> {
/// A reference to the compiler settings.
pub settings: &'ctx CompilerSettings,

pub sources: &'ctx SourceMap,

pub stdout: CompilerOutputStream,

/// Any diagnostics that have been emitted during the expansion stage.
Expand All @@ -58,13 +56,11 @@ impl<'ctx> AstExpander<'ctx> {
pub fn new(
id: SourceId,
settings: &'ctx CompilerSettings,
sources: &'ctx SourceMap,
data_layout: &'ctx TargetDataLayout,
stdout: CompilerOutputStream,
) -> Self {
Self {
settings,
sources,
stdout,
diagnostics: ExpansionDiagnostics::new(),
checker: AttrChecker::new(id, data_layout),
Expand Down
3 changes: 1 addition & 2 deletions compiler/hash-ast-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ impl<Ctx: AstExpansionCtxQuery> CompilerStage<Ctx> for AstExpansionPass {
let (sender, receiver) = unbounded::<ExpansionDiagnostic>();

let node_map = &mut workspace.node_map;
let sources = &mut workspace.source_map;
let source_stage_info = &mut workspace.source_stage_info;

let make_expander =
|source| AstExpander::new(source, settings, sources, data_layout, stdout.clone());
|source| AstExpander::new(source, settings, data_layout, stdout.clone());

pool.scope(|scope| {
let source_info = source_stage_info.get(entry_point);
Expand Down
4 changes: 1 addition & 3 deletions compiler/hash-ast-utils/src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::fmt;

use hash_source::SourceMap;
use hash_utils::{
clap,
tree_writing::{CharacterSet, TreeWriter, TreeWriterConfig},
Expand Down Expand Up @@ -36,7 +35,6 @@ pub fn dump_ast(
node: AttrNode<'_>,
mode: AstDumpMode,
character_set: CharacterSet,
source_map: &SourceMap,
writer: &mut impl std::io::Write,
) -> std::io::Result<()> {
match mode {
Expand All @@ -46,7 +44,7 @@ pub fn dump_ast(
}
AstDumpMode::Tree => {
// In the tree mode, we prepend the output with the item that we dumped.
writeln!(writer, "AST for `{}`:", source_map.fmt_location(node.id().span()))?;
writeln!(writer, "AST for `{}`:", node.id().span().fmt_path())?;

let tree = node.visit(AstTreePrinter).unwrap();
let config = TreeWriterConfig::from_character_set(character_set);
Expand Down
3 changes: 1 addition & 2 deletions compiler/hash-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
fmt::Display,
hash::Hash,
ops::{Deref, DerefMut},
path::PathBuf,
};

use hash_source::{
Expand Down Expand Up @@ -2009,7 +2008,7 @@ define_tree! {
#[node]
pub struct Import {
pub path: InternedStr,
pub resolved_path: PathBuf,
pub source: SourceId,
}

/// A variable expression.
Expand Down
15 changes: 5 additions & 10 deletions compiler/hash-ast/src/lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
use std::num;

use hash_reporting::{hash_error_codes::error_codes::HashErrorCode, reporter::Reporter};
use hash_source::{
constant::{
BigIntTy, FloatConstant, FloatTy, IntConstant, IntTy, InternedFloat, InternedInt,
NormalisedIntTy, SIntTy, Size, UIntTy,
},
SourceMap,
use hash_source::constant::{
BigIntTy, FloatConstant, FloatTy, IntConstant, IntTy, InternedFloat, InternedInt,
NormalisedIntTy, SIntTy, Size, UIntTy,
};
pub use hash_token::{FloatLitKind, IntLitKind};
use num_bigint::BigInt;
Expand Down Expand Up @@ -179,15 +176,14 @@ impl IntValue {
pub fn parse_int_const_from_lit(
lit: &IntLit,
annotation: Option<IntTy>,
sources: &SourceMap,
ptr_size: Size,
allow_big: bool,
) -> LitParseResult<IntValue> {
let ty = NormalisedIntTy::new(annotation.unwrap_or_default(), ptr_size);
let base: u32 = lit.base.into();

// We have to cleanup the hunk, remove any underscores
let mut hunk = sources.hunk(lit.hunk.span()).to_string();
let mut hunk = lit.hunk.span().contents();
hunk.retain(|c| c != '_');

macro_rules! parse {
Expand Down Expand Up @@ -247,12 +243,11 @@ pub fn parse_int_const_from_lit(
pub fn parse_float_const_from_lit(
lit: &FloatLit,
annotation: Option<FloatTy>,
sources: &SourceMap,
) -> LitParseResult<InternedFloat> {
let ty = annotation.unwrap_or_default();

// We have to cleanup the hunk, remove any underscores
let mut hunk = sources.hunk(lit.hunk.span()).to_string();
let mut hunk = lit.hunk.span().contents();
hunk.retain(|c| c != '_');

macro_rules! parse {
Expand Down
26 changes: 7 additions & 19 deletions compiler/hash-ast/src/node_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use std::{
};

use hash_source::{InteractiveId, ModuleId, SourceId};
use hash_utils::{
index_vec::{index_vec, IndexVec},
path::adjust_canonicalisation,
};
use hash_utils::index_vec::{index_vec, IndexVec};

use crate::ast::{AstNode, BodyBlock, Module, OwnsAstNode};

Expand Down Expand Up @@ -69,44 +66,35 @@ impl OwnsAstNode<BodyBlock> for InteractiveBlock {
pub struct ModuleEntry {
/// The absolute path of the module on disk.
pub path: PathBuf,

/// The generated AST for the module, set when parsing is complete.
node: Option<AstNode<Module>>,
node: AstNode<Module>,
}

impl ModuleEntry {
/// Create a new [ModuleEntry] with a specified `path` and the `node being
/// set to [None].
pub fn new(path: PathBuf) -> Self {
Self { path, node: None }
pub fn new(path: PathBuf, node: AstNode<Module>) -> Self {
Self { path, node }
}

/// Get the `path` from the [Module].
pub fn path(&self) -> &Path {
&self.path
}

/// Get the canonicalised `path` from the [Module].
pub fn canonicalised_path(&self) -> String {
adjust_canonicalisation(self.path())
}

/// Set the `node` for given [Module]
pub fn set_node(&mut self, node: AstNode<Module>) {
self.node = Some(node);
}
}

impl OwnsAstNode<Module> for ModuleEntry {
/// Get a reference to node within [ModuleEntry]. This
/// assumes that the node had already been set.
fn node(&self) -> &AstNode<Module> {
self.node.as_ref().unwrap()
&self.node
}

/// Get a mutable reference to node within [ModuleEntry]. This
/// assumes that the node had already been set.
fn node_mut(&mut self) -> &mut AstNode<Module> {
self.node.as_mut().unwrap()
&mut self.node
}
}

Expand Down
12 changes: 3 additions & 9 deletions compiler/hash-attrs/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use hash_ast::{
use hash_source::{
constant::{InternedFloat, InternedInt, InternedStr},
identifier::Identifier,
SourceMap,
};
use hash_storage::store::{DefaultPartialStore, PartialStore};
use hash_target::{abi::Integer, data_layout::HasDataLayout, primitives::IntTy, size::Size};
Expand Down Expand Up @@ -171,22 +170,17 @@ pub enum AttrValueKind {

impl AttrValueKind {
/// Try to convert an [ast::Expr] into an [AttrValue].
pub fn try_from_expr(
expr: &ast::Expr,
sources: &SourceMap,
ptr_size: Size,
) -> LitParseResult<Option<Self>> {
pub fn try_from_expr(expr: &ast::Expr, ptr_size: Size) -> LitParseResult<Option<Self>> {
match expr {
ast::Expr::Lit(ast::LitExpr { data }) => match data.body() {
ast::Lit::Str(ast::StrLit { data }) => Ok(Some(Self::Str(*data))),
ast::Lit::Char(ast::CharLit { data }) => Ok(Some(Self::Char(*data))),
ast::Lit::Int(int_lit) => {
let value =
parse_int_const_from_lit(int_lit, None, sources, ptr_size, false)?.small();
let value = parse_int_const_from_lit(int_lit, None, ptr_size, false)?.small();
Ok(Some(Self::Int(value)))
}
ast::Lit::Float(float_lit) => {
let value = parse_float_const_from_lit(float_lit, None, sources)?;
let value = parse_float_const_from_lit(float_lit, None)?;
Ok(Some(Self::Float(value)))
}
_ => Ok(None),
Expand Down
Loading
Loading