aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/item_tree.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/item_tree.rs')
-rw-r--r--crates/hir_def/src/item_tree.rs35
1 files changed, 24 insertions, 11 deletions
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index cad8a7479..528270d49 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -1,6 +1,9 @@
1//! A simplified AST that only contains items. 1//! A simplified AST that only contains items.
2 2
3mod lower; 3mod lower;
4mod pretty;
5#[cfg(test)]
6mod tests;
4 7
5use std::{ 8use std::{
6 any::type_name, 9 any::type_name,
@@ -132,6 +135,7 @@ impl ItemTree {
132 let ItemTreeData { 135 let ItemTreeData {
133 imports, 136 imports,
134 extern_crates, 137 extern_crates,
138 extern_blocks,
135 functions, 139 functions,
136 params, 140 params,
137 structs, 141 structs,
@@ -154,6 +158,7 @@ impl ItemTree {
154 158
155 imports.shrink_to_fit(); 159 imports.shrink_to_fit();
156 extern_crates.shrink_to_fit(); 160 extern_crates.shrink_to_fit();
161 extern_blocks.shrink_to_fit();
157 functions.shrink_to_fit(); 162 functions.shrink_to_fit();
158 params.shrink_to_fit(); 163 params.shrink_to_fit();
159 structs.shrink_to_fit(); 164 structs.shrink_to_fit();
@@ -203,6 +208,10 @@ impl ItemTree {
203 } 208 }
204 } 209 }
205 210
211 pub fn pretty_print(&self) -> String {
212 pretty::print_item_tree(self)
213 }
214
206 fn data(&self) -> &ItemTreeData { 215 fn data(&self) -> &ItemTreeData {
207 self.data.as_ref().expect("attempted to access data of empty ItemTree") 216 self.data.as_ref().expect("attempted to access data of empty ItemTree")
208 } 217 }
@@ -239,6 +248,7 @@ static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(P
239struct ItemTreeData { 248struct ItemTreeData {
240 imports: Arena<Import>, 249 imports: Arena<Import>,
241 extern_crates: Arena<ExternCrate>, 250 extern_crates: Arena<ExternCrate>,
251 extern_blocks: Arena<ExternBlock>,
242 functions: Arena<Function>, 252 functions: Arena<Function>,
243 params: Arena<Param>, 253 params: Arena<Param>,
244 structs: Arena<Struct>, 254 structs: Arena<Struct>,
@@ -432,6 +442,7 @@ macro_rules! mod_items {
432mod_items! { 442mod_items! {
433 Import in imports -> ast::Use, 443 Import in imports -> ast::Use,
434 ExternCrate in extern_crates -> ast::ExternCrate, 444 ExternCrate in extern_crates -> ast::ExternCrate,
445 ExternBlock in extern_blocks -> ast::ExternBlock,
435 Function in functions -> ast::Fn, 446 Function in functions -> ast::Fn,
436 Struct in structs -> ast::Struct, 447 Struct in structs -> ast::Struct,
437 Union in unions -> ast::Union, 448 Union in unions -> ast::Union,
@@ -508,6 +519,13 @@ pub struct ExternCrate {
508} 519}
509 520
510#[derive(Debug, Clone, Eq, PartialEq)] 521#[derive(Debug, Clone, Eq, PartialEq)]
522pub struct ExternBlock {
523 pub abi: Option<Interned<str>>,
524 pub ast_id: FileAstId<ast::ExternBlock>,
525 pub children: Box<[ModItem]>,
526}
527
528#[derive(Debug, Clone, Eq, PartialEq)]
511pub struct Function { 529pub struct Function {
512 pub name: Name, 530 pub name: Name,
513 pub visibility: RawVisibilityId, 531 pub visibility: RawVisibilityId,
@@ -549,17 +567,6 @@ pub struct Struct {
549 pub generic_params: Interned<GenericParams>, 567 pub generic_params: Interned<GenericParams>,
550 pub fields: Fields, 568 pub fields: Fields,
551 pub ast_id: FileAstId<ast::Struct>, 569 pub ast_id: FileAstId<ast::Struct>,
552 pub kind: StructDefKind,
553}
554
555#[derive(Debug, Clone, Eq, PartialEq)]
556pub enum StructDefKind {
557 /// `struct S { ... }` - type namespace only.
558 Record,
559 /// `struct S(...);`
560 Tuple,
561 /// `struct S;`
562 Unit,
563} 570}
564 571
565#[derive(Debug, Clone, Eq, PartialEq)] 572#[derive(Debug, Clone, Eq, PartialEq)]
@@ -691,6 +698,7 @@ impl ModItem {
691 match self { 698 match self {
692 ModItem::Import(_) 699 ModItem::Import(_)
693 | ModItem::ExternCrate(_) 700 | ModItem::ExternCrate(_)
701 | ModItem::ExternBlock(_)
694 | ModItem::Struct(_) 702 | ModItem::Struct(_)
695 | ModItem::Union(_) 703 | ModItem::Union(_)
696 | ModItem::Enum(_) 704 | ModItem::Enum(_)
@@ -715,6 +723,7 @@ impl ModItem {
715 match self { 723 match self {
716 ModItem::Import(it) => tree[it.index].ast_id().upcast(), 724 ModItem::Import(it) => tree[it.index].ast_id().upcast(),
717 ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(), 725 ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
726 ModItem::ExternBlock(it) => tree[it.index].ast_id().upcast(),
718 ModItem::Function(it) => tree[it.index].ast_id().upcast(), 727 ModItem::Function(it) => tree[it.index].ast_id().upcast(),
719 ModItem::Struct(it) => tree[it.index].ast_id().upcast(), 728 ModItem::Struct(it) => tree[it.index].ast_id().upcast(),
720 ModItem::Union(it) => tree[it.index].ast_id().upcast(), 729 ModItem::Union(it) => tree[it.index].ast_id().upcast(),
@@ -774,6 +783,10 @@ impl<T> IdRange<T> {
774 fn new(range: Range<Idx<T>>) -> Self { 783 fn new(range: Range<Idx<T>>) -> Self {
775 Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData } 784 Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData }
776 } 785 }
786
787 fn is_empty(&self) -> bool {
788 self.range.is_empty()
789 }
777} 790}
778 791
779impl<T> Iterator for IdRange<T> { 792impl<T> Iterator for IdRange<T> {