Skip to content

Commit

Permalink
Add lazy span for expr
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Apr 6, 2023
1 parent 0621fea commit 6dfb646
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 84 deletions.
6 changes: 3 additions & 3 deletions crates/hir/src/hir_def/expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cranelift_entity::entity_impl;

use super::{Body, IdentId, IntegerId, LitKind, Partial, PatId, PathId, StmtId};
use super::{Body, GenericArgListId, IdentId, IntegerId, LitKind, Partial, PatId, PathId, StmtId};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Expr {
Expand All @@ -13,10 +13,10 @@ pub enum Expr {
Bin(ExprId, ExprId, Partial<BinOp>),
Un(ExprId, Partial<UnOp>),
/// The first `ExprId` is the callee, the second is the arguments.
Call(ExprId, Vec<CallArg>),
Call(ExprId, GenericArgListId, Vec<CallArg>),
/// The first `ExprId` is the method receiver, the second is the method
/// name, the third is the arguments.
MethodCall(ExprId, Partial<IdentId>, Vec<CallArg>),
MethodCall(ExprId, Partial<IdentId>, GenericArgListId, Vec<CallArg>),
Path(Partial<PathId>),
/// The record construction expression.
/// The fist `PathId` is the record type, the second is the record fields.
Expand Down
10 changes: 7 additions & 3 deletions crates/hir/src/lower/expr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use parser::ast::{self, prelude::*};

use crate::{
hir_def::{expr::*, Body, IdentId, IntegerId, LitKind, Pat, PathId, Stmt},
hir_def::{expr::*, Body, GenericArgListId, IdentId, IntegerId, LitKind, Pat, PathId, Stmt},
span::LocalOrigin,
};

Expand Down Expand Up @@ -43,6 +43,8 @@ impl Expr {

ast::ExprKind::Call(call) => {
let callee = Self::push_to_body_opt(ctxt, call.callee());
let generic_args =
GenericArgListId::lower_ast_opt(ctxt.f_ctxt, call.generic_args());
let args = call
.args()
.map(|args| {
Expand All @@ -51,13 +53,15 @@ impl Expr {
.collect()
})
.unwrap_or_default();
Self::Call(callee, args)
Self::Call(callee, generic_args, args)
}

ast::ExprKind::MethodCall(method_call) => {
let receiver = Self::push_to_body_opt(ctxt, method_call.receiver());
let method_name =
IdentId::lower_token_partial(ctxt.f_ctxt, method_call.method_name());
let generic_args =
GenericArgListId::lower_ast_opt(ctxt.f_ctxt, method_call.generic_args());
let args = method_call
.args()
.map(|args| {
Expand All @@ -66,7 +70,7 @@ impl Expr {
.collect()
})
.unwrap_or_default();
Self::MethodCall(receiver, method_name, args)
Self::MethodCall(receiver, method_name, generic_args, args)
}

ast::ExprKind::Path(path) => {
Expand Down
12 changes: 6 additions & 6 deletions crates/hir/src/span/attr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use parser::ast::{self, prelude::*};

use super::define_lazy_span_item;
use super::define_lazy_span_node;

define_lazy_span_item!(
define_lazy_span_node!(
LazyAttrListSpan,
ast::AttrList,
@idx {
Expand All @@ -20,8 +20,8 @@ impl LazyAttrListSpan {
}
}

define_lazy_span_item!(LazyAttrSpan);
define_lazy_span_item!(
define_lazy_span_node!(LazyAttrSpan);
define_lazy_span_node!(
LazyNormalAttrSpan,
ast::NormalAttr,
@token {
Expand All @@ -32,15 +32,15 @@ define_lazy_span_item!(
}
);

define_lazy_span_item!(
define_lazy_span_node!(
LazyAttrArgListSpan,
ast::AttrArgList,
@idx {
(arg, LazyAttrArgSpan),
}
);

define_lazy_span_item!(
define_lazy_span_node!(
LazyAttrArgSpan,
ast::AttrArg,
@token {
Expand Down
186 changes: 186 additions & 0 deletions crates/hir/src/span/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
use common::InputFile;
use parser::{ast, SyntaxNode};

use crate::{
hir_def::{Body, ExprId},
parse_file,
span::{params::LazyGenericArgListSpan, path::LazyPathSpan, LazySpanAtom},
};

use super::{
db::SpannedHirDb,
define_lazy_span_node,
transition::{ChainRoot, SpanTransitionChain},
};

define_lazy_span_node!(LazyExprSpan, ast::Expr,);
impl LazyExprSpan {
pub fn new(expr: ExprId, body: Body) -> Self {
let root = ExprRoot { expr, body };
Self(SpanTransitionChain::new(root))
}

pub fn into_bin_expr(self) -> LazyBinExprSpan {
LazyBinExprSpan(self.0)
}

pub fn into_un_expr(self) -> LazyUnExprSpan {
LazyUnExprSpan(self.0)
}

pub fn into_call_expr(self) -> LazyCallExprSpan {
LazyCallExprSpan(self.0)
}

pub fn into_method_call_expr(self) -> LazyMethodCallExprSpan {
LazyMethodCallExprSpan(self.0)
}

pub fn into_path_expr(self) -> LazyPathExprSpan {
LazyPathExprSpan(self.0)
}

pub fn into_record_init_expr(self) -> LazyRecordInitExprSpan {
LazyRecordInitExprSpan(self.0)
}

pub fn into_field_expr(self) -> LazyFieldExprSpan {
LazyFieldExprSpan(self.0)
}

pub fn into_match_expr(self) -> LazyMatchExprSpan {
LazyMatchExprSpan(self.0)
}
}

define_lazy_span_node!(
LazyBinExprSpan,
ast::BinExpr,
@node {
(op, op, LazySpanAtom),
}
);

define_lazy_span_node!(
LazyUnExprSpan,
ast::UnExpr,
@node {
(op, op, LazySpanAtom),
}
);

define_lazy_span_node!(
LazyCallExprSpan,
ast::CallExpr,
@node {
(generic_args, generic_args, LazyGenericArgListSpan),
(args, args, LazyCallArgListSpan),
}
);

define_lazy_span_node!(
LazyMethodCallExprSpan,
ast::MethodCallExpr,
@token {
(method_name, method_name),
}
@node {
(generic_args, generic_args, LazyGenericArgListSpan),
(args, args, LazyCallArgListSpan),
}
);

define_lazy_span_node! {
LazyPathExprSpan,
ast::PathExpr,
@node {
(path, path, LazyPathSpan),
}
}

define_lazy_span_node!(
LazyRecordInitExprSpan,
ast::RecordInitExpr,
@node {
(path, path, LazyPathSpan),
(fields, fields, LazyRecordFieldListSpan),
}
);

define_lazy_span_node!(
LazyFieldExprSpan,
ast::FieldExpr,
@token {
(accessor, name_or_index),
}
);

define_lazy_span_node!(
LazyMatchExprSpan,
ast::MatchExpr,
@node {
(arms, arms, LazyMatchArmListSpan),
}
);

define_lazy_span_node!(
LazyCallArgListSpan,
ast::CallArgList,
@idx {
(arg, LazyCallArgSpan),
}
);

define_lazy_span_node!(
LazyCallArgSpan,
ast::CallArg,
@token {
(label, label),
}
);

define_lazy_span_node!(
LazyRecordFieldListSpan,
ast::RecordFieldList,
@idx {
(field, LazyRecordFieldSpan),
}
);

define_lazy_span_node!(
LazyRecordFieldSpan,
ast::RecordField,
@token {
(label, label),
}
);

define_lazy_span_node!(
LazyMatchArmListSpan,
ast::MatchArmList,
@idx {
(arm, LazySpanAtom),
}
);

#[derive(Clone, Copy)]
struct ExprRoot {
expr: ExprId,
body: Body,
}

impl ChainRoot for ExprRoot {
fn root(&self, db: &dyn SpannedHirDb) -> (InputFile, Option<SyntaxNode>) {
let body_ast = db.body_ast(self.body);
let file = body_ast.file;
let source_map = db.body_source_map(self.body);
let pat_source = source_map.expr_map.node_to_source(self.expr);
let Some(ptr) = pat_source.syntax_ptr() else {
return (file, None);
};

let root_node = SyntaxNode::new_root(parse_file(db.upcast(), file));
let node = ptr.to_node(&root_node);
(file, node.into())
}
}
Loading

0 comments on commit 6dfb646

Please sign in to comment.