Skip to content

Commit

Permalink
Merge pull request #909 from hash-org/source-location-to-span
Browse files Browse the repository at this point in the history
core: Use `SourceLocation` (new name `Span`) over `Span` (new name `ByteRange`) to refer to locations
  • Loading branch information
feds01 authored Jul 22, 2023
2 parents 165f531 + 85115fe commit 5f3af9e
Show file tree
Hide file tree
Showing 71 changed files with 770 additions and 777 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions compiler/hash-ast-desugaring/src/desugaring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'s> AstDesugaring<'s> {
let block = match node {
Block::For(body) => body,
block => panic_on_span!(
self.source_location(parent_span),
parent_span,
self.source_map,
"lowering: expected for-loop, got {}",
block.as_str()
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<'s> AstDesugaring<'s> {
Pat::Constructor(ConstructorPat {
subject: make_binding_pat("Some"),
spread: None,
fields: ast_nodes![AstNode::new(TuplePatEntry { name: None, pat }, pat_span)],
fields: ast_nodes![AstNode::new(TuplePatEntry { name: None, pat }, pat_span); pat_span],
}),
pat_span,
);
Expand All @@ -96,14 +96,14 @@ impl<'s> AstDesugaring<'s> {
Pat::Constructor(ConstructorPat {
subject: make_binding_pat("None"),
spread: None,
fields: ast_nodes![],
fields: ast_nodes![; pat_span],
},),
pat_span
),
expr: AstNode::new(Expr::Break(BreakStatement {}), body_span),
},
pat_span
),
); parent_span
];

