• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! keys to be used with `DynMap`
2 
3 use std::marker::PhantomData;
4 
5 use hir_expand::{attrs::AttrId, MacroCallId};
6 use rustc_hash::FxHashMap;
7 use syntax::{ast, AstNode, AstPtr};
8 
9 use crate::{
10     dyn_map::{DynMap, Policy},
11     ConstId, EnumId, EnumVariantId, FieldId, FunctionId, ImplId, LifetimeParamId, Macro2Id,
12     MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId, TraitId, TypeAliasId,
13     TypeOrConstParamId, UnionId,
14 };
15 
16 pub type Key<K, V> = crate::dyn_map::Key<K, V, AstPtrPolicy<K, V>>;
17 
18 pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new();
19 pub const CONST: Key<ast::Const, ConstId> = Key::new();
20 pub const STATIC: Key<ast::Static, StaticId> = Key::new();
21 pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
22 pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
23 pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
24 pub const TRAIT_ALIAS: Key<ast::TraitAlias, TraitAliasId> = Key::new();
25 pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
26 pub const UNION: Key<ast::Union, UnionId> = Key::new();
27 pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
28 
29 pub const VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
30 pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();
31 pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new();
32 pub const TYPE_PARAM: Key<ast::TypeParam, TypeOrConstParamId> = Key::new();
33 pub const CONST_PARAM: Key<ast::ConstParam, TypeOrConstParamId> = Key::new();
34 pub const LIFETIME_PARAM: Key<ast::LifetimeParam, LifetimeParamId> = Key::new();
35 
36 pub const MACRO_RULES: Key<ast::MacroRules, MacroRulesId> = Key::new();
37 pub const MACRO2: Key<ast::MacroDef, Macro2Id> = Key::new();
38 pub const PROC_MACRO: Key<ast::Fn, ProcMacroId> = Key::new();
39 pub const ATTR_MACRO_CALL: Key<ast::Item, MacroCallId> = Key::new();
40 pub const DERIVE_MACRO_CALL: Key<ast::Attr, (AttrId, MacroCallId, Box<[Option<MacroCallId>]>)> =
41     Key::new();
42 
43 /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
44 /// equal if they point to exactly the same object.
45 ///
46 /// In general, we do not guarantee that we have exactly one instance of a
47 /// syntax tree for each file. We probably should add such guarantee, but, for
48 /// the time being, we will use identity-less AstPtr comparison.
49 pub struct AstPtrPolicy<AST, ID> {
50     _phantom: PhantomData<(AST, ID)>,
51 }
52 
53 impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
54     type K = AST;
55     type V = ID;
insert(map: &mut DynMap, key: AST, value: ID)56     fn insert(map: &mut DynMap, key: AST, value: ID) {
57         let key = AstPtr::new(&key);
58         map.map
59             .entry::<FxHashMap<AstPtr<AST>, ID>>()
60             .or_insert_with(Default::default)
61             .insert(key, value);
62     }
get<'a>(map: &'a DynMap, key: &AST) -> Option<&'a ID>63     fn get<'a>(map: &'a DynMap, key: &AST) -> Option<&'a ID> {
64         let key = AstPtr::new(key);
65         map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(&key)
66     }
is_empty(map: &DynMap) -> bool67     fn is_empty(map: &DynMap) -> bool {
68         map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty())
69     }
70 }
71