aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/keys.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-08-13 15:28:27 +0100
committerAleksey Kladov <[email protected]>2020-08-13 15:29:33 +0100
commitb28c54a2c239acd73f2eea80fda9ee3960d2c046 (patch)
tree1bf0ea193bdb3b16ff42c2c01118b13a4276b2bb /crates/hir_def/src/keys.rs
parentb7aa4898e0841ab8199643f89a0caa967b698ca8 (diff)
Rename ra_hir_def -> hir_def
Diffstat (limited to 'crates/hir_def/src/keys.rs')
-rw-r--r--crates/hir_def/src/keys.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/crates/hir_def/src/keys.rs b/crates/hir_def/src/keys.rs
new file mode 100644
index 000000000..40a5d92b5
--- /dev/null
+++ b/crates/hir_def/src/keys.rs
@@ -0,0 +1,58 @@
1//! keys to be used with `DynMap`
2
3use std::marker::PhantomData;
4
5use hir_expand::{InFile, MacroDefId};
6use rustc_hash::FxHashMap;
7use syntax::{ast, AstNode, AstPtr};
8
9use crate::{
10 dyn_map::{DynMap, Policy},
11 ConstId, EnumId, EnumVariantId, FieldId, FunctionId, ImplId, StaticId, StructId, TraitId,
12 TypeAliasId, TypeParamId, UnionId,
13};
14
15pub type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
16
17pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new();
18pub const CONST: Key<ast::Const, ConstId> = Key::new();
19pub const STATIC: Key<ast::Static, StaticId> = Key::new();
20pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new();
21pub const IMPL: Key<ast::Impl, ImplId> = Key::new();
22pub const TRAIT: Key<ast::Trait, TraitId> = Key::new();
23pub const STRUCT: Key<ast::Struct, StructId> = Key::new();
24pub const UNION: Key<ast::Union, UnionId> = Key::new();
25pub const ENUM: Key<ast::Enum, EnumId> = Key::new();
26
27pub const VARIANT: Key<ast::Variant, EnumVariantId> = Key::new();
28pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new();
29pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new();
30pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
31
32pub const MACRO: Key<ast::MacroCall, MacroDefId> = Key::new();
33
34/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
35/// equal if they point to exactly the same object.
36///
37/// In general, we do not guarantee that we have exactly one instance of a
38/// syntax tree for each file. We probably should add such guarantee, but, for
39/// the time being, we will use identity-less AstPtr comparison.
40pub struct AstPtrPolicy<AST, ID> {
41 _phantom: PhantomData<(AST, ID)>,
42}
43
44impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> {
45 type K = InFile<AST>;
46 type V = ID;
47 fn insert(map: &mut DynMap, key: InFile<AST>, value: ID) {
48 let key = key.as_ref().map(AstPtr::new);
49 map.map
50 .entry::<FxHashMap<InFile<AstPtr<AST>>, ID>>()
51 .or_insert_with(Default::default)
52 .insert(key, value);
53 }
54 fn get<'a>(map: &'a DynMap, key: &InFile<AST>) -> Option<&'a ID> {
55 let key = key.as_ref().map(AstPtr::new);
56 map.map.get::<FxHashMap<InFile<AstPtr<AST>>, ID>>()?.get(&key)
57 }
58}