2019-12-05 16:34:12 -06:00
|
|
|
//! keys to be used with `DynMap`
|
|
|
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2022-01-08 05:13:45 -06:00
|
|
|
use hir_expand::{MacroCallId, MacroDefId};
|
2019-12-05 16:34:12 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{ast, AstNode, AstPtr};
|
2019-12-05 16:34:12 -06:00
|
|
|
|
|
|
|
use crate::{
|
2022-01-07 07:19:11 -06:00
|
|
|
attr::AttrId,
|
2019-12-05 16:34:12 -06:00
|
|
|
dyn_map::{DynMap, Policy},
|
2021-01-01 03:06:42 -06:00
|
|
|
ConstId, ConstParamId, EnumId, EnumVariantId, FieldId, FunctionId, ImplId, LifetimeParamId,
|
|
|
|
StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
|
2019-12-05 16:34:12 -06:00
|
|
|
};
|
|
|
|
|
2022-01-08 05:13:45 -06:00
|
|
|
pub type Key<K, V> = crate::dyn_map::Key<K, V, AstPtrPolicy<K, V>>;
|
2019-12-05 16:34:12 -06:00
|
|
|
|
2020-07-30 07:51:08 -05:00
|
|
|
pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new();
|
2020-07-30 11:02:20 -05:00
|
|
|
pub const CONST: Key<ast::Const, ConstId> = Key::new();
|
|
|
|
pub const STATIC: Key<ast::Static, StaticId> = Key::new();
|
2020-07-30 08:25:46 -05:00
|
|
|
pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
|
2020-07-30 11:28:28 -05:00
|
|
|
pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
|
2020-07-30 11:17:28 -05:00
|
|
|
pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
|
2020-07-30 10:50:40 -05:00
|
|
|
pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
|
2020-07-30 10:36:46 -05:00
|
|
|
pub const UNION: Key<ast::Union, UnionId> = Key::new();
|
2020-07-30 10:52:53 -05:00
|
|
|
pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
|
2019-12-12 07:09:13 -06:00
|
|
|
|
2020-07-30 10:56:53 -05:00
|
|
|
pub const VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
|
2020-07-30 09:49:13 -05:00
|
|
|
pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();
|
|
|
|
pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new();
|
2019-12-07 13:09:53 -06:00
|
|
|
pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
|
2020-12-13 15:13:16 -06:00
|
|
|
pub const LIFETIME_PARAM: Key<ast::LifetimeParam, LifetimeParamId> = Key::new();
|
2021-01-01 03:06:42 -06:00
|
|
|
pub const CONST_PARAM: Key<ast::ConstParam, ConstParamId> = Key::new();
|
2020-01-16 09:08:46 -06:00
|
|
|
|
2022-01-08 03:45:12 -06:00
|
|
|
pub const MACRO: Key<ast::Macro, MacroDefId> = Key::new();
|
2022-01-07 07:19:11 -06:00
|
|
|
pub const ATTR_MACRO_CALL: Key<ast::Item, MacroCallId> = Key::new();
|
2022-02-20 19:42:58 -06:00
|
|
|
pub const DERIVE_MACRO_CALL: Key<ast::Attr, (AttrId, MacroCallId, Box<[Option<MacroCallId>]>)> =
|
|
|
|
Key::new();
|
2019-12-05 16:34:12 -06:00
|
|
|
|
|
|
|
/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
|
|
|
|
/// equal if they point to exactly the same object.
|
|
|
|
///
|
|
|
|
/// In general, we do not guarantee that we have exactly one instance of a
|
|
|
|
/// syntax tree for each file. We probably should add such guarantee, but, for
|
|
|
|
/// the time being, we will use identity-less AstPtr comparison.
|
|
|
|
pub struct AstPtrPolicy<AST, ID> {
|
|
|
|
_phantom: PhantomData<(AST, ID)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
|
2022-01-08 05:13:45 -06:00
|
|
|
type K = AST;
|
2019-12-05 16:34:12 -06:00
|
|
|
type V = ID;
|
2022-01-08 05:13:45 -06:00
|
|
|
fn insert(map: &mut DynMap, key: AST, value: ID) {
|
|
|
|
let key = AstPtr::new(&key);
|
2019-12-05 16:34:12 -06:00
|
|
|
map.map
|
2022-01-08 05:13:45 -06:00
|
|
|
.entry::<FxHashMap<AstPtr<AST>, ID>>()
|
2019-12-05 16:34:12 -06:00
|
|
|
.or_insert_with(Default::default)
|
|
|
|
.insert(key, value);
|
|
|
|
}
|
2022-01-08 05:13:45 -06:00
|
|
|
fn get<'a>(map: &'a DynMap, key: &AST) -> Option<&'a ID> {
|
|
|
|
let key = AstPtr::new(key);
|
|
|
|
map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(&key)
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|
2022-02-21 06:21:25 -06:00
|
|
|
fn is_empty(map: &DynMap) -> bool {
|
|
|
|
map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty())
|
|
|
|
}
|
2019-12-05 16:34:12 -06:00
|
|
|
}
|