1
Fork 0

Factor out ITEM_REFS

This commit is contained in:
Cameron Steffen 2022-10-26 16:18:39 -05:00
parent ebfa1f0185
commit f808430497
2 changed files with 21 additions and 20 deletions

View file

@ -12,14 +12,11 @@ use crate::errors::LangItemError;
use crate::{MethodKind, Target};
use rustc_ast as ast;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_macros::HashStable_Generic;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;
use std::sync::LazyLock;
pub enum LangItemGroup {
Op,
Fn,
@ -104,6 +101,14 @@ macro_rules! language_item_table {
}
}
/// Opposite of [`LangItem::name`]
pub fn from_name(name: Symbol) -> Option<Self> {
match name {
$( $module::$name => Some(LangItem::$variant), )*
_ => None,
}
}
/// The [group](LangItemGroup) that this lang item belongs to,
/// or `None` if it doesn't belong to a group.
pub fn group(self) -> Option<LangItemGroup> {
@ -113,6 +118,12 @@ macro_rules! language_item_table {
}
}
pub fn target(self) -> Target {
match self {
$( LangItem::$variant => $target, )*
}
}
pub fn required_generics(&self) -> GenericRequirement {
match self {
$( LangItem::$variant => $generics, )*
@ -145,15 +156,6 @@ macro_rules! language_item_table {
}
)*
}
/// A mapping from the name of the lang item to its order and the form it must be of.
pub static ITEM_REFS: LazyLock<FxIndexMap<Symbol, (usize, Target)>> = LazyLock::new(|| {
let mut item_refs = FxIndexMap::default();
$( item_refs.insert($module::$name, (LangItem::$variant as usize, $target)); )*
item_refs
});
// End of the macro
}
}