remove ast::*Kind from hir

This commit is contained in:
Aleksey Kladov 2019-08-19 14:04:51 +03:00
parent 39e444d701
commit b50a04827c
6 changed files with 86 additions and 83 deletions

View file

@ -602,8 +602,8 @@ where
fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
let syntax_ptr = SyntaxNodePtr::new(expr.syntax());
match expr.kind() {
ast::ExprKind::IfExpr(e) => {
match expr {
ast::Expr::IfExpr(e) => {
let then_branch = self.collect_block_opt(e.then_branch());
let else_branch = e.else_branch().map(|b| match b {
@ -639,16 +639,16 @@ where
self.alloc_expr(Expr::If { condition, then_branch, else_branch }, syntax_ptr)
}
ast::ExprKind::TryBlockExpr(e) => {
ast::Expr::TryBlockExpr(e) => {
let body = self.collect_block_opt(e.try_body());
self.alloc_expr(Expr::TryBlock { body }, syntax_ptr)
}
ast::ExprKind::BlockExpr(e) => self.collect_block_opt(e.block()),
ast::ExprKind::LoopExpr(e) => {
ast::Expr::BlockExpr(e) => self.collect_block_opt(e.block()),
ast::Expr::LoopExpr(e) => {
let body = self.collect_block_opt(e.loop_body());
self.alloc_expr(Expr::Loop { body }, syntax_ptr)
}
ast::ExprKind::WhileExpr(e) => {
ast::Expr::WhileExpr(e) => {
let body = self.collect_block_opt(e.loop_body());
let condition = match e.condition() {
@ -675,13 +675,13 @@ where
self.alloc_expr(Expr::While { condition, body }, syntax_ptr)
}
ast::ExprKind::ForExpr(e) => {
ast::Expr::ForExpr(e) => {
let iterable = self.collect_expr_opt(e.iterable());
let pat = self.collect_pat_opt(e.pat());
let body = self.collect_block_opt(e.loop_body());
self.alloc_expr(Expr::For { iterable, pat, body }, syntax_ptr)
}
ast::ExprKind::CallExpr(e) => {
ast::Expr::CallExpr(e) => {
let callee = self.collect_expr_opt(e.expr());
let args = if let Some(arg_list) = e.arg_list() {
arg_list.args().map(|e| self.collect_expr(e)).collect()
@ -690,7 +690,7 @@ where
};
self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
}
ast::ExprKind::MethodCallExpr(e) => {
ast::Expr::MethodCallExpr(e) => {
let receiver = self.collect_expr_opt(e.expr());
let args = if let Some(arg_list) = e.arg_list() {
arg_list.args().map(|e| self.collect_expr(e)).collect()
@ -704,7 +704,7 @@ where
syntax_ptr,
)
}
ast::ExprKind::MatchExpr(e) => {
ast::Expr::MatchExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
let arms = if let Some(match_arm_list) = e.match_arm_list() {
match_arm_list
@ -723,30 +723,30 @@ where
};
self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
}
ast::ExprKind::PathExpr(e) => {
ast::Expr::PathExpr(e) => {
let path =
e.path().and_then(Path::from_ast).map(Expr::Path).unwrap_or(Expr::Missing);
self.alloc_expr(path, syntax_ptr)
}
ast::ExprKind::ContinueExpr(_e) => {
ast::Expr::ContinueExpr(_e) => {
// FIXME: labels
self.alloc_expr(Expr::Continue, syntax_ptr)
}
ast::ExprKind::BreakExpr(e) => {
ast::Expr::BreakExpr(e) => {
let expr = e.expr().map(|e| self.collect_expr(e));
self.alloc_expr(Expr::Break { expr }, syntax_ptr)
}
ast::ExprKind::ParenExpr(e) => {
ast::Expr::ParenExpr(e) => {
let inner = self.collect_expr_opt(e.expr());
// make the paren expr point to the inner expression as well
self.source_map.expr_map.insert(syntax_ptr, inner);
inner
}
ast::ExprKind::ReturnExpr(e) => {
ast::Expr::ReturnExpr(e) => {
let expr = e.expr().map(|e| self.collect_expr(e));
self.alloc_expr(Expr::Return { expr }, syntax_ptr)
}
ast::ExprKind::StructLit(e) => {
ast::Expr::StructLit(e) => {
let path = e.path().and_then(Path::from_ast);
let mut field_ptrs = Vec::new();
let struct_lit = if let Some(nfl) = e.named_field_list() {
@ -787,7 +787,7 @@ where
}
res
}
ast::ExprKind::FieldExpr(e) => {
ast::Expr::FieldExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
let name = match e.field_access() {
Some(kind) => kind.as_name(),
@ -795,25 +795,25 @@ where
};
self.alloc_expr(Expr::Field { expr, name }, syntax_ptr)
}
ast::ExprKind::AwaitExpr(e) => {
ast::Expr::AwaitExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
self.alloc_expr(Expr::Await { expr }, syntax_ptr)
}
ast::ExprKind::TryExpr(e) => {
ast::Expr::TryExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
self.alloc_expr(Expr::Try { expr }, syntax_ptr)
}
ast::ExprKind::CastExpr(e) => {
ast::Expr::CastExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
let type_ref = TypeRef::from_ast_opt(e.type_ref());
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
}
ast::ExprKind::RefExpr(e) => {
ast::Expr::RefExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
let mutability = Mutability::from_mutable(e.is_mut());
self.alloc_expr(Expr::Ref { expr, mutability }, syntax_ptr)
}
ast::ExprKind::PrefixExpr(e) => {
ast::Expr::PrefixExpr(e) => {
let expr = self.collect_expr_opt(e.expr());
if let Some(op) = e.op_kind() {
self.alloc_expr(Expr::UnaryOp { expr, op }, syntax_ptr)
@ -821,7 +821,7 @@ where
self.alloc_expr(Expr::Missing, syntax_ptr)
}
}
ast::ExprKind::LambdaExpr(e) => {
ast::Expr::LambdaExpr(e) => {
let mut args = Vec::new();
let mut arg_types = Vec::new();
if let Some(pl) = e.param_list() {
@ -835,18 +835,18 @@ where
let body = self.collect_expr_opt(e.body());
self.alloc_expr(Expr::Lambda { args, arg_types, body }, syntax_ptr)
}
ast::ExprKind::BinExpr(e) => {
ast::Expr::BinExpr(e) => {
let lhs = self.collect_expr_opt(e.lhs());
let rhs = self.collect_expr_opt(e.rhs());
let op = e.op_kind().map(BinaryOp::from);
self.alloc_expr(Expr::BinaryOp { lhs, rhs, op }, syntax_ptr)
}
ast::ExprKind::TupleExpr(e) => {
ast::Expr::TupleExpr(e) => {
let exprs = e.exprs().map(|expr| self.collect_expr(expr)).collect();
self.alloc_expr(Expr::Tuple { exprs }, syntax_ptr)
}
ast::ExprKind::ArrayExpr(e) => {
ast::Expr::ArrayExpr(e) => {
let kind = e.kind();
match kind {
@ -865,7 +865,7 @@ where
}
}
ast::ExprKind::Literal(e) => {
ast::Expr::Literal(e) => {
let lit = match e.kind() {
LiteralKind::IntNumber { suffix } => {
let known_name = suffix
@ -895,16 +895,16 @@ where
};
self.alloc_expr(Expr::Literal(lit), syntax_ptr)
}
ast::ExprKind::IndexExpr(e) => {
ast::Expr::IndexExpr(e) => {
let base = self.collect_expr_opt(e.base());
let index = self.collect_expr_opt(e.index());
self.alloc_expr(Expr::Index { base, index }, syntax_ptr)
}
// FIXME implement HIR for these:
ast::ExprKind::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::ExprKind::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::ExprKind::MacroCall(e) => {
ast::Expr::Label(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::Expr::RangeExpr(_e) => self.alloc_expr(Expr::Missing, syntax_ptr),
ast::Expr::MacroCall(e) => {
let ast_id = self
.db
.ast_id_map(self.current_file_id)
@ -945,16 +945,14 @@ where
fn collect_block(&mut self, block: ast::Block) -> ExprId {
let statements = block
.statements()
.map(|s| match s.kind() {
ast::StmtKind::LetStmt(stmt) => {
.map(|s| match s {
ast::Stmt::LetStmt(stmt) => {
let pat = self.collect_pat_opt(stmt.pat());
let type_ref = stmt.ascribed_type().map(TypeRef::from_ast);
let initializer = stmt.initializer().map(|e| self.collect_expr(e));
Statement::Let { pat, type_ref, initializer }
}
ast::StmtKind::ExprStmt(stmt) => {
Statement::Expr(self.collect_expr_opt(stmt.expr()))
}
ast::Stmt::ExprStmt(stmt) => Statement::Expr(self.collect_expr_opt(stmt.expr())),
})
.collect();
let tail = block.expr().map(|e| self.collect_expr(e));
@ -970,33 +968,33 @@ where
}
fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
let pattern = match pat.kind() {
ast::PatKind::BindPat(bp) => {
let pattern = match &pat {
ast::Pat::BindPat(bp) => {
let name = bp.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
let annotation = BindingAnnotation::new(bp.is_mutable(), bp.is_ref());
let subpat = bp.pat().map(|subpat| self.collect_pat(subpat));
Pat::Bind { name, mode: annotation, subpat }
}
ast::PatKind::TupleStructPat(p) => {
ast::Pat::TupleStructPat(p) => {
let path = p.path().and_then(Path::from_ast);
let args = p.args().map(|p| self.collect_pat(p)).collect();
Pat::TupleStruct { path, args }
}
ast::PatKind::RefPat(p) => {
ast::Pat::RefPat(p) => {
let pat = self.collect_pat_opt(p.pat());
let mutability = Mutability::from_mutable(p.is_mut());
Pat::Ref { pat, mutability }
}
ast::PatKind::PathPat(p) => {
ast::Pat::PathPat(p) => {
let path = p.path().and_then(Path::from_ast);
path.map(Pat::Path).unwrap_or(Pat::Missing)
}
ast::PatKind::TuplePat(p) => {
ast::Pat::TuplePat(p) => {
let args = p.args().map(|p| self.collect_pat(p)).collect();
Pat::Tuple(args)
}
ast::PatKind::PlaceholderPat(_) => Pat::Wild,
ast::PatKind::StructPat(p) => {
ast::Pat::PlaceholderPat(_) => Pat::Wild,
ast::Pat::StructPat(p) => {
let path = p.path().and_then(Path::from_ast);
let field_pat_list =
p.field_pat_list().expect("every struct should have a field list");
@ -1022,8 +1020,8 @@ where
}
// FIXME: implement
ast::PatKind::LiteralPat(_) => Pat::Missing,
ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing,
ast::Pat::LiteralPat(_) => Pat::Missing,
ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing,
};
let ptr = AstPtr::new(&pat);
self.alloc_pat(pattern, Either::A(ptr))

View file

@ -137,8 +137,8 @@ impl GenericParams {
fn add_where_predicate_from_bound(&mut self, bound: ast::TypeBound, type_ref: TypeRef) {
let path = bound
.type_ref()
.and_then(|tr| match tr.kind() {
ast::TypeRefKind::PathType(path) => path.path(),
.and_then(|tr| match tr {
ast::TypeRef::PathType(path) => path.path(),
_ => None,
})
.and_then(Path::from_ast);

View file

@ -131,10 +131,10 @@ impl ImplData {
let items = if let Some(item_list) = node.item_list() {
item_list
.impl_items()
.map(|item_node| match item_node.kind() {
ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
.map(|item_node| match item_node {
ast::ImplItem::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
ast::ImplItem::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
ast::ImplItem::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
})
.collect()
} else {

View file

@ -207,24 +207,24 @@ impl RawItemsCollector {
}
fn add_item(&mut self, current_module: Option<Module>, item: ast::ModuleItem) {
let (kind, name) = match item.kind() {
ast::ModuleItemKind::Module(module) => {
let (kind, name) = match item {
ast::ModuleItem::Module(module) => {
self.add_module(current_module, module);
return;
}
ast::ModuleItemKind::UseItem(use_item) => {
ast::ModuleItem::UseItem(use_item) => {
self.add_use_item(current_module, use_item);
return;
}
ast::ModuleItemKind::ExternCrateItem(extern_crate) => {
ast::ModuleItem::ExternCrateItem(extern_crate) => {
self.add_extern_crate_item(current_module, extern_crate);
return;
}
ast::ModuleItemKind::ImplBlock(_) => {
ast::ModuleItem::ImplBlock(_) => {
// impls don't participate in name resolution
return;
}
ast::ModuleItemKind::StructDef(it) => {
ast::ModuleItem::StructDef(it) => {
let id = self.source_ast_id_map.ast_id(&it);
let name = it.name();
if it.is_union() {
@ -233,22 +233,22 @@ impl RawItemsCollector {
(DefKind::Struct(id), name)
}
}
ast::ModuleItemKind::EnumDef(it) => {
ast::ModuleItem::EnumDef(it) => {
(DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::FnDef(it) => {
ast::ModuleItem::FnDef(it) => {
(DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::TraitDef(it) => {
ast::ModuleItem::TraitDef(it) => {
(DefKind::Trait(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::TypeAliasDef(it) => {
ast::ModuleItem::TypeAliasDef(it) => {
(DefKind::TypeAlias(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::ConstDef(it) => {
ast::ModuleItem::ConstDef(it) => {
(DefKind::Const(self.source_ast_id_map.ast_id(&it)), it.name())
}
ast::ModuleItemKind::StaticDef(it) => {
ast::ModuleItem::StaticDef(it) => {
(DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name())
}
};

View file

@ -30,10 +30,10 @@ impl TraitData {
let items = if let Some(item_list) = src.ast.item_list() {
item_list
.impl_items()
.map(|item_node| match item_node.kind() {
ast::ImplItemKind::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
ast::ImplItemKind::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
.map(|item_node| match item_node {
ast::ImplItem::FnDef(it) => Function { id: ctx.to_def(&it) }.into(),
ast::ImplItem::ConstDef(it) => Const { id: ctx.to_def(&it) }.into(),
ast::ImplItem::TypeAliasDef(it) => TypeAlias { id: ctx.to_def(&it) }.into(),
})
.collect()
} else {

View file

@ -57,28 +57,33 @@ pub enum TypeRef {
impl TypeRef {
/// Converts an `ast::TypeRef` to a `hir::TypeRef`.
pub(crate) fn from_ast(node: ast::TypeRef) -> Self {
use ra_syntax::ast::TypeRefKind::*;
match node.kind() {
ParenType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
TupleType(inner) => TypeRef::Tuple(inner.fields().map(TypeRef::from_ast).collect()),
NeverType(..) => TypeRef::Never,
PathType(inner) => {
match node {
ast::TypeRef::ParenType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
ast::TypeRef::TupleType(inner) => {
TypeRef::Tuple(inner.fields().map(TypeRef::from_ast).collect())
}
ast::TypeRef::NeverType(..) => TypeRef::Never,
ast::TypeRef::PathType(inner) => {
inner.path().and_then(Path::from_ast).map(TypeRef::Path).unwrap_or(TypeRef::Error)
}
PointerType(inner) => {
ast::TypeRef::PointerType(inner) => {
let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
let mutability = Mutability::from_mutable(inner.is_mut());
TypeRef::RawPtr(Box::new(inner_ty), mutability)
}
ArrayType(inner) => TypeRef::Array(Box::new(TypeRef::from_ast_opt(inner.type_ref()))),
SliceType(inner) => TypeRef::Slice(Box::new(TypeRef::from_ast_opt(inner.type_ref()))),
ReferenceType(inner) => {
ast::TypeRef::ArrayType(inner) => {
TypeRef::Array(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
}
ast::TypeRef::SliceType(inner) => {
TypeRef::Slice(Box::new(TypeRef::from_ast_opt(inner.type_ref())))
}
ast::TypeRef::ReferenceType(inner) => {
let inner_ty = TypeRef::from_ast_opt(inner.type_ref());
let mutability = Mutability::from_mutable(inner.is_mut());
TypeRef::Reference(Box::new(inner_ty), mutability)
}
PlaceholderType(_inner) => TypeRef::Placeholder,
FnPointerType(inner) => {
ast::TypeRef::PlaceholderType(_inner) => TypeRef::Placeholder,
ast::TypeRef::FnPointerType(inner) => {
let ret_ty = TypeRef::from_ast_opt(inner.ret_type().and_then(|rt| rt.type_ref()));
let mut params = if let Some(pl) = inner.param_list() {
pl.params().map(|p| p.ascribed_type()).map(TypeRef::from_ast_opt).collect()
@ -89,9 +94,9 @@ impl TypeRef {
TypeRef::Fn(params)
}
// for types are close enough for our purposes to the inner type for now...
ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
ImplTraitType(_inner) => TypeRef::Error,
DynTraitType(_inner) => TypeRef::Error,
ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(inner.type_ref()),
ast::TypeRef::ImplTraitType(_inner) => TypeRef::Error,
ast::TypeRef::DynTraitType(_inner) => TypeRef::Error,
}
}