mirror of
https://github.com/oxalica/nil.git
synced 2024-11-26 02:48:06 +03:00
Replace Box<str> with SmolStr, rename and simplify
This commit is contained in:
parent
e7e171f988
commit
9ce13ad940
@ -5,6 +5,7 @@ use super::{
|
||||
use crate::base::{FileId, InFile};
|
||||
use la_arena::Arena;
|
||||
use rowan::ast::AstNode;
|
||||
use smol_str::SmolStr;
|
||||
use syntax::ast::{self, LiteralKind};
|
||||
|
||||
pub(super) fn lower(root: InFile<ast::SourceFile>) -> (Module, ModuleSourceMap) {
|
||||
@ -68,7 +69,7 @@ impl LowerCtx {
|
||||
let name = e
|
||||
.token()
|
||||
.map_or_else(Default::default, |tok| tok.text().into());
|
||||
self.alloc_expr(Expr::Ident(name), ptr)
|
||||
self.alloc_expr(Expr::Reference(name), ptr)
|
||||
}
|
||||
ast::Expr::Apply(e) => {
|
||||
let func = self.lower_expr_opt(e.function());
|
||||
@ -116,7 +117,7 @@ impl LowerCtx {
|
||||
let tok = lit.token().unwrap();
|
||||
let mut text = tok.text();
|
||||
|
||||
fn normalize_path(path: &str) -> (usize, Box<str>) {
|
||||
fn normalize_path(path: &str) -> (usize, SmolStr) {
|
||||
let mut ret = String::new();
|
||||
let mut supers = 0usize;
|
||||
for seg in path.split('/').filter(|&seg| !seg.is_empty() && seg != ".") {
|
||||
|
@ -8,10 +8,7 @@ use crate::base::{FileId, SourceDatabase};
|
||||
use la_arena::{Arena, ArenaMap, Idx};
|
||||
use ordered_float::OrderedFloat;
|
||||
use smol_str::SmolStr;
|
||||
use std::borrow::Borrow;
|
||||
use std::collections::HashMap;
|
||||
use std::ops;
|
||||
use std::sync::Arc;
|
||||
use std::{collections::HashMap, ops, sync::Arc};
|
||||
|
||||
pub use self::scope::{ModuleScopes, ScopeData, ScopeId};
|
||||
|
||||
@ -26,8 +23,8 @@ pub trait DefDatabase: SourceDatabase {
|
||||
#[salsa::invoke(ModuleScopes::module_scopes_query)]
|
||||
fn scopes(&self, file_id: FileId) -> Arc<ModuleScopes>;
|
||||
|
||||
#[salsa::invoke(ModuleScopes::lookup_name_query)]
|
||||
fn lookup_name(&self, file_id: FileId, expr_id: ExprId) -> Option<NameDefId>;
|
||||
#[salsa::invoke(ModuleScopes::resolve_name_query)]
|
||||
fn resolve_name(&self, file_id: FileId, expr_id: ExprId) -> Option<NameDefId>;
|
||||
}
|
||||
|
||||
fn module_with_source_map(
|
||||
@ -98,7 +95,7 @@ impl ModuleSourceMap {
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Expr {
|
||||
Missing,
|
||||
Ident(Name),
|
||||
Reference(SmolStr),
|
||||
Literal(Literal),
|
||||
Apply(ExprId, ExprId),
|
||||
Lambda(Option<NameDefId>, Option<Pat>, ExprId),
|
||||
@ -107,37 +104,15 @@ pub enum Expr {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct NameDef {
|
||||
pub name: Name,
|
||||
pub name: SmolStr,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub enum Literal {
|
||||
Int(i64),
|
||||
Float(OrderedFloat<f64>),
|
||||
String(Box<str>),
|
||||
String(SmolStr),
|
||||
Path(Path),
|
||||
// TODO
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct Name(SmolStr);
|
||||
|
||||
impl From<&'_ str> for Name {
|
||||
fn from(s: &'_ str) -> Self {
|
||||
Self(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Name {
|
||||
fn from(s: String) -> Self {
|
||||
Self(s.into())
|
||||
}
|
||||
}
|
||||
|
||||
impl Borrow<str> for Name {
|
||||
fn borrow(&self) -> &str {
|
||||
&*self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
@ -145,7 +120,7 @@ pub struct Path {
|
||||
pub anchor: PathAnchor,
|
||||
pub supers: usize,
|
||||
// Normalized path separated by `/`, with no `.` or `..` segments.
|
||||
pub raw_segments: Box<str>,
|
||||
pub raw_segments: SmolStr,
|
||||
}
|
||||
|
||||
impl Path {
|
||||
@ -159,7 +134,7 @@ pub enum PathAnchor {
|
||||
Relative(FileId),
|
||||
Absolute,
|
||||
Home,
|
||||
Search(Box<str>),
|
||||
Search(SmolStr),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
|
@ -1,11 +1,8 @@
|
||||
use super::{DefDatabase, Expr, ExprId, Module, Name, NameDefId};
|
||||
use super::{DefDatabase, Expr, ExprId, Module, NameDefId};
|
||||
use crate::base::FileId;
|
||||
use la_arena::{Arena, ArenaMap, Idx};
|
||||
use std::borrow::Borrow;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hash;
|
||||
use std::sync::Arc;
|
||||
use std::{iter, ops};
|
||||
use smol_str::SmolStr;
|
||||
use std::{collections::HashMap, iter, ops, sync::Arc};
|
||||
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ModuleScopes {
|
||||
@ -18,7 +15,7 @@ pub type ScopeId = Idx<ScopeData>;
|
||||
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ScopeData {
|
||||
parent: Option<ScopeId>,
|
||||
name_defs: HashMap<Name, NameDefId>,
|
||||
name_defs: HashMap<SmolStr, NameDefId>,
|
||||
}
|
||||
|
||||
impl ops::Index<ScopeId> for ModuleScopes {
|
||||
@ -37,17 +34,17 @@ impl ModuleScopes {
|
||||
Arc::new(this)
|
||||
}
|
||||
|
||||
pub(crate) fn lookup_name_query(
|
||||
pub(crate) fn resolve_name_query(
|
||||
db: &dyn DefDatabase,
|
||||
file_id: FileId,
|
||||
expr_id: ExprId,
|
||||
) -> Option<NameDefId> {
|
||||
let module = db.module(file_id);
|
||||
let name = match &module[expr_id] {
|
||||
Expr::Ident(name) => name,
|
||||
Expr::Reference(name) => name,
|
||||
_ => return None,
|
||||
};
|
||||
db.scopes(file_id).lookup(expr_id, name)
|
||||
db.scopes(file_id).resolve_anme(expr_id, name)
|
||||
}
|
||||
|
||||
pub fn scope_by_expr(&self, expr_id: ExprId) -> Option<ScopeId> {
|
||||
@ -58,9 +55,9 @@ impl ModuleScopes {
|
||||
iter::successors(Some(scope_id), |&i| self[i].parent).map(|i| &self[i])
|
||||
}
|
||||
|
||||
pub fn lookup(&self, expr_id: ExprId, name: &Name) -> Option<NameDefId> {
|
||||
pub fn resolve_anme(&self, expr_id: ExprId, name: &SmolStr) -> Option<NameDefId> {
|
||||
self.ancestors(self.scope_by_expr(expr_id)?)
|
||||
.find_map(|scope| scope.get(name))
|
||||
.find_map(|scope| scope.resolve(name))
|
||||
}
|
||||
|
||||
fn new_root_scope(&mut self) -> ScopeId {
|
||||
@ -70,7 +67,7 @@ impl ModuleScopes {
|
||||
})
|
||||
}
|
||||
|
||||
fn new_scope(&mut self, parent: ScopeId, name_defs: HashMap<Name, NameDefId>) -> ScopeId {
|
||||
fn new_scope(&mut self, parent: ScopeId, name_defs: HashMap<SmolStr, NameDefId>) -> ScopeId {
|
||||
self.scopes.alloc(ScopeData {
|
||||
parent: Some(parent),
|
||||
name_defs,
|
||||
@ -80,7 +77,7 @@ impl ModuleScopes {
|
||||
fn traverse_expr(&mut self, module: &Module, scope_id: ScopeId, expr_id: ExprId) {
|
||||
self.scope_by_expr.insert(expr_id, scope_id);
|
||||
match module[expr_id] {
|
||||
Expr::Missing | Expr::Ident(_) | Expr::Literal(_) => {}
|
||||
Expr::Missing | Expr::Reference(_) | Expr::Literal(_) => {}
|
||||
Expr::Apply(func, arg) => {
|
||||
self.traverse_expr(module, scope_id, func);
|
||||
self.traverse_expr(module, scope_id, arg);
|
||||
@ -117,11 +114,7 @@ impl ScopeData {
|
||||
self.name_defs.values().copied()
|
||||
}
|
||||
|
||||
pub fn get<Q>(&self, name: &Q) -> Option<NameDefId>
|
||||
where
|
||||
Q: Hash + Eq,
|
||||
Name: Borrow<Q>,
|
||||
{
|
||||
pub fn resolve(&self, name: &SmolStr) -> Option<NameDefId> {
|
||||
self.name_defs.get(name).copied()
|
||||
}
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ pub(crate) fn goto_definition(
|
||||
|
||||
let source_map = db.source_map(file_id);
|
||||
let expr_id = source_map.node_expr(AstPtr::new(node.syntax()))?;
|
||||
let def_id = db.lookup_name(file_id, expr_id)?;
|
||||
let def_id = db.resolve_name(file_id, expr_id)?;
|
||||
let def_node = source_map
|
||||
.name_def_node(def_id)?
|
||||
.to_node(&parse.syntax_node());
|
||||
|
Loading…
Reference in New Issue
Block a user