Skip to content

Commit

Permalink
[WIP] Define ScopeGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Apr 17, 2023
1 parent f2881ad commit 3fcb28c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/hir/src/hir_def/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
113 changes: 113 additions & 0 deletions crates/hir/src/hir_def/scope_graph.rs
Original file line number Diff line number Diff line change
@@ -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<LocalScopeId, LocalScope>,
pub unresolved: FxHashSet<UnresolvedName>,
}

pub struct UnresolvedName {
pub scope: LocalScopeId,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LocalScope {
kind: ScopeKind,
edges: Vec<ScopeEdge>,
}

#[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);
4 changes: 2 additions & 2 deletions crates/hir/src/hir_def/use_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct UseTreeId {

//// The alias of this use tree.
/// `Bar` in `Foo as Bar;`
pub alias: Option<Partial<UseTreeAlias>>,
pub alias: Option<Partial<UseAlias>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
Expand All @@ -33,7 +33,7 @@ pub enum UsePathSegment {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UseTreeAlias {
pub enum UseAlias {
Ident(IdentId),
Underscore,
}
4 changes: 2 additions & 2 deletions crates/hir/src/lower/use_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -55,7 +55,7 @@ impl UsePathSegment {
}
}

impl UseTreeAlias {
impl UseAlias {
pub(super) fn lower_ast_partial(
ctxt: &mut FileLowerCtxt<'_>,
ast: ast::UseTreeAlias,
Expand Down

0 comments on commit 3fcb28c

Please sign in to comment.