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

spanless ast #907

Merged
merged 3 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions compiler/hash-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
num-bigint = "0.4"
replace_with = "0.1.7"
once_cell = "1.17"

hash-source = { path = "../hash-source" }
hash-tree-def = { path = "../hash-tree-def" }
Expand Down
92 changes: 63 additions & 29 deletions compiler/hash-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,51 @@ use hash_source::{
location::Span,
};
use hash_tree_def::define_tree;
use hash_utils::{counter, smallvec::SmallVec};
use hash_utils::{
index_vec::{define_index_type, IndexVec},
parking_lot::RwLock,
smallvec::SmallVec,
};
use once_cell::sync::Lazy;
use replace_with::replace_with_or_abort;

counter! {
name: AstNodeId,
counter_name: AST_NODE_ID_COUNTER,
visibility: pub,
method_visibility: pub,
define_index_type! {
/// This is the unique identifier for an AST node. This is used to
/// map spans to nodes, and vice versa. [AstNodeId]s are unique and
/// they are always increasing as a new nodes are created.
pub struct AstNodeId = u32;
MAX_INDEX = i32::max_value() as usize;
DISABLE_MAX_INDEX_CHECK = cfg!(not(debug_assertions));
}

/// 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()));

/// Utilities for working with the [`SPAN_MAP`].
pub struct SpanMap;

impl SpanMap {
/// Get the span of a node by [AstNodeId].
pub fn span_of(id: AstNodeId) -> Span {
SPAN_MAP.read()[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.
fn add_span(span: Span) -> AstNodeId {
SPAN_MAP.write().push(span)
}

/// Update the span of a node by [AstNodeId].
fn update_span(id: AstNodeId, span: Span) {
SPAN_MAP.write()[id] = span;
}
}

/// Represents an abstract syntax tree node.
Expand All @@ -30,8 +67,6 @@ counter! {
pub struct AstNode<T> {
/// The stored data within this node
body: Box<T>,
/// Associated [Span] with this node
span: Span,
/// Associated `id` with this [AstNode<T>]
id: AstNodeId,
}
Expand All @@ -45,7 +80,8 @@ impl<T> PartialEq for AstNode<T> {
impl<T> AstNode<T> {
/// Create a new node with a given body and location.
pub fn new(body: T, span: Span) -> Self {
Self { body: Box::new(body), span, id: AstNodeId::new() }
let id = SpanMap::add_span(span);
Self { body: Box::new(body), id }
}

/// Get a reference to the body contained within this node.
Expand All @@ -65,12 +101,12 @@ impl<T> AstNode<T> {

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

/// Set the [Span] of this [AstNode].
pub fn set_span(&mut self, span: Span) {
self.span = span;
SpanMap::update_span(self.id, span)
}

/// Get the [AstNodeId] of this node.
Expand All @@ -80,27 +116,26 @@ impl<T> AstNode<T> {

/// Create an [AstNodeRef] from this [AstNode].
pub fn ast_ref(&self) -> AstNodeRef<T> {
AstNodeRef { body: self.body.as_ref(), span: self.span, id: self.id }
AstNodeRef { body: self.body.as_ref(), id: self.id }
}

/// Create an [AstNodeRefMut] from this [AstNode].
pub fn ast_ref_mut(&mut self) -> AstNodeRefMut<T> {
AstNodeRefMut { body: self.body.as_mut(), span: self.span, id: self.id }
AstNodeRefMut { body: self.body.as_mut(), id: self.id }
}

/// Create an [AstNodeRef] by providing a body and copying over the
/// [Span] and [AstNodeId] that belong to this [AstNode].
pub fn with_body<'u, U>(&self, body: &'u U) -> AstNodeRef<'u, U> {
AstNodeRef { body, span: self.span, id: self.id }
AstNodeRef { body, id: self.id }
}
}

#[derive(Debug)]
pub struct AstNodeRef<'t, T> {
/// A reference to the body of the [AstNode].
pub body: &'t T,
/// The [Span] of the node.
pub span: Span,

/// The [AstNodeId] of the node, representing a unique identifier within
/// the AST, useful for performing fast comparisons of trees.
pub id: AstNodeId,
Expand All @@ -116,8 +151,8 @@ impl<T> Copy for AstNodeRef<'_, T> {}

impl<'t, T> AstNodeRef<'t, T> {
/// Create a new [AstNodeRef<T>].
pub fn new(body: &'t T, span: Span, id: AstNodeId) -> Self {
AstNodeRef { body, span, id }
pub fn new(body: &'t T, id: AstNodeId) -> Self {
AstNodeRef { body, id }
}

/// Get a reference to body of the [AstNodeRef].
Expand All @@ -128,12 +163,12 @@ impl<'t, T> AstNodeRef<'t, T> {
/// Utility function to copy over the [Span] and [AstNodeId] from
/// another [AstNodeRef] with a provided body.
pub fn with_body<'u, U>(&self, body: &'u U) -> AstNodeRef<'u, U> {
AstNodeRef { body, span: self.span, id: self.id }
AstNodeRef { body, id: self.id }
}

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

/// Get the [AstNodeId] of this [AstNodeRef].
Expand All @@ -154,17 +189,16 @@ impl<T> Deref for AstNodeRef<'_, T> {
pub struct AstNodeRefMut<'t, T> {
/// A mutable reference to the body of the [AstNode].
body: &'t mut T,
/// The [Span] of the [AstNode].
pub span: Span,

/// The [AstNodeId] of the [AstNode], representing a unique identifier
/// within the AST, useful for performing fast comparisons of trees.
pub id: AstNodeId,
}

impl<'t, T> AstNodeRefMut<'t, T> {
/// Create a new [AstNodeRefMut<T>].
pub fn new(body: &'t mut T, span: Span, id: AstNodeId) -> Self {
AstNodeRefMut { body, span, id }
pub fn new(body: &'t mut T, id: AstNodeId) -> Self {
AstNodeRefMut { body, id }
}

/// Get a reference to body of the [AstNodeRefMut].
Expand All @@ -184,7 +218,7 @@ impl<'t, T> AstNodeRefMut<'t, T> {

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

/// Get the [AstNodeId] of this [AstNodeRefMut].
Expand All @@ -194,7 +228,7 @@ impl<'t, T> AstNodeRefMut<'t, T> {

/// Get this node as an immutable reference
pub fn immutable(&self) -> AstNodeRef<T> {
AstNodeRef::new(self.body, self.span, self.id)
AstNodeRef::new(self.body, self.id)
}
}

Expand Down Expand Up @@ -1999,7 +2033,7 @@ mod size_asserts {

use super::*;

static_assert_size!(Expr, 96);
static_assert_size!(Pat, 88);
static_assert_size!(Ty, 80);
static_assert_size!(Expr, 88);
static_assert_size!(Pat, 72);
static_assert_size!(Ty, 64);
}
2 changes: 1 addition & 1 deletion compiler/hash-ast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Hash Compiler AST library file

#![feature(box_into_inner, iter_intersperse, let_chains)]
#![feature(box_into_inner, iter_intersperse, let_chains, lazy_cell)]

pub mod ast;
pub mod node_map;
Expand Down
5 changes: 1 addition & 4 deletions compiler/hash-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ mod source;
use std::{env, path::PathBuf};

use crossbeam_channel::{unbounded, Sender};
use hash_ast::{
ast::{self},
node_map::ModuleEntry,
};
use hash_ast::{ast, node_map::ModuleEntry};
use hash_lexer::Lexer;
use hash_pipeline::{
interface::{CompilerInterface, CompilerStage},
Expand Down
1 change: 0 additions & 1 deletion compiler/hash-tir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ textwrap = "0.16"
utility-types = "0.0.2"
typed-builder = "0.11"
lazy_static = "1.4"
parking_lot = "0.12"

hash-ast = { path = "../hash-ast" }
hash-source = { path = "../hash-source" }
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-tir/src/ast_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::hash::Hash;

use bimap::BiMap;
use hash_ast::ast::AstNodeId;
use parking_lot::RwLock;
use hash_utils::parking_lot::RwLock;

use crate::{
args::ArgId,
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-tir/src/locations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use hash_source::location::{SourceLocation, Span};
use hash_storage::store::SequenceStoreKey;
use parking_lot::RwLock;
use hash_utils::parking_lot::RwLock;

use super::{
args::{ArgId, ArgsId, PatArgId, PatArgsId},
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-tir/src/problems.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use parking_lot::RwLock;
use hash_utils::parking_lot::RwLock;

use crate::context::Context;

Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-tir/src/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hash_storage::{
static_single_store,
store::{statics::StoreId, Store, TrivialSequenceStoreKey},
};
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
use hash_utils::parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
use textwrap::indent;
use utility_types::omit;

Expand Down
6 changes: 3 additions & 3 deletions compiler/hash-tree-def/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ fn emit_walk_node_field(
if nodes_mut {
Ok(Some(quote! {
visitor.#visit_child_function_name(
super::#node_ref_name::new(#field_path, span, id)
super::#node_ref_name::new(#field_path, id)
)?
}))
} else {
Expand Down Expand Up @@ -629,7 +629,7 @@ fn emit_walker_enum_function(
nodes_mut,
self_mut,
quote! {
let (span, id) = (node.span(), node.id());
let id = node.id();
Ok(match #ref_or_mut *node {
#(#cases),*
})
Expand Down Expand Up @@ -676,7 +676,7 @@ fn emit_walker_struct_function(
nodes_mut,
self_mut,
quote! {
let (span, id) = (node.span(), node.id());
let id = node.id();
Ok(#node_name {
#(#walk_fields),*
})
Expand Down
1 change: 1 addition & 0 deletions compiler/hash-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ pub use fxhash;
pub use index_vec;
pub use itertools;
pub use log;
pub use parking_lot;
pub use smallvec;
pub use thin_vec;
Loading