From 3fcb28c9428bbc25de1d1f422ec11afa4825c31c Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Mon, 17 Apr 2023 16:08:27 +0200 Subject: [PATCH] [WIP] Define `ScopeGraph` --- crates/hir/src/hir_def/mod.rs | 1 + crates/hir/src/hir_def/scope_graph.rs | 113 ++++++++++++++++++++++++++ crates/hir/src/hir_def/use_tree.rs | 4 +- crates/hir/src/lower/use_tree.rs | 4 +- 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 crates/hir/src/hir_def/scope_graph.rs diff --git a/crates/hir/src/hir_def/mod.rs b/crates/hir/src/hir_def/mod.rs index af4f5f3939..a4dca66586 100644 --- a/crates/hir/src/hir_def/mod.rs +++ b/crates/hir/src/hir_def/mod.rs @@ -5,6 +5,7 @@ pub mod item; pub mod params; pub mod pat; pub mod path; +pub mod scope_graph; pub mod stmt; pub mod types; pub mod use_tree; diff --git a/crates/hir/src/hir_def/scope_graph.rs b/crates/hir/src/hir_def/scope_graph.rs new file mode 100644 index 0000000000..9f284bc7ce --- /dev/null +++ b/crates/hir/src/hir_def/scope_graph.rs @@ -0,0 +1,113 @@ +use cranelift_entity::{entity_impl, PrimaryMap}; +use rustc_hash::FxHashSet; + +use super::{IdentId, ItemKind, PathId, Struct, TopLevelMod, UseAlias}; + +pub struct ScopeGraph { + pub top_mod: TopLevelMod, + pub scopes: PrimaryMap, + pub unresolved: FxHashSet, +} + +pub struct UnresolvedName { + pub scope: LocalScopeId, +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct LocalScope { + kind: ScopeKind, + edges: Vec, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum ScopeKind { + Item(ItemKind), + Field(usize), + Variant(Struct, usize), +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct ScopeEdge { + dest: ScopeId, + kind: EdgeKind, +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum EdgeKind { + Lex(LexEdge), + Mod(ModEdge), + GlobUse(GlobUseEdge), + Use(UseEdge), + Type(TypeEdge), + Func(FuncEdge), + Field(FieldEdge), + Variant(VariantEdge), + Super(SuperEdge), + Ingot(IngotEdge), + SelfTy(SelfTyEdge), + Anon(AnonEdge), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct LexEdge(); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct ModEdge(IdentId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct GlobUseEdge { + /// `UsePathSegment` are lowered to a normal `Path`. + path: PathId, + alias: UseAlias, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct UseEdge { + /// `UsePathSegment` are lowered to a normal `Path`. + path: PathId, + alias: UseAlias, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct TypeEdge(IdentId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct FuncEdge(IdentId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct FieldEdge(IdentId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct VariantEdge(IdentId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct SuperEdge(); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct IngotEdge(); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct SelfEdge { + path: PathId, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct SelfTyEdge { + path: PathId, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct LocalEdge(IdentId); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct AnonEdge(); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct ScopeId { + top_mod: TopLevelMod, + local_id: LocalScopeId, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct LocalScopeId(u32); +entity_impl!(LocalScopeId); diff --git a/crates/hir/src/hir_def/use_tree.rs b/crates/hir/src/hir_def/use_tree.rs index 474a36a3e2..b025d47d88 100644 --- a/crates/hir/src/hir_def/use_tree.rs +++ b/crates/hir/src/hir_def/use_tree.rs @@ -16,7 +16,7 @@ pub struct UseTreeId { //// The alias of this use tree. /// `Bar` in `Foo as Bar;` - pub alias: Option>, + pub alias: Option>, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -33,7 +33,7 @@ pub enum UsePathSegment { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum UseTreeAlias { +pub enum UseAlias { Ident(IdentId), Underscore, } diff --git a/crates/hir/src/lower/use_tree.rs b/crates/hir/src/lower/use_tree.rs index 4080154758..98f4b7a1f1 100644 --- a/crates/hir/src/lower/use_tree.rs +++ b/crates/hir/src/lower/use_tree.rs @@ -23,7 +23,7 @@ impl UseTreeId { }; let alias = ast .alias() - .map(|ast| UseTreeAlias::lower_ast_partial(ctxt, ast)); + .map(|ast| UseAlias::lower_ast_partial(ctxt, ast)); Self::new(ctxt.db, path, subtree, alias) } @@ -55,7 +55,7 @@ impl UsePathSegment { } } -impl UseTreeAlias { +impl UseAlias { pub(super) fn lower_ast_partial( ctxt: &mut FileLowerCtxt<'_>, ast: ast::UseTreeAlias,