diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-30 16:17:49 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-30 16:17:49 +0000 |
commit | d929f9c49bceb3b7c32ea45c5e55c42f168bbf34 (patch) | |
tree | 3f0f71a7b9406738b1d15d53970e76302ac624c4 /crates/ra_hir_expand/src/hygiene.rs | |
parent | 5806195bc1cdb1ca3fa257e99fd6e0dd897713a9 (diff) | |
parent | cf4f7eb56660cfff355cb6bd41d5c17f7d19571b (diff) |
Merge #2130
2130: improve compile time a bit r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_expand/src/hygiene.rs')
-rw-r--r-- | crates/ra_hir_expand/src/hygiene.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/crates/ra_hir_expand/src/hygiene.rs b/crates/ra_hir_expand/src/hygiene.rs new file mode 100644 index 000000000..77428ec99 --- /dev/null +++ b/crates/ra_hir_expand/src/hygiene.rs | |||
@@ -0,0 +1,46 @@ | |||
1 | //! This modules handles hygiene information. | ||
2 | //! | ||
3 | //! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at | ||
4 | //! this moment, this is horribly incomplete and handles only `$crate`. | ||
5 | use ra_db::CrateId; | ||
6 | use ra_syntax::ast; | ||
7 | |||
8 | use crate::{ | ||
9 | db::AstDatabase, | ||
10 | either::Either, | ||
11 | name::{AsName, Name}, | ||
12 | HirFileId, HirFileIdRepr, | ||
13 | }; | ||
14 | |||
15 | #[derive(Debug)] | ||
16 | pub struct Hygiene { | ||
17 | // This is what `$crate` expands to | ||
18 | def_crate: Option<CrateId>, | ||
19 | } | ||
20 | |||
21 | impl Hygiene { | ||
22 | pub fn new(db: &impl AstDatabase, file_id: HirFileId) -> Hygiene { | ||
23 | let def_crate = match file_id.0 { | ||
24 | HirFileIdRepr::FileId(_) => None, | ||
25 | HirFileIdRepr::MacroFile(macro_file) => { | ||
26 | let loc = db.lookup_intern_macro(macro_file.macro_call_id); | ||
27 | Some(loc.def.krate) | ||
28 | } | ||
29 | }; | ||
30 | Hygiene { def_crate } | ||
31 | } | ||
32 | |||
33 | pub fn new_unhygienic() -> Hygiene { | ||
34 | Hygiene { def_crate: None } | ||
35 | } | ||
36 | |||
37 | // FIXME: this should just return name | ||
38 | pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> { | ||
39 | if let Some(def_crate) = self.def_crate { | ||
40 | if name_ref.text() == "$crate" { | ||
41 | return Either::B(def_crate); | ||
42 | } | ||
43 | } | ||
44 | Either::A(name_ref.as_name()) | ||
45 | } | ||
46 | } | ||