rust-analyzer/crates/hir-ty/src/lang_items.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
3.9 KiB
Rust
Raw Normal View History

2022-10-23 08:12:05 +00:00
//! Functions to detect special lang items
use hir_def::{data::adt::StructFlags, lang_item::LangItem, AdtId};
2023-03-29 21:33:39 +00:00
use hir_expand::name::Name;
2024-07-12 13:57:54 +00:00
use intern::sym;
2022-10-23 08:12:05 +00:00
use crate::db::HirDatabase;
2023-03-30 07:37:52 +00:00
pub fn is_box(db: &dyn HirDatabase, adt: AdtId) -> bool {
let AdtId::StructId(id) = adt else { return false };
2023-04-23 18:25:47 +00:00
db.struct_data(id).flags.contains(StructFlags::IS_BOX)
2022-10-23 08:12:05 +00:00
}
2023-03-30 07:37:52 +00:00
pub fn is_unsafe_cell(db: &dyn HirDatabase, adt: AdtId) -> bool {
let AdtId::StructId(id) = adt else { return false };
db.struct_data(id).flags.contains(StructFlags::IS_UNSAFE_CELL)
2022-10-23 08:12:05 +00:00
}
2023-03-29 21:33:39 +00:00
pub fn lang_items_for_bin_op(op: syntax::ast::BinaryOp) -> Option<(Name, LangItem)> {
use syntax::ast::{ArithOp, BinaryOp, CmpOp, Ordering};
Some(match op {
BinaryOp::LogicOp(_) => return None,
BinaryOp::ArithOp(aop) => match aop {
ArithOp::Add => (Name::new_symbol_root(sym::add.clone()), LangItem::Add),
ArithOp::Mul => (Name::new_symbol_root(sym::mul.clone()), LangItem::Mul),
ArithOp::Sub => (Name::new_symbol_root(sym::sub.clone()), LangItem::Sub),
ArithOp::Div => (Name::new_symbol_root(sym::div.clone()), LangItem::Div),
ArithOp::Rem => (Name::new_symbol_root(sym::rem.clone()), LangItem::Rem),
ArithOp::Shl => (Name::new_symbol_root(sym::shl.clone()), LangItem::Shl),
ArithOp::Shr => (Name::new_symbol_root(sym::shr.clone()), LangItem::Shr),
ArithOp::BitXor => (Name::new_symbol_root(sym::bitxor.clone()), LangItem::BitXor),
ArithOp::BitOr => (Name::new_symbol_root(sym::bitor.clone()), LangItem::BitOr),
ArithOp::BitAnd => (Name::new_symbol_root(sym::bitand.clone()), LangItem::BitAnd),
2023-03-29 21:33:39 +00:00
},
BinaryOp::Assignment { op: Some(aop) } => match aop {
ArithOp::Add => (Name::new_symbol_root(sym::add_assign.clone()), LangItem::AddAssign),
ArithOp::Mul => (Name::new_symbol_root(sym::mul_assign.clone()), LangItem::MulAssign),
ArithOp::Sub => (Name::new_symbol_root(sym::sub_assign.clone()), LangItem::SubAssign),
ArithOp::Div => (Name::new_symbol_root(sym::div_assign.clone()), LangItem::DivAssign),
ArithOp::Rem => (Name::new_symbol_root(sym::rem_assign.clone()), LangItem::RemAssign),
ArithOp::Shl => (Name::new_symbol_root(sym::shl_assign.clone()), LangItem::ShlAssign),
ArithOp::Shr => (Name::new_symbol_root(sym::shr_assign.clone()), LangItem::ShrAssign),
ArithOp::BitXor => {
(Name::new_symbol_root(sym::bitxor_assign.clone()), LangItem::BitXorAssign)
}
ArithOp::BitOr => {
(Name::new_symbol_root(sym::bitor_assign.clone()), LangItem::BitOrAssign)
}
ArithOp::BitAnd => {
(Name::new_symbol_root(sym::bitand_assign.clone()), LangItem::BitAndAssign)
}
2023-03-29 21:33:39 +00:00
},
BinaryOp::CmpOp(cop) => match cop {
CmpOp::Eq { negated: false } => {
(Name::new_symbol_root(sym::eq.clone()), LangItem::PartialEq)
}
CmpOp::Eq { negated: true } => {
(Name::new_symbol_root(sym::ne.clone()), LangItem::PartialEq)
}
2023-03-29 21:33:39 +00:00
CmpOp::Ord { ordering: Ordering::Less, strict: false } => {
(Name::new_symbol_root(sym::le.clone()), LangItem::PartialOrd)
2023-03-29 21:33:39 +00:00
}
CmpOp::Ord { ordering: Ordering::Less, strict: true } => {
(Name::new_symbol_root(sym::lt.clone()), LangItem::PartialOrd)
2023-03-29 21:33:39 +00:00
}
CmpOp::Ord { ordering: Ordering::Greater, strict: false } => {
(Name::new_symbol_root(sym::ge.clone()), LangItem::PartialOrd)
2023-03-29 21:33:39 +00:00
}
CmpOp::Ord { ordering: Ordering::Greater, strict: true } => {
(Name::new_symbol_root(sym::gt.clone()), LangItem::PartialOrd)
2023-03-29 21:33:39 +00:00
}
},
BinaryOp::Assignment { op: None } => return None,
})
}