// Here want to transform the for-loop into just a loop block
Expand All @@ -121,7 +121,7 @@ impl<'s> AstDesugaring<'s> {
args: ast_nodes![AstNode::new(
ConstructorCallArg { name: None, value: iterator },
iter_span
)],
); iter_span],
}),
body_span,
),
Expand Down Expand Up @@ -168,7 +168,7 @@ impl<'s> AstDesugaring<'s> {
let block = match node {
Block::While(body) => body,
block => panic_on_span!(
self.source_location(parent_span),
parent_span,
self.source_map,
"lowering: expected while-block, got {}",
block.as_str()
Expand Down Expand Up @@ -215,7 +215,8 @@ impl<'s> AstDesugaring<'s> {
expr: AstNode::new(Expr::Break(BreakStatement {}), condition_span)
},
condition_span
),
);
parent_span
],
origin: MatchOrigin::While,
}),
Expand Down Expand Up @@ -334,7 +335,7 @@ impl<'s> AstDesugaring<'s> {
let block = match node {
Block::If(body) => body,
block => panic_on_span!(
self.source_location(parent_span),
parent_span,
self.source_map,
"lowering: expected if-block, got {}",
block.as_str()
Expand Down Expand Up @@ -380,7 +381,7 @@ impl<'s> AstDesugaring<'s> {
Expr::Block(BlockExpr {
data: AstNode::new(
Block::Body(BodyBlock {
statements: AstNodes::empty(),
statements: AstNodes::empty(parent_span),
expr: None,
}),
parent_span,
Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-ast-desugaring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<Ctx: AstDesugaringCtxQuery> CompilerStage<Ctx> for AstDesugaringPass {
// 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, entry_point);
let mut desugarer = AstDesugaring::new(source_map);

desugarer.visit_body_block(source.node_ref_mut()).unwrap();
}
Expand All @@ -101,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, source_id);
let mut desugarer = AstDesugaring::new(source_map);
desugarer.visit_expr(expr.ast_ref_mut()).unwrap()
})
}
Expand Down
15 changes: 3 additions & 12 deletions compiler/hash-ast-desugaring/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@ use hash_ast::{
ast_visitor_mut_default_impl,
visitor::{walk_mut, AstVisitorMut},
};
use hash_source::{
location::{SourceLocation, Span},
SourceId, SourceMap,
};
use hash_source::SourceMap;

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

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, source_id: SourceId) -> Self {
Self { source_map, source_id }
}

/// Create a [SourceLocation] from a [Span]
pub(crate) fn source_location(&self, span: Span) -> SourceLocation {
SourceLocation { span, id: self.source_id }
pub fn new(source_map: &'s SourceMap) -> Self {
Self { source_map }
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-ast-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<Ctx: AstExpansionCtxQuery> CompilerStage<Ctx> for AstExpansionPass {

// De-sugar the target if it isn't already de-sugared
if source_info.is_expanded() && entry_point.is_interactive() {
let mut expander = AstExpander::new(source_map, entry_point, settings, stdout.clone());
let mut expander = AstExpander::new(source_map, settings, stdout.clone());
let source = node_map.get_interactive_block(entry_point.into());

expander.visit_body_block(source.node_ref()).unwrap();
Expand All @@ -79,7 +79,7 @@ impl<Ctx: AstExpansionCtxQuery> CompilerStage<Ctx> for AstExpansionPass {
continue;
}

let mut expander = AstExpander::new(source_map, source_id, settings, stdout.clone());
let mut expander = AstExpander::new(source_map, settings, stdout.clone());
expander.visit_module(module.node_ref()).unwrap();
}

Expand Down
23 changes: 7 additions & 16 deletions compiler/hash-ast-expand/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ use hash_pipeline::{
interface::CompilerOutputStream,
settings::{AstDumpMode, CompilerSettings},
};
use hash_source::{
identifier::IDENTS,
location::{SourceLocation, Span},
SourceId, SourceMap,
};
use hash_source::{identifier::IDENTS, SourceMap};
use hash_utils::{
stream_writeln,
tree_writing::{TreeNode, TreeWriter, TreeWriterConfig},
Expand All @@ -25,8 +21,6 @@ use hash_utils::{
pub struct AstExpander<'s> {
/// The map of the current workspace sources.
pub(crate) source_map: &'s SourceMap,
/// The `id` of the module that is currently being checked
source_id: SourceId,

/// The settings to the AST expansion pass.
pub(crate) settings: &'s CompilerSettings,
Expand All @@ -40,16 +34,10 @@ impl<'s> AstExpander<'s> {
/// current id of the source in reference.
pub fn new(
source_map: &'s SourceMap,
source_id: SourceId,
settings: &'s CompilerSettings,
stdout: CompilerOutputStream,
) -> Self {
Self { source_map, settings, source_id, stdout }
}

/// Create a [SourceLocation] from a [Span]
pub(crate) fn source_location(&self, span: Span) -> SourceLocation {
SourceLocation { span, id: self.source_id }
Self { source_map, settings, stdout }
}
}

Expand Down Expand Up @@ -77,9 +65,12 @@ impl<'s> AstVisitorMutSelf for AstExpander<'s> {
} else {
node.subject.span()
};
let location = self.source_location(directive_span);

stream_writeln!(self.stdout, "AST dump for {}", self.source_map.fmt_location(location));
stream_writeln!(
self.stdout,
"AST dump for {}",
self.source_map.fmt_location(directive_span)
);

match ast_settings.dump_mode {
AstDumpMode::Pretty => {
Expand Down
64 changes: 46 additions & 18 deletions compiler/hash-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use std::{
use hash_source::{
constant::{FloatTy, IntTy, InternedFloat, InternedInt, InternedStr, SIntTy},
identifier::Identifier,
location::Span,
location::{ByteRange, Span},
SourceId,
};
use hash_tree_def::define_tree;
use hash_utils::{
Expand All @@ -30,11 +31,24 @@ define_index_type! {
DISABLE_MAX_INDEX_CHECK = cfg!(not(debug_assertions));
}

impl AstNodeId {
/// Get the [Span] of this [AstNodeId].
pub fn span(&self) -> Span {
SpanMap::span_of(*self)
}

/// Get the [SourceId] of this [AstNodeId].
pub fn source(&self) -> SourceId {
SpanMap::source_of(*self)
}
}

/// The [`SPAN_MAP`] is a global static that is used to store the span
/// of each AST node. This is used to avoid storing the [Span] on the
/// [`AstNode<T>`] itself in order for other data structures to be able
/// to query the [Span] of a node simply by using the [AstNodeId] of the
/// node.

static SPAN_MAP: Lazy<RwLock<IndexVec<AstNodeId, Span>>> =
Lazy::new(|| RwLock::new(IndexVec::new()));

Expand All @@ -47,6 +61,11 @@ impl SpanMap {
SPAN_MAP.read()[id]
}

/// Get the [SourceId] of a node by [AstNodeId].
pub fn source_of(id: AstNodeId) -> SourceId {
SpanMap::span_of(id).id
}

/// Get a mutable reference to the [`SPAN_MAP`]. This is only
/// internal to the `hash-ast` crate since it creates entries
/// in the span map when creating new AST nodes.
Expand Down Expand Up @@ -104,6 +123,11 @@ impl<T> AstNode<T> {
SpanMap::span_of(self.id)
}

/// Get the [ByteRange] of this [AstNode].
pub fn byte_range(&self) -> ByteRange {
self.span().span
}

/// Set the [Span] of this [AstNode].
pub fn set_span(&mut self, span: Span) {
SpanMap::update_span(self.id, span)
Expand All @@ -125,7 +149,7 @@ impl<T> AstNode<T> {
}

/// Create an [AstNodeRef] by providing a body and copying over the
/// [Span] and [AstNodeId] that belong to this [AstNode].
/// [AstNodeId] that belong to this [AstNode].
pub fn with_body<'u, U>(&self, body: &'u U) -> AstNodeRef<'u, U> {
AstNodeRef { body, id: self.id }
}
Expand Down Expand Up @@ -160,7 +184,7 @@ impl<'t, T> AstNodeRef<'t, T> {
self.body
}

/// Utility function to copy over the [Span] and [AstNodeId] from
/// Utility function to copy over the [AstNodeId] from
/// another [AstNodeRef] with a provided body.
pub fn with_body<'u, U>(&self, body: &'u U) -> AstNodeRef<'u, U> {
AstNodeRef { body, id: self.id }
Expand Down Expand Up @@ -273,39 +297,43 @@ pub trait OwnsAstNode<T> {
pub struct AstNodes<T> {
/// The nodes that the [AstNodes] holds.
pub nodes: Vec<AstNode<T>>,
/// The span of the AST nodes if one is available.
pub span: Option<Span>,

/// The id that is used to refer to the span of the [AstNodes].
id: AstNodeId,
}

#[macro_export]
macro_rules! ast_nodes {
($($item:expr),*) => {
$crate::ast::AstNodes::new(vec![$($item,)*], None)
($($item:expr),*; $span:expr) => {
$crate::ast::AstNodes::new(vec![$($item,)*], $span)
};
($($item:expr,)*) => {
$crate::ast::AstNodes::new(vec![$($item,)*], None)
($($item:expr,)*; $span:expr) => {
$crate::ast::AstNodes::new(vec![$($item,)*], $span)
};
}

impl<T> AstNodes<T> {
pub fn empty() -> Self {
Self { nodes: vec![], span: None }
/// Create a new [AstNodes].
pub fn empty(span: Span) -> Self {
Self::new(vec![], span)
}

pub fn new(nodes: Vec<AstNode<T>>, span: Option<Span>) -> Self {
Self { nodes, span }
pub fn new(nodes: Vec<AstNode<T>>, span: Span) -> Self {
let id = SpanMap::add_span(span);
Self { nodes, id }
}

/// Function to adjust the span location of [AstNodes] if it is initially
/// incorrectly offset because there is a 'pre-conditional' token that must
/// be parsed before parsing the nodes. This token could be something like a
/// '<' or '(' which starts a tuple, or type bound
pub fn set_span(&mut self, span: Span) {
self.span = Some(span);
SpanMap::update_span(self.id, span);
}

pub fn span(&self) -> Option<Span> {
self.span.or_else(|| Some(self.nodes.first()?.span().join(self.nodes.last()?.span())))
/// Get the [AstNodeId] of this [AstNodes].
pub fn span(&self) -> Span {
SpanMap::span_of(self.id)
}

pub fn ast_ref_iter(&self) -> impl Iterator<Item = AstNodeRef<T>> {
Expand Down Expand Up @@ -2033,7 +2061,7 @@ mod size_asserts {

use super::*;

static_assert_size!(Expr, 88);
static_assert_size!(Expr, 72);
static_assert_size!(Pat, 72);
static_assert_size!(Ty, 64);
static_assert_size!(Ty, 56);
}
6 changes: 3 additions & 3 deletions compiler/hash-codegen-llvm/src/translation/debug_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hash_codegen::{
abi::FnAbi,
traits::debug::{DebugInfoBuilderMethods, VariableKind},
};
use hash_source::{identifier::Identifier, location::SourceLocation};
use hash_source::{identifier::Identifier, location::Span};

use super::LLVMBuilder;

Expand All @@ -24,15 +24,15 @@ impl<'b, 'm> DebugInfoBuilderMethods for LLVMBuilder<'_, 'b, 'm> {
_ty: hash_ir::ty::IrTyId,
_scope: Self::DebugInfoScope,
_kind: VariableKind,
_span: SourceLocation,
_span: Span,
) -> Self::DebugInfoVariable {
unimplemented!()
}

fn create_debug_info_location(
&self,
_scope: Self::DebugInfoScope,
_span: SourceLocation,
_span: Span,
) -> Self::DebugInfoLocation {
unimplemented!()
}
Expand Down
Loading

0 comments on commit 5f3af9e

Please sign in to comment.