|
21 | 21 | //!
|
22 | 22 | //! This is a work of fiction. Any similarities to Kotlin's `BindingContext` are
|
23 | 23 | //! a coincidence.
|
24 |
| -pub mod keys; |
| 24 | +
|
| 25 | +pub mod keys { |
| 26 | + use std::marker::PhantomData; |
| 27 | + |
| 28 | + use hir_expand::{attrs::AttrId, MacroCallId}; |
| 29 | + use rustc_hash::FxHashMap; |
| 30 | + use syntax::{ast, AstNode, AstPtr}; |
| 31 | + |
| 32 | + use crate::{ |
| 33 | + dyn_map::{DynMap, Policy}, |
| 34 | + BlockId, ConstId, EnumId, EnumVariantId, ExternCrateId, FieldId, FunctionId, ImplId, |
| 35 | + LifetimeParamId, Macro2Id, MacroRulesId, ProcMacroId, StaticId, StructId, TraitAliasId, |
| 36 | + TraitId, TypeAliasId, TypeOrConstParamId, UnionId, UseId, |
| 37 | + }; |
| 38 | + |
| 39 | + pub type Key<K, V> = crate::dyn_map::Key<AstPtr<K>, V, AstPtrPolicy<K, V>>; |
| 40 | + |
| 41 | + pub const BLOCK: Key<ast::BlockExpr, BlockId> = Key::new(); |
| 42 | + pub const FUNCTION: Key<ast::Fn, FunctionId> = Key::new(); |
| 43 | + pub const CONST: Key<ast::Const, ConstId> = Key::new(); |
| 44 | + pub const STATIC: Key<ast::Static, StaticId> = Key::new(); |
| 45 | + pub const TYPE_ALIAS: Key<ast::TypeAlias, TypeAliasId> = Key::new(); |
| 46 | + pub const IMPL: Key<ast::Impl, ImplId> = Key::new(); |
| 47 | + pub const TRAIT: Key<ast::Trait, TraitId> = Key::new(); |
| 48 | + pub const TRAIT_ALIAS: Key<ast::TraitAlias, TraitAliasId> = Key::new(); |
| 49 | + pub const STRUCT: Key<ast::Struct, StructId> = Key::new(); |
| 50 | + pub const UNION: Key<ast::Union, UnionId> = Key::new(); |
| 51 | + pub const ENUM: Key<ast::Enum, EnumId> = Key::new(); |
| 52 | + pub const EXTERN_CRATE: Key<ast::ExternCrate, ExternCrateId> = Key::new(); |
| 53 | + pub const USE: Key<ast::Use, UseId> = Key::new(); |
| 54 | + |
| 55 | + pub const ENUM_VARIANT: Key<ast::Variant, EnumVariantId> = Key::new(); |
| 56 | + pub const TUPLE_FIELD: Key<ast::TupleField, FieldId> = Key::new(); |
| 57 | + pub const RECORD_FIELD: Key<ast::RecordField, FieldId> = Key::new(); |
| 58 | + pub const TYPE_PARAM: Key<ast::TypeParam, TypeOrConstParamId> = Key::new(); |
| 59 | + pub const CONST_PARAM: Key<ast::ConstParam, TypeOrConstParamId> = Key::new(); |
| 60 | + pub const LIFETIME_PARAM: Key<ast::LifetimeParam, LifetimeParamId> = Key::new(); |
| 61 | + |
| 62 | + pub const MACRO_RULES: Key<ast::MacroRules, MacroRulesId> = Key::new(); |
| 63 | + pub const MACRO2: Key<ast::MacroDef, Macro2Id> = Key::new(); |
| 64 | + pub const PROC_MACRO: Key<ast::Fn, ProcMacroId> = Key::new(); |
| 65 | + pub const MACRO_CALL: Key<ast::MacroCall, MacroCallId> = Key::new(); |
| 66 | + pub const ATTR_MACRO_CALL: Key<ast::Item, MacroCallId> = Key::new(); |
| 67 | + pub const DERIVE_MACRO_CALL: Key<ast::Attr, (AttrId, MacroCallId, Box<[Option<MacroCallId>]>)> = |
| 68 | + Key::new(); |
| 69 | + |
| 70 | + /// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are |
| 71 | + /// equal if they point to exactly the same object. |
| 72 | + /// |
| 73 | + /// In general, we do not guarantee that we have exactly one instance of a |
| 74 | + /// syntax tree for each file. We probably should add such guarantee, but, for |
| 75 | + /// the time being, we will use identity-less AstPtr comparison. |
| 76 | + pub struct AstPtrPolicy<AST, ID> { |
| 77 | + _phantom: PhantomData<(AST, ID)>, |
| 78 | + } |
| 79 | + |
| 80 | + impl<AST: AstNode + 'static, ID: 'static> Policy for AstPtrPolicy<AST, ID> { |
| 81 | + type K = AstPtr<AST>; |
| 82 | + type V = ID; |
| 83 | + fn insert(map: &mut DynMap, key: AstPtr<AST>, value: ID) { |
| 84 | + map.map |
| 85 | + .entry::<FxHashMap<AstPtr<AST>, ID>>() |
| 86 | + .or_insert_with(Default::default) |
| 87 | + .insert(key, value); |
| 88 | + } |
| 89 | + fn get<'a>(map: &'a DynMap, key: &AstPtr<AST>) -> Option<&'a ID> { |
| 90 | + map.map.get::<FxHashMap<AstPtr<AST>, ID>>()?.get(key) |
| 91 | + } |
| 92 | + fn is_empty(map: &DynMap) -> bool { |
| 93 | + map.map.get::<FxHashMap<AstPtr<AST>, ID>>().map_or(true, |it| it.is_empty()) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
25 | 97 |
|
26 | 98 | use std::{
|
27 | 99 | hash::Hash,
|
|
0 commit comments