introduce hir::Name

This commit is contained in:
Aleksey Kladov 2018-12-27 20:07:21 +03:00
parent 3b820bcca3
commit d963042ca9
11 changed files with 121 additions and 51 deletions

View File

@ -14,7 +14,7 @@
cancelation::{Canceled, Cancelable}, cancelation::{Canceled, Cancelable},
syntax_ptr::LocalSyntaxPtr, syntax_ptr::LocalSyntaxPtr,
input::{ input::{
FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency,
FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery, FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery,
FileRelativePathQuery FileRelativePathQuery
}, },

View File

@ -1,7 +1,6 @@
use ra_syntax::SmolStr;
pub use ra_db::CrateId; pub use ra_db::CrateId;
use crate::{HirDatabase, Module, Cancelable}; use crate::{HirDatabase, Module, Cancelable, Name, AsName};
/// hir::Crate describes a single crate. It's the main inteface with which /// hir::Crate describes a single crate. It's the main inteface with which
/// crate's dependencies interact. Mostly, it should be just a proxy for the /// crate's dependencies interact. Mostly, it should be just a proxy for the
@ -14,7 +13,7 @@ pub struct Crate {
#[derive(Debug)] #[derive(Debug)]
pub struct CrateDependency { pub struct CrateDependency {
pub krate: Crate, pub krate: Crate,
pub name: SmolStr, pub name: Name,
} }
impl Crate { impl Crate {
@ -27,7 +26,7 @@ pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
.dependencies(self.crate_id) .dependencies(self.crate_id)
.map(|dep| { .map(|dep| {
let krate = Crate::new(dep.crate_id()); let krate = Crate::new(dep.crate_id());
let name = dep.name.clone(); let name = dep.as_name();
CrateDependency { krate, name } CrateDependency { krate, name }
}) })
.collect() .collect()

View File

@ -22,6 +22,7 @@ macro_rules! ctry {
mod arena; mod arena;
pub mod source_binder; pub mod source_binder;
mod name;
mod krate; mod krate;
mod module; mod module;
mod function; mod function;
@ -37,10 +38,12 @@ macro_rules! ctry {
use crate::{ use crate::{
db::HirDatabase, db::HirDatabase,
arena::{Arena, Id}, arena::{Arena, Id},
name::AsName,
}; };
pub use self::{ pub use self::{
path::{Path, PathKind}, path::{Path, PathKind},
name::Name,
krate::Crate, krate::Crate,
module::{Module, ModuleId, Problem, nameres::{ItemMap, PerNs, Namespace}, ModuleScope, Resolution}, module::{Module, ModuleId, Problem, nameres::{ItemMap, PerNs, Namespace}, ModuleScope, Resolution},
function::{Function, FnScopes}, function::{Function, FnScopes},

View File

@ -7,13 +7,14 @@
use ra_syntax::{ use ra_syntax::{
algo::generate, algo::generate,
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},
SmolStr, SyntaxNode, SyntaxNode,
}; };
use ra_db::{SourceRootId, FileId, Cancelable}; use ra_db::{SourceRootId, FileId, Cancelable};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::{ use crate::{
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate, DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate,
Name,
arena::{Arena, Id}, arena::{Arena, Id},
}; };
@ -84,7 +85,7 @@ pub fn crate_root(&self) -> Module {
} }
/// `name` is `None` for the crate's root module /// `name` is `None` for the crate's root module
pub fn name(&self) -> Option<SmolStr> { pub fn name(&self) -> Option<&Name> {
let link = self.module_id.parent_link(&self.tree)?; let link = self.module_id.parent_link(&self.tree)?;
Some(link.name(&self.tree)) Some(link.name(&self.tree))
} }
@ -100,7 +101,7 @@ pub fn def_id(&self, db: &impl HirDatabase) -> DefId {
} }
/// Finds a child module with the specified name. /// Finds a child module with the specified name.
pub fn child(&self, name: &str) -> Option<Module> { pub fn child(&self, name: &Name) -> Option<Module> {
let child_id = self.module_id.child(&self.tree, name)?; let child_id = self.module_id.child(&self.tree, name)?;
Some(Module { Some(Module {
module_id: child_id, module_id: child_id,
@ -230,15 +231,15 @@ fn crate_root(self, tree: &ModuleTree) -> ModuleId {
.last() .last()
.unwrap() .unwrap()
} }
fn child(self, tree: &ModuleTree, name: &str) -> Option<ModuleId> { fn child(self, tree: &ModuleTree, name: &Name) -> Option<ModuleId> {
let link = tree.mods[self] let link = tree.mods[self]
.children .children
.iter() .iter()
.map(|&it| &tree.links[it]) .map(|&it| &tree.links[it])
.find(|it| it.name == name)?; .find(|it| it.name == *name)?;
Some(*link.points_to.first()?) Some(*link.points_to.first()?)
} }
fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (SmolStr, ModuleId)> + 'a { fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (Name, ModuleId)> + 'a {
tree.mods[self].children.iter().filter_map(move |&it| { tree.mods[self].children.iter().filter_map(move |&it| {
let link = &tree.links[it]; let link = &tree.links[it];
let module = *link.points_to.first()?; let module = *link.points_to.first()?;
@ -263,8 +264,8 @@ impl LinkId {
fn owner(self, tree: &ModuleTree) -> ModuleId { fn owner(self, tree: &ModuleTree) -> ModuleId {
tree.links[self].owner tree.links[self].owner
} }
fn name(self, tree: &ModuleTree) -> SmolStr { fn name(self, tree: &ModuleTree) -> &Name {
tree.links[self].name.clone() &tree.links[self].name
} }
fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode { fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode {
let owner = self.owner(tree); let owner = self.owner(tree);
@ -328,7 +329,7 @@ pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
#[derive(Hash, Debug, PartialEq, Eq)] #[derive(Hash, Debug, PartialEq, Eq)]
struct LinkData { struct LinkData {
owner: ModuleId, owner: ModuleId,
name: SmolStr, name: Name,
points_to: Vec<ModuleId>, points_to: Vec<ModuleId>,
problem: Option<Problem>, problem: Option<Problem>,
} }

View File

@ -1,16 +1,13 @@
use std::sync::Arc; use std::sync::Arc;
use ra_syntax::{ use ra_syntax::ast::{self, NameOwner};
ast::{self, NameOwner},
SmolStr,
};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId}; use ra_db::{SourceRoot, SourceRootId, Cancelable, FileId};
use crate::{ use crate::{
HirDatabase, HirDatabase, Name, AsName,
}; };
use super::{ use super::{
@ -20,12 +17,12 @@
#[derive(Clone, Hash, PartialEq, Eq, Debug)] #[derive(Clone, Hash, PartialEq, Eq, Debug)]
pub enum Submodule { pub enum Submodule {
Declaration(SmolStr), Declaration(Name),
Definition(SmolStr, ModuleSource), Definition(Name, ModuleSource),
} }
impl Submodule { impl Submodule {
fn name(&self) -> &SmolStr { fn name(&self) -> &Name {
match self { match self {
Submodule::Declaration(name) => name, Submodule::Declaration(name) => name,
Submodule::Definition(name, _) => name, Submodule::Definition(name, _) => name,
@ -35,14 +32,14 @@ fn name(&self) -> &SmolStr {
pub(crate) fn modules<'a>( pub(crate) fn modules<'a>(
root: impl ast::ModuleItemOwner<'a>, root: impl ast::ModuleItemOwner<'a>,
) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> { ) -> impl Iterator<Item = (Name, ast::Module<'a>)> {
root.items() root.items()
.filter_map(|item| match item { .filter_map(|item| match item {
ast::ModuleItem::Module(m) => Some(m), ast::ModuleItem::Module(m) => Some(m),
_ => None, _ => None,
}) })
.filter_map(|module| { .filter_map(|module| {
let name = module.name()?.text(); let name = module.name()?.as_name();
Some((name, module)) Some((name, module))
}) })
} }
@ -155,7 +152,7 @@ fn build_subtree(
fn resolve_submodule( fn resolve_submodule(
db: &impl HirDatabase, db: &impl HirDatabase,
source: ModuleSource, source: ModuleSource,
name: &SmolStr, name: &Name,
) -> (Vec<FileId>, Option<Problem>) { ) -> (Vec<FileId>, Option<Problem>) {
// FIXME: handle submodules of inline modules properly // FIXME: handle submodules of inline modules properly
let file_id = source.file_id(); let file_id = source.file_id();

View File

@ -14,14 +14,12 @@
//! modifications (that is, typing inside a function shold not change IMIs), //! modifications (that is, typing inside a function shold not change IMIs),
//! such that the results of name resolution can be preserved unless the module //! such that the results of name resolution can be preserved unless the module
//! structure itself is modified. //! structure itself is modified.
use std::{ use std::sync::Arc;
sync::Arc,
};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use ra_syntax::{ use ra_syntax::{
TextRange, TextRange,
SmolStr, SyntaxKind::{self, *}, SyntaxKind::{self, *},
ast::{self, AstNode} ast::{self, AstNode}
}; };
use ra_db::SourceRootId; use ra_db::SourceRootId;
@ -32,6 +30,7 @@
SourceItemId, SourceFileItemId, SourceFileItems, SourceItemId, SourceFileItemId, SourceFileItems,
Path, PathKind, Path, PathKind,
HirDatabase, Crate, HirDatabase, Crate,
Name, AsName,
module::{Module, ModuleId, ModuleTree}, module::{Module, ModuleId, ModuleTree},
}; };
@ -45,14 +44,14 @@ pub struct ItemMap {
#[derive(Debug, Default, PartialEq, Eq, Clone)] #[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct ModuleScope { pub struct ModuleScope {
items: FxHashMap<SmolStr, Resolution>, items: FxHashMap<Name, Resolution>,
} }
impl ModuleScope { impl ModuleScope {
pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a SmolStr, &'a Resolution)> + 'a { pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a {
self.items.iter() self.items.iter()
} }
pub fn get(&self, name: &SmolStr) -> Option<&Resolution> { pub fn get(&self, name: &Name) -> Option<&Resolution> {
self.items.get(name) self.items.get(name)
} }
} }
@ -72,7 +71,7 @@ pub struct InputModuleItems {
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
struct ModuleItem { struct ModuleItem {
id: SourceFileItemId, id: SourceFileItemId,
name: SmolStr, name: Name,
kind: SyntaxKind, kind: SyntaxKind,
vis: Vis, vis: Vis,
} }
@ -260,7 +259,7 @@ fn add_use_item(&mut self, file_items: &SourceFileItems, item: ast::UseItem) {
impl ModuleItem { impl ModuleItem {
fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> { fn new<'a>(file_items: &SourceFileItems, item: impl ast::NameOwner<'a>) -> Option<ModuleItem> {
let name = item.name()?.text(); let name = item.name()?.as_name();
let kind = item.syntax().kind(); let kind = item.syntax().kind();
let vis = Vis::Other; let vis = Vis::Other;
let id = file_items.id_of_unchecked(item.syntax()); let id = file_items.id_of_unchecked(item.syntax());
@ -328,7 +327,11 @@ fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) ->
for dep in krate.dependencies(self.db) { for dep in krate.dependencies(self.db) {
if let Some(module) = dep.krate.root_module(self.db)? { if let Some(module) = dep.krate.root_module(self.db)? {
let def_id = module.def_id(self.db); let def_id = module.def_id(self.db);
self.add_module_item(&mut module_items, dep.name, PerNs::types(def_id)); self.add_module_item(
&mut module_items,
dep.name.clone(),
PerNs::types(def_id),
);
} }
} }
}; };
@ -389,7 +392,7 @@ fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) ->
Ok(()) Ok(())
} }
fn add_module_item(&self, module_items: &mut ModuleScope, name: SmolStr, def_id: PerNs<DefId>) { fn add_module_item(&self, module_items: &mut ModuleScope, name: Name, def_id: PerNs<DefId>) {
let resolution = Resolution { let resolution = Resolution {
def_id, def_id,
import: None, import: None,

View File

@ -9,6 +9,7 @@
self as hir, self as hir,
db::HirDatabase, db::HirDatabase,
mock::MockDatabase, mock::MockDatabase,
Name,
}; };
fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) { fn item_map(fixture: &str) -> (Arc<hir::ItemMap>, hir::ModuleId) {
@ -38,7 +39,7 @@ fn item_map_smoke_test() {
pub struct Baz; pub struct Baz;
", ",
); );
let name = SmolStr::from("Baz"); let name = Name::new(SmolStr::from("Baz"));
let resolution = &item_map.per_module[&module_id].items[&name]; let resolution = &item_map.per_module[&module_id].items[&name];
assert!(resolution.def_id.take_types().is_some()); assert!(resolution.def_id.take_types().is_some());
} }
@ -57,7 +58,7 @@ fn test_self() {
pub struct Baz; pub struct Baz;
", ",
); );
let name = SmolStr::from("Baz"); let name = Name::new(SmolStr::from("Baz"));
let resolution = &item_map.per_module[&module_id].items[&name]; let resolution = &item_map.per_module[&module_id].items[&name];
assert!(resolution.def_id.take_types().is_some()); assert!(resolution.def_id.take_types().is_some());
} }
@ -90,7 +91,7 @@ fn item_map_across_crates() {
let module_id = module.module_id; let module_id = module.module_id;
let item_map = db.item_map(source_root).unwrap(); let item_map = db.item_map(source_root).unwrap();
let name = SmolStr::from("Baz"); let name = Name::new(SmolStr::from("Baz"));
let resolution = &item_map.per_module[&module_id].items[&name]; let resolution = &item_map.per_module[&module_id].items[&name];
assert!(resolution.def_id.take_types().is_some()); assert!(resolution.def_id.take_types().is_some());
} }

56
crates/ra_hir/src/name.rs Normal file
View File

@ -0,0 +1,56 @@
use std::fmt;
use ra_syntax::{ast, SmolStr};
/// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygene info, but we are
/// not there yet!
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Name {
text: SmolStr,
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.text, f)
}
}
impl Name {
// TODO: get rid of this?
pub(crate) fn as_str(&self) -> &str {
self.text.as_str()
}
#[cfg(not(test))]
fn new(text: SmolStr) -> Name {
Name { text }
}
#[cfg(test)]
pub(crate) fn new(text: SmolStr) -> Name {
Name { text }
}
}
pub(crate) trait AsName {
fn as_name(&self) -> Name;
}
impl AsName for ast::NameRef<'_> {
fn as_name(&self) -> Name {
Name::new(self.text())
}
}
impl AsName for ast::Name<'_> {
fn as_name(&self) -> Name {
Name::new(self.text())
}
}
impl AsName for ra_db::Dependency {
fn as_name(&self) -> Name {
Name::new(self.name.clone())
}
}

View File

@ -1,9 +1,11 @@
use ra_syntax::{SmolStr, ast, AstNode, TextRange}; use ra_syntax::{ast, AstNode, TextRange};
use crate::{Name, AsName};
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Path { pub struct Path {
pub kind: PathKind, pub kind: PathKind,
pub segments: Vec<SmolStr>, pub segments: Vec<Name>,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -29,7 +31,7 @@ pub fn from_ast(mut path: ast::Path) -> Option<Path> {
loop { loop {
let segment = path.segment()?; let segment = path.segment()?;
match segment.kind()? { match segment.kind()? {
ast::PathSegmentKind::Name(name) => segments.push(name.text()), ast::PathSegmentKind::Name(name) => segments.push(name.as_name()),
ast::PathSegmentKind::CrateKw => { ast::PathSegmentKind::CrateKw => {
kind = PathKind::Crate; kind = PathKind::Crate;
break; break;
@ -67,6 +69,14 @@ fn qualifier(path: ast::Path) -> Option<ast::Path> {
pub fn is_ident(&self) -> bool { pub fn is_ident(&self) -> bool {
self.kind == PathKind::Plain && self.segments.len() == 1 self.kind == PathKind::Plain && self.segments.len() == 1
} }
/// If this path is a single identifier, like `foo`, return its name.
pub fn as_ident(&self) -> Option<&Name> {
if self.kind != PathKind::Plain || self.segments.len() > 1 {
return None;
}
self.segments.first()
}
} }
fn expand_use_tree( fn expand_use_tree(
@ -130,7 +140,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path) -> Option<Path> {
kind: PathKind::Plain, kind: PathKind::Plain,
segments: Vec::with_capacity(1), segments: Vec::with_capacity(1),
}); });
res.segments.push(name.text()); res.segments.push(name.as_name());
res res
} }
ast::PathSegmentKind::CrateKw => { ast::PathSegmentKind::CrateKw => {

View File

@ -11,7 +11,7 @@
use ra_db::{SourceRootId, FileId, Cancelable,}; use ra_db::{SourceRootId, FileId, Cancelable,};
use crate::{ use crate::{
SourceFileItems, SourceItemId, DefKind, Function, DefId, SourceFileItems, SourceItemId, DefKind, Function, DefId, Name, AsName,
db::HirDatabase, db::HirDatabase,
function::{FnScopes, FnId}, function::{FnScopes, FnId},
module::{ module::{
@ -130,14 +130,14 @@ fn collect_submodules<'a>(
pub(crate) fn modules<'a>( pub(crate) fn modules<'a>(
root: impl ast::ModuleItemOwner<'a>, root: impl ast::ModuleItemOwner<'a>,
) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> { ) -> impl Iterator<Item = (Name, ast::Module<'a>)> {
root.items() root.items()
.filter_map(|item| match item { .filter_map(|item| match item {
ast::ModuleItem::Module(m) => Some(m), ast::ModuleItem::Module(m) => Some(m),
_ => None, _ => None,
}) })
.filter_map(|module| { .filter_map(|module| {
let name = module.name()?.text(); let name = module.name()?.as_name();
Some((name, module)) Some((name, module))
}) })
} }

View File

@ -179,13 +179,13 @@ pub(crate) fn from_hir_path(
module: &Module, module: &Module,
path: &Path, path: &Path,
) -> Cancelable<Self> { ) -> Cancelable<Self> {
if path.is_ident() { if let Some(name) = path.as_ident() {
let name = &path.segments[0]; let name = name.as_str(); // :-(
if let Some(int_ty) = primitive::IntTy::from_string(&name) { if let Some(int_ty) = primitive::IntTy::from_string(name) {
return Ok(Ty::Int(int_ty)); return Ok(Ty::Int(int_ty));
} else if let Some(uint_ty) = primitive::UintTy::from_string(&name) { } else if let Some(uint_ty) = primitive::UintTy::from_string(name) {
return Ok(Ty::Uint(uint_ty)); return Ok(Ty::Uint(uint_ty));
} else if let Some(float_ty) = primitive::FloatTy::from_string(&name) { } else if let Some(float_ty) = primitive::FloatTy::from_string(name) {
return Ok(Ty::Float(float_ty)); return Ok(Ty::Float(float_ty));
} }
} }