]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs
New upstream version 1.65.0+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / hir-def / src / item_tree.rs
1 //! A simplified AST that only contains items.
2 //!
3 //! This is the primary IR used throughout `hir_def`. It is the input to the name resolution
4 //! algorithm, as well as to the queries defined in `adt.rs`, `data.rs`, and most things in
5 //! `attr.rs`.
6 //!
7 //! `ItemTree`s are built per `HirFileId`, from the syntax tree of the parsed file. This means that
8 //! they are crate-independent: they don't know which `#[cfg]`s are active or which module they
9 //! belong to, since those concepts don't exist at this level (a single `ItemTree` might be part of
10 //! multiple crates, or might be included into the same crate twice via `#[path]`).
11 //!
12 //! One important purpose of this layer is to provide an "invalidation barrier" for incremental
13 //! computations: when typing inside an item body, the `ItemTree` of the modified file is typically
14 //! unaffected, so we don't have to recompute name resolution results or item data (see `data.rs`).
15 //!
16 //! The `ItemTree` for the currently open file can be displayed by using the VS Code command
17 //! "rust-analyzer: Debug ItemTree".
18 //!
19 //! Compared to rustc's architecture, `ItemTree` has properties from both rustc's AST and HIR: many
20 //! syntax-level Rust features are already desugared to simpler forms in the `ItemTree`, but name
21 //! resolution has not yet been performed. `ItemTree`s are per-file, while rustc's AST and HIR are
22 //! per-crate, because we are interested in incrementally computing it.
23 //!
24 //! The representation of items in the `ItemTree` should generally mirror the surface syntax: it is
25 //! usually a bad idea to desugar a syntax-level construct to something that is structurally
26 //! different here. Name resolution needs to be able to process attributes and expand macros
27 //! (including attribute macros), and having a 1-to-1 mapping between syntax and the `ItemTree`
28 //! avoids introducing subtle bugs.
29 //!
30 //! In general, any item in the `ItemTree` stores its `AstId`, which allows mapping it back to its
31 //! surface syntax.
32
33 mod lower;
34 mod pretty;
35 #[cfg(test)]
36 mod tests;
37
38 use std::{
39 fmt::{self, Debug},
40 hash::{Hash, Hasher},
41 marker::PhantomData,
42 ops::Index,
43 sync::Arc,
44 };
45
46 use ast::{AstNode, HasName, StructKind};
47 use base_db::CrateId;
48 use either::Either;
49 use hir_expand::{
50 ast_id_map::FileAstId,
51 hygiene::Hygiene,
52 name::{name, AsName, Name},
53 ExpandTo, HirFileId, InFile,
54 };
55 use la_arena::{Arena, Idx, IdxRange, RawIdx};
56 use profile::Count;
57 use rustc_hash::FxHashMap;
58 use smallvec::SmallVec;
59 use stdx::never;
60 use syntax::{ast, match_ast, SyntaxKind};
61
62 use crate::{
63 attr::{Attrs, RawAttrs},
64 db::DefDatabase,
65 generics::GenericParams,
66 intern::Interned,
67 path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind},
68 type_ref::{Mutability, TraitRef, TypeBound, TypeRef},
69 visibility::RawVisibility,
70 BlockId,
71 };
72
73 #[derive(Copy, Clone, Eq, PartialEq)]
74 pub struct RawVisibilityId(u32);
75
76 impl RawVisibilityId {
77 pub const PUB: Self = RawVisibilityId(u32::max_value());
78 pub const PRIV: Self = RawVisibilityId(u32::max_value() - 1);
79 pub const PUB_CRATE: Self = RawVisibilityId(u32::max_value() - 2);
80 }
81
82 impl fmt::Debug for RawVisibilityId {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 let mut f = f.debug_tuple("RawVisibilityId");
85 match *self {
86 Self::PUB => f.field(&"pub"),
87 Self::PRIV => f.field(&"pub(self)"),
88 Self::PUB_CRATE => f.field(&"pub(crate)"),
89 _ => f.field(&self.0),
90 };
91 f.finish()
92 }
93 }
94
95 /// The item tree of a source file.
96 #[derive(Debug, Default, Eq, PartialEq)]
97 pub struct ItemTree {
98 _c: Count<Self>,
99
100 top_level: SmallVec<[ModItem; 1]>,
101 attrs: FxHashMap<AttrOwner, RawAttrs>,
102
103 data: Option<Box<ItemTreeData>>,
104 }
105
106 impl ItemTree {
107 pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
108 let _p = profile::span("file_item_tree_query").detail(|| format!("{:?}", file_id));
109 let syntax = match db.parse_or_expand(file_id) {
110 Some(node) => node,
111 None => return Default::default(),
112 };
113 if never!(syntax.kind() == SyntaxKind::ERROR) {
114 // FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic
115 return Default::default();
116 }
117
118 let ctx = lower::Ctx::new(db, file_id);
119 let mut top_attrs = None;
120 let mut item_tree = match_ast! {
121 match syntax {
122 ast::SourceFile(file) => {
123 top_attrs = Some(RawAttrs::new(db, &file, ctx.hygiene()));
124 ctx.lower_module_items(&file)
125 },
126 ast::MacroItems(items) => {
127 ctx.lower_module_items(&items)
128 },
129 ast::MacroStmts(stmts) => {
130 // The produced statements can include items, which should be added as top-level
131 // items.
132 ctx.lower_macro_stmts(stmts)
133 },
134 _ => {
135 panic!("cannot create item tree from {:?} {}", syntax, syntax);
136 },
137 }
138 };
139
140 if let Some(attrs) = top_attrs {
141 item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
142 }
143 item_tree.shrink_to_fit();
144 Arc::new(item_tree)
145 }
146
147 /// Returns an iterator over all items located at the top level of the `HirFileId` this
148 /// `ItemTree` was created from.
149 pub fn top_level_items(&self) -> &[ModItem] {
150 &self.top_level
151 }
152
153 /// Returns the inner attributes of the source file.
154 pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
155 self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&RawAttrs::EMPTY).clone().filter(db, krate)
156 }
157
158 pub(crate) fn raw_attrs(&self, of: AttrOwner) -> &RawAttrs {
159 self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY)
160 }
161
162 pub(crate) fn attrs(&self, db: &dyn DefDatabase, krate: CrateId, of: AttrOwner) -> Attrs {
163 self.raw_attrs(of).clone().filter(db, krate)
164 }
165
166 pub fn pretty_print(&self) -> String {
167 pretty::print_item_tree(self)
168 }
169
170 fn data(&self) -> &ItemTreeData {
171 self.data.as_ref().expect("attempted to access data of empty ItemTree")
172 }
173
174 fn data_mut(&mut self) -> &mut ItemTreeData {
175 self.data.get_or_insert_with(Box::default)
176 }
177
178 fn block_item_tree(db: &dyn DefDatabase, block: BlockId) -> Arc<ItemTree> {
179 let loc = db.lookup_intern_block(block);
180 let block = loc.ast_id.to_node(db.upcast());
181 let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
182 Arc::new(ctx.lower_block(&block))
183 }
184
185 fn shrink_to_fit(&mut self) {
186 if let Some(data) = &mut self.data {
187 let ItemTreeData {
188 imports,
189 extern_crates,
190 extern_blocks,
191 functions,
192 params,
193 structs,
194 fields,
195 unions,
196 enums,
197 variants,
198 consts,
199 statics,
200 traits,
201 impls,
202 type_aliases,
203 mods,
204 macro_calls,
205 macro_rules,
206 macro_defs,
207 vis,
208 } = &mut **data;
209
210 imports.shrink_to_fit();
211 extern_crates.shrink_to_fit();
212 extern_blocks.shrink_to_fit();
213 functions.shrink_to_fit();
214 params.shrink_to_fit();
215 structs.shrink_to_fit();
216 fields.shrink_to_fit();
217 unions.shrink_to_fit();
218 enums.shrink_to_fit();
219 variants.shrink_to_fit();
220 consts.shrink_to_fit();
221 statics.shrink_to_fit();
222 traits.shrink_to_fit();
223 impls.shrink_to_fit();
224 type_aliases.shrink_to_fit();
225 mods.shrink_to_fit();
226 macro_calls.shrink_to_fit();
227 macro_rules.shrink_to_fit();
228 macro_defs.shrink_to_fit();
229
230 vis.arena.shrink_to_fit();
231 }
232 }
233 }
234
235 #[derive(Default, Debug, Eq, PartialEq)]
236 struct ItemVisibilities {
237 arena: Arena<RawVisibility>,
238 }
239
240 impl ItemVisibilities {
241 fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
242 match &vis {
243 RawVisibility::Public => RawVisibilityId::PUB,
244 RawVisibility::Module(path) if path.segments().is_empty() => match &path.kind {
245 PathKind::Super(0) => RawVisibilityId::PRIV,
246 PathKind::Crate => RawVisibilityId::PUB_CRATE,
247 _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
248 },
249 _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
250 }
251 }
252 }
253
254 static VIS_PUB: RawVisibility = RawVisibility::Public;
255 static VIS_PRIV: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)));
256 static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate));
257
258 #[derive(Default, Debug, Eq, PartialEq)]
259 struct ItemTreeData {
260 imports: Arena<Import>,
261 extern_crates: Arena<ExternCrate>,
262 extern_blocks: Arena<ExternBlock>,
263 functions: Arena<Function>,
264 params: Arena<Param>,
265 structs: Arena<Struct>,
266 fields: Arena<Field>,
267 unions: Arena<Union>,
268 enums: Arena<Enum>,
269 variants: Arena<Variant>,
270 consts: Arena<Const>,
271 statics: Arena<Static>,
272 traits: Arena<Trait>,
273 impls: Arena<Impl>,
274 type_aliases: Arena<TypeAlias>,
275 mods: Arena<Mod>,
276 macro_calls: Arena<MacroCall>,
277 macro_rules: Arena<MacroRules>,
278 macro_defs: Arena<MacroDef>,
279
280 vis: ItemVisibilities,
281 }
282
283 #[derive(Debug, Eq, PartialEq, Hash)]
284 pub enum AttrOwner {
285 /// Attributes on an item.
286 ModItem(ModItem),
287 /// Inner attributes of the source file.
288 TopLevel,
289
290 Variant(Idx<Variant>),
291 Field(Idx<Field>),
292 Param(Idx<Param>),
293 }
294
295 macro_rules! from_attrs {
296 ( $( $var:ident($t:ty) ),+ ) => {
297 $(
298 impl From<$t> for AttrOwner {
299 fn from(t: $t) -> AttrOwner {
300 AttrOwner::$var(t)
301 }
302 }
303 )+
304 };
305 }
306
307 from_attrs!(ModItem(ModItem), Variant(Idx<Variant>), Field(Idx<Field>), Param(Idx<Param>));
308
309 /// Trait implemented by all item nodes in the item tree.
310 pub trait ItemTreeNode: Clone {
311 type Source: AstNode + Into<ast::Item>;
312
313 fn ast_id(&self) -> FileAstId<Self::Source>;
314
315 /// Looks up an instance of `Self` in an item tree.
316 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
317
318 /// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
319 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
320
321 /// Upcasts a `FileItemTreeId` to a generic `ModItem`.
322 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
323 }
324
325 pub struct FileItemTreeId<N: ItemTreeNode> {
326 index: Idx<N>,
327 _p: PhantomData<N>,
328 }
329
330 impl<N: ItemTreeNode> Clone for FileItemTreeId<N> {
331 fn clone(&self) -> Self {
332 Self { index: self.index, _p: PhantomData }
333 }
334 }
335 impl<N: ItemTreeNode> Copy for FileItemTreeId<N> {}
336
337 impl<N: ItemTreeNode> PartialEq for FileItemTreeId<N> {
338 fn eq(&self, other: &FileItemTreeId<N>) -> bool {
339 self.index == other.index
340 }
341 }
342 impl<N: ItemTreeNode> Eq for FileItemTreeId<N> {}
343
344 impl<N: ItemTreeNode> Hash for FileItemTreeId<N> {
345 fn hash<H: Hasher>(&self, state: &mut H) {
346 self.index.hash(state)
347 }
348 }
349
350 impl<N: ItemTreeNode> fmt::Debug for FileItemTreeId<N> {
351 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
352 self.index.fmt(f)
353 }
354 }
355
356 /// Identifies a particular [`ItemTree`].
357 #[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
358 pub struct TreeId {
359 file: HirFileId,
360 block: Option<BlockId>,
361 }
362
363 impl TreeId {
364 pub(crate) fn new(file: HirFileId, block: Option<BlockId>) -> Self {
365 Self { file, block }
366 }
367
368 pub(crate) fn item_tree(&self, db: &dyn DefDatabase) -> Arc<ItemTree> {
369 match self.block {
370 Some(block) => ItemTree::block_item_tree(db, block),
371 None => db.file_item_tree(self.file),
372 }
373 }
374
375 pub(crate) fn file_id(self) -> HirFileId {
376 self.file
377 }
378
379 pub(crate) fn is_block(self) -> bool {
380 self.block.is_some()
381 }
382 }
383
384 #[derive(Debug)]
385 pub struct ItemTreeId<N: ItemTreeNode> {
386 tree: TreeId,
387 pub value: FileItemTreeId<N>,
388 }
389
390 impl<N: ItemTreeNode> ItemTreeId<N> {
391 pub fn new(tree: TreeId, idx: FileItemTreeId<N>) -> Self {
392 Self { tree, value: idx }
393 }
394
395 pub fn file_id(self) -> HirFileId {
396 self.tree.file
397 }
398
399 pub fn tree_id(self) -> TreeId {
400 self.tree
401 }
402
403 pub fn item_tree(self, db: &dyn DefDatabase) -> Arc<ItemTree> {
404 self.tree.item_tree(db)
405 }
406 }
407
408 impl<N: ItemTreeNode> Copy for ItemTreeId<N> {}
409 impl<N: ItemTreeNode> Clone for ItemTreeId<N> {
410 fn clone(&self) -> Self {
411 *self
412 }
413 }
414
415 impl<N: ItemTreeNode> PartialEq for ItemTreeId<N> {
416 fn eq(&self, other: &Self) -> bool {
417 self.tree == other.tree && self.value == other.value
418 }
419 }
420
421 impl<N: ItemTreeNode> Eq for ItemTreeId<N> {}
422
423 impl<N: ItemTreeNode> Hash for ItemTreeId<N> {
424 fn hash<H: Hasher>(&self, state: &mut H) {
425 self.tree.hash(state);
426 self.value.hash(state);
427 }
428 }
429
430 macro_rules! mod_items {
431 ( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
432 #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
433 pub enum ModItem {
434 $(
435 $typ(FileItemTreeId<$typ>),
436 )+
437 }
438
439 $(
440 impl From<FileItemTreeId<$typ>> for ModItem {
441 fn from(id: FileItemTreeId<$typ>) -> ModItem {
442 ModItem::$typ(id)
443 }
444 }
445 )+
446
447 $(
448 impl ItemTreeNode for $typ {
449 type Source = $ast;
450
451 fn ast_id(&self) -> FileAstId<Self::Source> {
452 self.ast_id
453 }
454
455 fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
456 &tree.data().$fld[index]
457 }
458
459 fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
460 match mod_item {
461 ModItem::$typ(id) => Some(id),
462 _ => None,
463 }
464 }
465
466 fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
467 ModItem::$typ(id)
468 }
469 }
470
471 impl Index<Idx<$typ>> for ItemTree {
472 type Output = $typ;
473
474 fn index(&self, index: Idx<$typ>) -> &Self::Output {
475 &self.data().$fld[index]
476 }
477 }
478 )+
479 };
480 }
481
482 mod_items! {
483 Import in imports -> ast::Use,
484 ExternCrate in extern_crates -> ast::ExternCrate,
485 ExternBlock in extern_blocks -> ast::ExternBlock,
486 Function in functions -> ast::Fn,
487 Struct in structs -> ast::Struct,
488 Union in unions -> ast::Union,
489 Enum in enums -> ast::Enum,
490 Const in consts -> ast::Const,
491 Static in statics -> ast::Static,
492 Trait in traits -> ast::Trait,
493 Impl in impls -> ast::Impl,
494 TypeAlias in type_aliases -> ast::TypeAlias,
495 Mod in mods -> ast::Module,
496 MacroCall in macro_calls -> ast::MacroCall,
497 MacroRules in macro_rules -> ast::MacroRules,
498 MacroDef in macro_defs -> ast::MacroDef,
499 }
500
501 macro_rules! impl_index {
502 ( $($fld:ident: $t:ty),+ $(,)? ) => {
503 $(
504 impl Index<Idx<$t>> for ItemTree {
505 type Output = $t;
506
507 fn index(&self, index: Idx<$t>) -> &Self::Output {
508 &self.data().$fld[index]
509 }
510 }
511 )+
512 };
513 }
514
515 impl_index!(fields: Field, variants: Variant, params: Param);
516
517 impl Index<RawVisibilityId> for ItemTree {
518 type Output = RawVisibility;
519 fn index(&self, index: RawVisibilityId) -> &Self::Output {
520 match index {
521 RawVisibilityId::PRIV => &VIS_PRIV,
522 RawVisibilityId::PUB => &VIS_PUB,
523 RawVisibilityId::PUB_CRATE => &VIS_PUB_CRATE,
524 _ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
525 }
526 }
527 }
528
529 impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
530 type Output = N;
531 fn index(&self, id: FileItemTreeId<N>) -> &N {
532 N::lookup(self, id.index)
533 }
534 }
535
536 #[derive(Debug, Clone, Eq, PartialEq)]
537 pub struct Import {
538 pub visibility: RawVisibilityId,
539 pub ast_id: FileAstId<ast::Use>,
540 pub use_tree: UseTree,
541 }
542
543 #[derive(Debug, Clone, Eq, PartialEq)]
544 pub struct UseTree {
545 pub index: Idx<ast::UseTree>,
546 kind: UseTreeKind,
547 }
548
549 #[derive(Debug, Clone, Eq, PartialEq)]
550 pub enum UseTreeKind {
551 /// ```
552 /// use path::to::Item;
553 /// use path::to::Item as Renamed;
554 /// use path::to::Trait as _;
555 /// ```
556 Single { path: Interned<ModPath>, alias: Option<ImportAlias> },
557
558 /// ```
559 /// use *; // (invalid, but can occur in nested tree)
560 /// use path::*;
561 /// ```
562 Glob { path: Option<Interned<ModPath>> },
563
564 /// ```
565 /// use prefix::{self, Item, ...};
566 /// ```
567 Prefixed { prefix: Option<Interned<ModPath>>, list: Box<[UseTree]> },
568 }
569
570 #[derive(Debug, Clone, Eq, PartialEq)]
571 pub struct ExternCrate {
572 pub name: Name,
573 pub alias: Option<ImportAlias>,
574 pub visibility: RawVisibilityId,
575 pub ast_id: FileAstId<ast::ExternCrate>,
576 }
577
578 #[derive(Debug, Clone, Eq, PartialEq)]
579 pub struct ExternBlock {
580 pub abi: Option<Interned<str>>,
581 pub ast_id: FileAstId<ast::ExternBlock>,
582 pub children: Box<[ModItem]>,
583 }
584
585 #[derive(Debug, Clone, Eq, PartialEq)]
586 pub struct Function {
587 pub name: Name,
588 pub visibility: RawVisibilityId,
589 pub explicit_generic_params: Interned<GenericParams>,
590 pub abi: Option<Interned<str>>,
591 pub params: IdxRange<Param>,
592 pub ret_type: Interned<TypeRef>,
593 pub async_ret_type: Option<Interned<TypeRef>>,
594 pub ast_id: FileAstId<ast::Fn>,
595 pub(crate) flags: FnFlags,
596 }
597
598 #[derive(Debug, Clone, Eq, PartialEq)]
599 pub enum Param {
600 Normal(Option<Name>, Interned<TypeRef>),
601 Varargs,
602 }
603
604 bitflags::bitflags! {
605 #[derive(Default)]
606 pub(crate) struct FnFlags: u8 {
607 const HAS_SELF_PARAM = 1 << 0;
608 const HAS_BODY = 1 << 1;
609 const HAS_DEFAULT_KW = 1 << 2;
610 const HAS_CONST_KW = 1 << 3;
611 const HAS_ASYNC_KW = 1 << 4;
612 const HAS_UNSAFE_KW = 1 << 5;
613 const IS_VARARGS = 1 << 6;
614 }
615 }
616
617 #[derive(Debug, Clone, Eq, PartialEq)]
618 pub struct Struct {
619 pub name: Name,
620 pub visibility: RawVisibilityId,
621 pub generic_params: Interned<GenericParams>,
622 pub fields: Fields,
623 pub ast_id: FileAstId<ast::Struct>,
624 }
625
626 #[derive(Debug, Clone, Eq, PartialEq)]
627 pub struct Union {
628 pub name: Name,
629 pub visibility: RawVisibilityId,
630 pub generic_params: Interned<GenericParams>,
631 pub fields: Fields,
632 pub ast_id: FileAstId<ast::Union>,
633 }
634
635 #[derive(Debug, Clone, Eq, PartialEq)]
636 pub struct Enum {
637 pub name: Name,
638 pub visibility: RawVisibilityId,
639 pub generic_params: Interned<GenericParams>,
640 pub variants: IdxRange<Variant>,
641 pub ast_id: FileAstId<ast::Enum>,
642 }
643
644 #[derive(Debug, Clone, Eq, PartialEq)]
645 pub struct Const {
646 /// `None` for `const _: () = ();`
647 pub name: Option<Name>,
648 pub visibility: RawVisibilityId,
649 pub type_ref: Interned<TypeRef>,
650 pub ast_id: FileAstId<ast::Const>,
651 }
652
653 #[derive(Debug, Clone, Eq, PartialEq)]
654 pub struct Static {
655 pub name: Name,
656 pub visibility: RawVisibilityId,
657 pub mutable: bool,
658 pub type_ref: Interned<TypeRef>,
659 pub ast_id: FileAstId<ast::Static>,
660 }
661
662 #[derive(Debug, Clone, Eq, PartialEq)]
663 pub struct Trait {
664 pub name: Name,
665 pub visibility: RawVisibilityId,
666 pub generic_params: Interned<GenericParams>,
667 pub is_auto: bool,
668 pub is_unsafe: bool,
669 pub items: Box<[AssocItem]>,
670 pub ast_id: FileAstId<ast::Trait>,
671 }
672
673 #[derive(Debug, Clone, Eq, PartialEq)]
674 pub struct Impl {
675 pub generic_params: Interned<GenericParams>,
676 pub target_trait: Option<Interned<TraitRef>>,
677 pub self_ty: Interned<TypeRef>,
678 pub is_negative: bool,
679 pub items: Box<[AssocItem]>,
680 pub ast_id: FileAstId<ast::Impl>,
681 }
682
683 #[derive(Debug, Clone, PartialEq, Eq)]
684 pub struct TypeAlias {
685 pub name: Name,
686 pub visibility: RawVisibilityId,
687 /// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
688 pub bounds: Box<[Interned<TypeBound>]>,
689 pub generic_params: Interned<GenericParams>,
690 pub type_ref: Option<Interned<TypeRef>>,
691 pub ast_id: FileAstId<ast::TypeAlias>,
692 }
693
694 #[derive(Debug, Clone, Eq, PartialEq)]
695 pub struct Mod {
696 pub name: Name,
697 pub visibility: RawVisibilityId,
698 pub kind: ModKind,
699 pub ast_id: FileAstId<ast::Module>,
700 }
701
702 #[derive(Debug, Clone, Eq, PartialEq)]
703 pub enum ModKind {
704 /// `mod m { ... }`
705 Inline { items: Box<[ModItem]> },
706
707 /// `mod m;`
708 Outline,
709 }
710
711 #[derive(Debug, Clone, Eq, PartialEq)]
712 pub struct MacroCall {
713 /// Path to the called macro.
714 pub path: Interned<ModPath>,
715 pub ast_id: FileAstId<ast::MacroCall>,
716 pub expand_to: ExpandTo,
717 }
718
719 #[derive(Debug, Clone, Eq, PartialEq)]
720 pub struct MacroRules {
721 /// The name of the declared macro.
722 pub name: Name,
723 pub ast_id: FileAstId<ast::MacroRules>,
724 }
725
726 /// "Macros 2.0" macro definition.
727 #[derive(Debug, Clone, Eq, PartialEq)]
728 pub struct MacroDef {
729 pub name: Name,
730 pub visibility: RawVisibilityId,
731 pub ast_id: FileAstId<ast::MacroDef>,
732 }
733
734 impl Import {
735 /// Maps a `UseTree` contained in this import back to its AST node.
736 pub fn use_tree_to_ast(
737 &self,
738 db: &dyn DefDatabase,
739 file_id: HirFileId,
740 index: Idx<ast::UseTree>,
741 ) -> ast::UseTree {
742 // Re-lower the AST item and get the source map.
743 // Note: The AST unwraps are fine, since if they fail we should have never obtained `index`.
744 let ast = InFile::new(file_id, self.ast_id).to_node(db.upcast());
745 let ast_use_tree = ast.use_tree().expect("missing `use_tree`");
746 let hygiene = Hygiene::new(db.upcast(), file_id);
747 let (_, source_map) =
748 lower::lower_use_tree(db, &hygiene, ast_use_tree).expect("failed to lower use tree");
749 source_map[index].clone()
750 }
751 }
752
753 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
754 pub enum ImportKind {
755 /// The `ModPath` is imported normally.
756 Plain,
757 /// This is a glob-import of all names in the `ModPath`.
758 Glob,
759 /// This is a `some::path::self` import, which imports `some::path` only in type namespace.
760 TypeOnly,
761 }
762
763 impl UseTree {
764 /// Expands the `UseTree` into individually imported `ModPath`s.
765 pub fn expand(
766 &self,
767 mut cb: impl FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>),
768 ) {
769 self.expand_impl(None, &mut cb)
770 }
771
772 fn expand_impl(
773 &self,
774 prefix: Option<ModPath>,
775 cb: &mut dyn FnMut(Idx<ast::UseTree>, ModPath, ImportKind, Option<ImportAlias>),
776 ) {
777 fn concat_mod_paths(
778 prefix: Option<ModPath>,
779 path: &ModPath,
780 ) -> Option<(ModPath, ImportKind)> {
781 match (prefix, &path.kind) {
782 (None, _) => Some((path.clone(), ImportKind::Plain)),
783 (Some(mut prefix), PathKind::Plain) => {
784 for segment in path.segments() {
785 prefix.push_segment(segment.clone());
786 }
787 Some((prefix, ImportKind::Plain))
788 }
789 (Some(mut prefix), PathKind::Super(n))
790 if *n > 0 && prefix.segments().is_empty() =>
791 {
792 // `super::super` + `super::rest`
793 match &mut prefix.kind {
794 PathKind::Super(m) => {
795 cov_mark::hit!(concat_super_mod_paths);
796 *m += *n;
797 for segment in path.segments() {
798 prefix.push_segment(segment.clone());
799 }
800 Some((prefix, ImportKind::Plain))
801 }
802 _ => None,
803 }
804 }
805 (Some(prefix), PathKind::Super(0)) if path.segments().is_empty() => {
806 // `some::path::self` == `some::path`
807 Some((prefix, ImportKind::TypeOnly))
808 }
809 (Some(_), _) => None,
810 }
811 }
812
813 match &self.kind {
814 UseTreeKind::Single { path, alias } => {
815 if let Some((path, kind)) = concat_mod_paths(prefix, path) {
816 cb(self.index, path, kind, alias.clone());
817 }
818 }
819 UseTreeKind::Glob { path: Some(path) } => {
820 if let Some((path, _)) = concat_mod_paths(prefix, path) {
821 cb(self.index, path, ImportKind::Glob, None);
822 }
823 }
824 UseTreeKind::Glob { path: None } => {
825 if let Some(prefix) = prefix {
826 cb(self.index, prefix, ImportKind::Glob, None);
827 }
828 }
829 UseTreeKind::Prefixed { prefix: additional_prefix, list } => {
830 let prefix = match additional_prefix {
831 Some(path) => match concat_mod_paths(prefix, path) {
832 Some((path, ImportKind::Plain)) => Some(path),
833 _ => return,
834 },
835 None => prefix,
836 };
837 for tree in &**list {
838 tree.expand_impl(prefix.clone(), cb);
839 }
840 }
841 }
842 }
843 }
844
845 macro_rules! impl_froms {
846 ($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
847 $(
848 impl From<$t> for $e {
849 fn from(it: $t) -> $e {
850 $e::$v(it)
851 }
852 }
853 )*
854 }
855 }
856
857 impl ModItem {
858 pub fn as_assoc_item(&self) -> Option<AssocItem> {
859 match self {
860 ModItem::Import(_)
861 | ModItem::ExternCrate(_)
862 | ModItem::ExternBlock(_)
863 | ModItem::Struct(_)
864 | ModItem::Union(_)
865 | ModItem::Enum(_)
866 | ModItem::Static(_)
867 | ModItem::Trait(_)
868 | ModItem::Impl(_)
869 | ModItem::Mod(_)
870 | ModItem::MacroRules(_)
871 | ModItem::MacroDef(_) => None,
872 ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
873 ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
874 ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
875 ModItem::Function(func) => Some(AssocItem::Function(*func)),
876 }
877 }
878
879 pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
880 N::id_from_mod_item(self)
881 }
882
883 pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
884 match self {
885 ModItem::Import(it) => tree[it.index].ast_id().upcast(),
886 ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
887 ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(),
888 ModItem::Function(it) => tree[it.index].ast_id().upcast(),
889 ModItem::Struct(it) => tree[it.index].ast_id().upcast(),
890 ModItem::Union(it) => tree[it.index].ast_id().upcast(),
891 ModItem::Enum(it) => tree[it.index].ast_id().upcast(),
892 ModItem::Const(it) => tree[it.index].ast_id().upcast(),
893 ModItem::Static(it) => tree[it.index].ast_id().upcast(),
894 ModItem::Trait(it) => tree[it.index].ast_id().upcast(),
895 ModItem::Impl(it) => tree[it.index].ast_id().upcast(),
896 ModItem::TypeAlias(it) => tree[it.index].ast_id().upcast(),
897 ModItem::Mod(it) => tree[it.index].ast_id().upcast(),
898 ModItem::MacroCall(it) => tree[it.index].ast_id().upcast(),
899 ModItem::MacroRules(it) => tree[it.index].ast_id().upcast(),
900 ModItem::MacroDef(it) => tree[it.index].ast_id().upcast(),
901 }
902 }
903 }
904
905 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
906 pub enum AssocItem {
907 Function(FileItemTreeId<Function>),
908 TypeAlias(FileItemTreeId<TypeAlias>),
909 Const(FileItemTreeId<Const>),
910 MacroCall(FileItemTreeId<MacroCall>),
911 }
912
913 impl_froms!(AssocItem {
914 Function(FileItemTreeId<Function>),
915 TypeAlias(FileItemTreeId<TypeAlias>),
916 Const(FileItemTreeId<Const>),
917 MacroCall(FileItemTreeId<MacroCall>),
918 });
919
920 impl From<AssocItem> for ModItem {
921 fn from(item: AssocItem) -> Self {
922 match item {
923 AssocItem::Function(it) => it.into(),
924 AssocItem::TypeAlias(it) => it.into(),
925 AssocItem::Const(it) => it.into(),
926 AssocItem::MacroCall(it) => it.into(),
927 }
928 }
929 }
930
931 impl AssocItem {
932 pub fn ast_id(self, tree: &ItemTree) -> FileAstId<ast::AssocItem> {
933 match self {
934 AssocItem::Function(id) => tree[id].ast_id.upcast(),
935 AssocItem::TypeAlias(id) => tree[id].ast_id.upcast(),
936 AssocItem::Const(id) => tree[id].ast_id.upcast(),
937 AssocItem::MacroCall(id) => tree[id].ast_id.upcast(),
938 }
939 }
940 }
941
942 #[derive(Debug, Eq, PartialEq)]
943 pub struct Variant {
944 pub name: Name,
945 pub fields: Fields,
946 }
947
948 #[derive(Debug, Clone, PartialEq, Eq)]
949 pub enum Fields {
950 Record(IdxRange<Field>),
951 Tuple(IdxRange<Field>),
952 Unit,
953 }
954
955 /// A single field of an enum variant or struct
956 #[derive(Debug, Clone, PartialEq, Eq)]
957 pub struct Field {
958 pub name: Name,
959 pub type_ref: Interned<TypeRef>,
960 pub visibility: RawVisibilityId,
961 }