aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src/lib.rs')
-rw-r--r--crates/ra_hir_expand/src/lib.rs170
1 files changed, 168 insertions, 2 deletions
diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs
index 6ccb11068..002a5b45a 100644
--- a/crates/ra_hir_expand/src/lib.rs
+++ b/crates/ra_hir_expand/src/lib.rs
@@ -5,7 +5,173 @@
5//! `ra_hir_def`, so this crates doesn't contain a lot at the moment. 5//! `ra_hir_def`, so this crates doesn't contain a lot at the moment.
6 6
7pub mod db; 7pub mod db;
8
9pub mod ast_id_map; 8pub mod ast_id_map;
10 9
11pub mod expand; 10use std::hash::{Hash, Hasher};
11
12use ra_db::{salsa, CrateId, FileId};
13use ra_syntax::ast::{self, AstNode};
14
15use crate::{ast_id_map::FileAstId, db::AstDatabase};
16
17macro_rules! impl_intern_key {
18 ($name:ident) => {
19 impl salsa::InternKey for $name {
20 fn from_intern_id(v: salsa::InternId) -> Self {
21 $name(v)
22 }
23 fn as_intern_id(&self) -> salsa::InternId {
24 self.0
25 }
26 }
27 };
28}
29
30/// Input to the analyzer is a set of files, where each file is identified by
31/// `FileId` and contains source code. However, another source of source code in
32/// Rust are macros: each macro can be thought of as producing a "temporary
33/// file". To assign an id to such a file, we use the id of the macro call that
34/// produced the file. So, a `HirFileId` is either a `FileId` (source code
35/// written by user), or a `MacroCallId` (source code produced by macro).
36///
37/// What is a `MacroCallId`? Simplifying, it's a `HirFileId` of a file
38/// containing the call plus the offset of the macro call in the file. Note that
39/// this is a recursive definition! However, the size_of of `HirFileId` is
40/// finite (because everything bottoms out at the real `FileId`) and small
41/// (`MacroCallId` uses the location interner).
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
43pub struct HirFileId(HirFileIdRepr);
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
46enum HirFileIdRepr {
47 FileId(FileId),
48 MacroFile(MacroFile),
49}
50
51impl From<FileId> for HirFileId {
52 fn from(id: FileId) -> Self {
53 HirFileId(HirFileIdRepr::FileId(id))
54 }
55}
56
57impl From<MacroFile> for HirFileId {
58 fn from(id: MacroFile) -> Self {
59 HirFileId(HirFileIdRepr::MacroFile(id))
60 }
61}
62
63impl HirFileId {
64 /// For macro-expansion files, returns the file original source file the
65 /// expansion originated from.
66 pub fn original_file(self, db: &impl AstDatabase) -> FileId {
67 match self.0 {
68 HirFileIdRepr::FileId(file_id) => file_id,
69 HirFileIdRepr::MacroFile(macro_file) => {
70 let loc = db.lookup_intern_macro(macro_file.macro_call_id);
71 loc.ast_id.file_id().original_file(db)
72 }
73 }
74 }
75
76 /// Get the crate which the macro lives in, if it is a macro file.
77 pub fn macro_crate(self, db: &impl AstDatabase) -> Option<CrateId> {
78 match self.0 {
79 HirFileIdRepr::FileId(_) => None,
80 HirFileIdRepr::MacroFile(macro_file) => {
81 let loc = db.lookup_intern_macro(macro_file.macro_call_id);
82 Some(loc.def.krate)
83 }
84 }
85 }
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
89pub struct MacroFile {
90 macro_call_id: MacroCallId,
91 macro_file_kind: MacroFileKind,
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
95pub enum MacroFileKind {
96 Items,
97 Expr,
98}
99
100/// `MacroCallId` identifies a particular macro invocation, like
101/// `println!("Hello, {}", world)`.
102#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
103pub struct MacroCallId(salsa::InternId);
104impl_intern_key!(MacroCallId);
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
107pub struct MacroDefId {
108 pub krate: CrateId,
109 pub ast_id: AstId<ast::MacroCall>,
110}
111
112#[derive(Debug, Clone, PartialEq, Eq, Hash)]
113pub struct MacroCallLoc {
114 pub def: MacroDefId,
115 pub ast_id: AstId<ast::MacroCall>,
116}
117
118impl MacroCallId {
119 pub fn loc(self, db: &impl AstDatabase) -> MacroCallLoc {
120 db.lookup_intern_macro(self)
121 }
122
123 pub fn as_file(self, kind: MacroFileKind) -> HirFileId {
124 let macro_file = MacroFile { macro_call_id: self, macro_file_kind: kind };
125 macro_file.into()
126 }
127}
128
129impl MacroCallLoc {
130 pub fn id(self, db: &impl AstDatabase) -> MacroCallId {
131 db.intern_macro(self)
132 }
133}
134
135/// `AstId` points to an AST node in any file.
136///
137/// It is stable across reparses, and can be used as salsa key/value.
138// FIXME: isn't this just a `Source<FileAstId<N>>` ?
139#[derive(Debug)]
140pub struct AstId<N: AstNode> {
141 file_id: HirFileId,
142 file_ast_id: FileAstId<N>,
143}
144
145impl<N: AstNode> Clone for AstId<N> {
146 fn clone(&self) -> AstId<N> {
147 *self
148 }
149}
150impl<N: AstNode> Copy for AstId<N> {}
151
152impl<N: AstNode> PartialEq for AstId<N> {
153 fn eq(&self, other: &Self) -> bool {
154 (self.file_id, self.file_ast_id) == (other.file_id, other.file_ast_id)
155 }
156}
157impl<N: AstNode> Eq for AstId<N> {}
158impl<N: AstNode> Hash for AstId<N> {
159 fn hash<H: Hasher>(&self, hasher: &mut H) {
160 (self.file_id, self.file_ast_id).hash(hasher);
161 }
162}
163
164impl<N: AstNode> AstId<N> {
165 pub fn new(file_id: HirFileId, file_ast_id: FileAstId<N>) -> AstId<N> {
166 AstId { file_id, file_ast_id }
167 }
168
169 pub fn file_id(&self) -> HirFileId {
170 self.file_id
171 }
172
173 pub fn to_node(&self, db: &impl AstDatabase) -> N {
174 let syntax_node = db.ast_id_to_node(self.file_id, self.file_ast_id.into());
175 N::cast(syntax_node).unwrap()
176 }
177}