From b28c54a2c239acd73f2eea80fda9ee3960d2c046 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 16:28:27 +0200 Subject: Rename ra_hir_def -> hir_def --- crates/hir_def/src/keys.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 crates/hir_def/src/keys.rs (limited to 'crates/hir_def/src/keys.rs') 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 @@ +//! keys to be used with `DynMap` + +use std::marker::PhantomData; + +use hir_expand::{InFile, MacroDefId}; +use rustc_hash::FxHashMap; +use syntax::{ast, AstNode, AstPtr}; + +use crate::{ + dyn_map::{DynMap, Policy}, + ConstId, EnumId, EnumVariantId, FieldId, FunctionId, ImplId, StaticId, StructId, TraitId, + TypeAliasId, TypeParamId, UnionId, +}; + +pub type Key = crate::dyn_map::Key, V, AstPtrPolicy>; + +pub const FUNCTION: Key = Key::new(); +pub const CONST: Key = Key::new(); +pub const STATIC: Key = Key::new(); +pub const TYPE_ALIAS: Key = Key::new(); +pub const IMPL: Key = Key::new(); +pub const TRAIT: Key = Key::new(); +pub const STRUCT: Key = Key::new(); +pub const UNION: Key = Key::new(); +pub const ENUM: Key = Key::new(); + +pub const VARIANT: Key = Key::new(); +pub const TUPLE_FIELD: Key = Key::new(); +pub const RECORD_FIELD: Key = Key::new(); +pub const TYPE_PARAM: Key = Key::new(); + +pub const MACRO: Key = Key::new(); + +/// 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 { + _phantom: PhantomData<(AST, ID)>, +} + +impl Policy for AstPtrPolicy { + type K = InFile; + type V = ID; + fn insert(map: &mut DynMap, key: InFile, value: ID) { + let key = key.as_ref().map(AstPtr::new); + map.map + .entry::>, ID>>() + .or_insert_with(Default::default) + .insert(key, value); + } + fn get<'a>(map: &'a DynMap, key: &InFile) -> Option<&'a ID> { + let key = key.as_ref().map(AstPtr::new); + map.map.get::>, ID>>()?.get(&key) + } +} -- cgit v1.2.3