2020-03-25 14:33:01 +00:00
|
|
|
//! A simplified AST that only contains items.
|
|
|
|
|
2020-06-16 12:52:43 +00:00
|
|
|
mod lower;
|
2020-06-22 13:07:06 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2020-06-16 12:52:43 +00:00
|
|
|
|
|
|
|
use std::{
|
2020-06-16 17:20:29 +00:00
|
|
|
fmt::{self, Debug},
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
marker::PhantomData,
|
2020-06-16 12:52:43 +00:00
|
|
|
ops::{Index, Range},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
2020-06-23 11:46:38 +00:00
|
|
|
use ast::{AstNode, AttrsOwner, NameOwner, StructKind, TypeAscriptionOwner};
|
2020-06-16 12:52:43 +00:00
|
|
|
use either::Either;
|
2020-03-25 14:33:01 +00:00
|
|
|
use hir_expand::{
|
2020-06-16 12:52:43 +00:00
|
|
|
ast_id_map::FileAstId,
|
2020-03-25 14:33:01 +00:00
|
|
|
hygiene::Hygiene,
|
|
|
|
name::{name, AsName, Name},
|
2020-06-12 11:58:02 +00:00
|
|
|
HirFileId, InFile,
|
2020-03-25 14:33:01 +00:00
|
|
|
};
|
|
|
|
use ra_arena::{Arena, Idx, RawId};
|
2020-06-11 17:46:56 +00:00
|
|
|
use ra_syntax::{ast, match_ast};
|
2020-06-16 12:52:43 +00:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-06-15 17:16:29 +00:00
|
|
|
use test_utils::mark;
|
2020-03-25 14:33:01 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
attr::Attrs,
|
2020-06-11 17:46:56 +00:00
|
|
|
db::DefDatabase,
|
2020-06-17 10:24:05 +00:00
|
|
|
generics::GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path},
|
|
|
|
type_ref::{Mutability, TypeBound, TypeRef},
|
|
|
|
visibility::RawVisibility,
|
|
|
|
};
|
2020-06-22 13:07:06 +00:00
|
|
|
use smallvec::SmallVec;
|
2020-03-25 14:33:01 +00:00
|
|
|
|
2020-06-12 11:58:02 +00:00
|
|
|
/// The item tree of a source file.
|
2020-06-22 13:07:06 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct ItemTree {
|
2020-06-22 13:07:06 +00:00
|
|
|
file_id: HirFileId,
|
2020-06-11 17:46:56 +00:00
|
|
|
top_level: Vec<ModItem>,
|
2020-06-15 17:16:02 +00:00
|
|
|
top_attrs: Attrs,
|
|
|
|
attrs: FxHashMap<ModItem, Attrs>,
|
|
|
|
empty_attrs: Attrs,
|
2020-06-22 13:07:06 +00:00
|
|
|
inner_items: FxHashMap<FileAstId<ast::ModuleItem>, SmallVec<[ModItem; 1]>>,
|
2020-06-15 17:16:02 +00:00
|
|
|
|
2020-03-25 14:33:01 +00:00
|
|
|
imports: Arena<Import>,
|
2020-06-22 13:07:06 +00:00
|
|
|
extern_crates: Arena<ExternCrate>,
|
2020-03-25 14:33:01 +00:00
|
|
|
functions: Arena<Function>,
|
|
|
|
structs: Arena<Struct>,
|
|
|
|
fields: Arena<Field>,
|
|
|
|
unions: Arena<Union>,
|
|
|
|
enums: Arena<Enum>,
|
|
|
|
variants: Arena<Variant>,
|
|
|
|
consts: Arena<Const>,
|
|
|
|
statics: Arena<Static>,
|
|
|
|
traits: Arena<Trait>,
|
|
|
|
impls: Arena<Impl>,
|
|
|
|
type_aliases: Arena<TypeAlias>,
|
|
|
|
mods: Arena<Mod>,
|
|
|
|
macro_calls: Arena<MacroCall>,
|
|
|
|
exprs: Arena<Expr>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemTree {
|
2020-06-11 17:46:56 +00:00
|
|
|
pub fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
|
2020-06-22 14:59:15 +00:00
|
|
|
let _p = ra_prof::profile("item_tree_query").detail(|| format!("{:?}", file_id));
|
2020-06-11 17:46:56 +00:00
|
|
|
let syntax = if let Some(node) = db.parse_or_expand(file_id) {
|
|
|
|
node
|
|
|
|
} else {
|
2020-06-22 13:07:06 +00:00
|
|
|
return Arc::new(Self::empty(file_id));
|
2020-06-11 17:46:56 +00:00
|
|
|
};
|
|
|
|
|
2020-06-15 17:16:02 +00:00
|
|
|
let hygiene = Hygiene::new(db.upcast(), file_id);
|
2020-06-23 11:46:38 +00:00
|
|
|
let ctx = lower::Ctx::new(db, hygiene.clone(), file_id);
|
2020-06-15 17:16:02 +00:00
|
|
|
let mut top_attrs = None;
|
2020-06-23 11:46:38 +00:00
|
|
|
let mut item_tree = match_ast! {
|
2020-06-11 17:46:56 +00:00
|
|
|
match syntax {
|
|
|
|
ast::SourceFile(file) => {
|
2020-06-15 17:16:02 +00:00
|
|
|
top_attrs = Some(Attrs::new(&file, &hygiene));
|
2020-06-23 11:46:38 +00:00
|
|
|
ctx.lower_module_items(&file)
|
|
|
|
},
|
|
|
|
ast::MacroItems(items) => {
|
|
|
|
ctx.lower_module_items(&items)
|
|
|
|
},
|
|
|
|
// Macros can expand to expressions. We return an empty item tree in this case, but
|
|
|
|
// still need to collect inner items.
|
|
|
|
ast::Expr(e) => {
|
|
|
|
ctx.lower_inner_items(e.syntax())
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
panic!("cannot create item tree from {:?}", syntax);
|
2020-06-11 17:46:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-22 13:07:06 +00:00
|
|
|
item_tree.top_attrs = top_attrs.unwrap_or_default();
|
|
|
|
Arc::new(item_tree)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty(file_id: HirFileId) -> Self {
|
|
|
|
Self {
|
|
|
|
file_id,
|
|
|
|
top_level: Default::default(),
|
|
|
|
top_attrs: Default::default(),
|
|
|
|
attrs: Default::default(),
|
|
|
|
empty_attrs: Default::default(),
|
|
|
|
inner_items: Default::default(),
|
|
|
|
imports: Default::default(),
|
|
|
|
extern_crates: Default::default(),
|
|
|
|
functions: Default::default(),
|
|
|
|
structs: Default::default(),
|
|
|
|
fields: Default::default(),
|
|
|
|
unions: Default::default(),
|
|
|
|
enums: Default::default(),
|
|
|
|
variants: Default::default(),
|
|
|
|
consts: Default::default(),
|
|
|
|
statics: Default::default(),
|
|
|
|
traits: Default::default(),
|
|
|
|
impls: Default::default(),
|
|
|
|
type_aliases: Default::default(),
|
|
|
|
mods: Default::default(),
|
|
|
|
macro_calls: Default::default(),
|
|
|
|
exprs: Default::default(),
|
|
|
|
}
|
2020-06-11 17:46:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over all items located at the top level of the `HirFileId` this
|
|
|
|
/// `ItemTree` was created from.
|
2020-06-12 21:24:26 +00:00
|
|
|
pub fn top_level_items(&self) -> &[ModItem] {
|
|
|
|
&self.top_level
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
2020-06-15 17:16:02 +00:00
|
|
|
|
|
|
|
/// Returns the inner attributes of the source file.
|
|
|
|
pub fn top_level_attrs(&self) -> &Attrs {
|
|
|
|
&self.top_attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn attrs(&self, of: ModItem) -> &Attrs {
|
|
|
|
self.attrs.get(&of).unwrap_or(&self.empty_attrs)
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
|
|
|
|
/// Returns the lowered inner items that `ast` corresponds to.
|
|
|
|
///
|
|
|
|
/// Most AST items are lowered to a single `ModItem`, but some (eg. `use` items) may be lowered
|
|
|
|
/// to multiple items in the `ItemTree`.
|
|
|
|
pub fn inner_items(&self, ast: FileAstId<ast::ModuleItem>) -> &[ModItem] {
|
|
|
|
&self.inner_items[&ast]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn all_inner_items(&self) -> impl Iterator<Item = ModItem> + '_ {
|
|
|
|
self.inner_items.values().flatten().copied()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn source<S: ItemTreeSource>(
|
|
|
|
&self,
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
of: FileItemTreeId<S>,
|
|
|
|
) -> S::Source {
|
|
|
|
// This unwrap cannot fail, since it has either succeeded above, or resulted in an empty
|
|
|
|
// ItemTree (in which case there is no valid `FileItemTreeId` to call this method with).
|
|
|
|
let root = db
|
|
|
|
.parse_or_expand(self.file_id)
|
|
|
|
.expect("parse_or_expand failed on constructed ItemTree");
|
|
|
|
|
|
|
|
let id = self[of].ast_id();
|
|
|
|
let map = db.ast_id_map(self.file_id);
|
|
|
|
let ptr = map.get(id);
|
|
|
|
ptr.to_node(&root)
|
|
|
|
}
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
/// Trait implemented by all nodes in the item tree.
|
|
|
|
pub trait ItemTreeNode: Clone {
|
|
|
|
/// Looks up an instance of `Self` in an item tree.
|
2020-06-16 17:20:29 +00:00
|
|
|
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
|
2020-06-22 13:07:06 +00:00
|
|
|
|
|
|
|
/// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
|
|
|
|
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
|
2020-06-22 17:15:54 +00:00
|
|
|
|
|
|
|
/// Upcasts a `FileItemTreeId` to a generic `ModItem`.
|
|
|
|
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
|
2020-06-16 17:20:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
/// Trait for item tree nodes that allow accessing the original AST node.
|
|
|
|
pub trait ItemTreeSource: ItemTreeNode {
|
2020-06-22 13:07:06 +00:00
|
|
|
type Source: AstNode + Into<ast::ModuleItem>;
|
2020-06-17 16:43:41 +00:00
|
|
|
|
|
|
|
fn ast_id(&self) -> FileAstId<Self::Source>;
|
|
|
|
}
|
|
|
|
|
2020-06-16 17:20:29 +00:00
|
|
|
pub struct FileItemTreeId<N: ItemTreeNode> {
|
|
|
|
index: Idx<N>,
|
|
|
|
_p: PhantomData<N>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: ItemTreeNode> Clone for FileItemTreeId<N> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self { index: self.index, _p: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<N: ItemTreeNode> Copy for FileItemTreeId<N> {}
|
|
|
|
|
|
|
|
impl<N: ItemTreeNode> PartialEq for FileItemTreeId<N> {
|
|
|
|
fn eq(&self, other: &FileItemTreeId<N>) -> bool {
|
|
|
|
self.index == other.index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<N: ItemTreeNode> Eq for FileItemTreeId<N> {}
|
|
|
|
|
|
|
|
impl<N: ItemTreeNode> Hash for FileItemTreeId<N> {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
|
|
self.index.hash(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<N: ItemTreeNode> fmt::Debug for FileItemTreeId<N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.index.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type ItemTreeId<N> = InFile<FileItemTreeId<N>>;
|
|
|
|
|
2020-06-23 16:41:32 +00:00
|
|
|
macro_rules! mod_items {
|
|
|
|
( $( $typ:ident in $fld:ident -> $ast:ty ),+ $(,)? ) => {
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
|
|
|
pub enum ModItem {
|
|
|
|
$(
|
|
|
|
$typ(FileItemTreeId<$typ>),
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
|
|
|
|
$(
|
|
|
|
impl From<FileItemTreeId<$typ>> for ModItem {
|
|
|
|
fn from(id: FileItemTreeId<$typ>) -> ModItem {
|
|
|
|
ModItem::$typ(id)
|
|
|
|
}
|
2020-06-16 17:20:29 +00:00
|
|
|
}
|
2020-06-23 16:41:32 +00:00
|
|
|
)+
|
2020-06-22 13:07:06 +00:00
|
|
|
|
2020-06-23 16:41:32 +00:00
|
|
|
$(
|
|
|
|
impl ItemTreeNode for $typ {
|
|
|
|
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
|
|
|
|
&tree.$fld[index]
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
|
2020-06-23 16:41:32 +00:00
|
|
|
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> {
|
|
|
|
if let ModItem::$typ(id) = mod_item {
|
|
|
|
Some(id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
|
|
|
|
ModItem::$typ(id)
|
2020-06-22 13:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 17:15:54 +00:00
|
|
|
|
2020-06-23 16:41:32 +00:00
|
|
|
impl ItemTreeSource for $typ {
|
|
|
|
type Source = $ast;
|
|
|
|
|
|
|
|
fn ast_id(&self) -> FileAstId<Self::Source> {
|
|
|
|
self.ast_id
|
|
|
|
}
|
2020-06-22 17:15:54 +00:00
|
|
|
}
|
2020-06-16 17:20:29 +00:00
|
|
|
|
2020-06-23 16:41:32 +00:00
|
|
|
impl Index<Idx<$typ>> for ItemTree {
|
|
|
|
type Output = $typ;
|
|
|
|
|
|
|
|
fn index(&self, index: Idx<$typ>) -> &Self::Output {
|
|
|
|
&self.$fld[index]
|
|
|
|
}
|
2020-06-17 16:43:41 +00:00
|
|
|
}
|
2020-06-23 16:41:32 +00:00
|
|
|
)+
|
|
|
|
};
|
2020-06-17 16:43:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 16:41:32 +00:00
|
|
|
mod_items! {
|
|
|
|
Import in imports -> ast::UseItem,
|
|
|
|
ExternCrate in extern_crates -> ast::ExternCrateItem,
|
|
|
|
Function in functions -> ast::FnDef,
|
|
|
|
Struct in structs -> ast::StructDef,
|
|
|
|
Union in unions -> ast::UnionDef,
|
|
|
|
Enum in enums -> ast::EnumDef,
|
|
|
|
Const in consts -> ast::ConstDef,
|
|
|
|
Static in statics -> ast::StaticDef,
|
|
|
|
Trait in traits -> ast::TraitDef,
|
|
|
|
Impl in impls -> ast::ImplDef,
|
|
|
|
TypeAlias in type_aliases -> ast::TypeAliasDef,
|
|
|
|
Mod in mods -> ast::Module,
|
|
|
|
MacroCall in macro_calls -> ast::MacroCall,
|
2020-06-17 16:43:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 14:33:01 +00:00
|
|
|
macro_rules! impl_index {
|
|
|
|
( $($fld:ident: $t:ty),+ $(,)? ) => {
|
|
|
|
$(
|
|
|
|
impl Index<Idx<$t>> for ItemTree {
|
|
|
|
type Output = $t;
|
|
|
|
|
|
|
|
fn index(&self, index: Idx<$t>) -> &Self::Output {
|
|
|
|
&self.$fld[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:41:32 +00:00
|
|
|
impl_index!(fields: Field, variants: Variant, exprs: Expr);
|
2020-03-25 14:33:01 +00:00
|
|
|
|
2020-06-16 17:20:29 +00:00
|
|
|
impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
|
|
|
|
type Output = N;
|
|
|
|
fn index(&self, id: FileItemTreeId<N>) -> &N {
|
|
|
|
N::lookup(self, id.index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 13:07:06 +00:00
|
|
|
/// A desugared `use` import.
|
2020-06-12 21:24:26 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Import {
|
|
|
|
pub path: ModPath,
|
|
|
|
pub alias: Option<ImportAlias>,
|
|
|
|
pub visibility: RawVisibility,
|
|
|
|
pub is_glob: bool,
|
|
|
|
pub is_prelude: bool,
|
2020-06-22 13:07:06 +00:00
|
|
|
/// AST ID of the `use` or `extern crate` item this import was derived from. Note that many
|
|
|
|
/// `Import`s can map to the same `use` item.
|
|
|
|
pub ast_id: FileAstId<ast::UseItem>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct ExternCrate {
|
|
|
|
pub path: ModPath,
|
|
|
|
pub alias: Option<ImportAlias>,
|
|
|
|
pub visibility: RawVisibility,
|
|
|
|
/// Whether this is a `#[macro_use] extern crate ...`.
|
2020-03-25 14:33:01 +00:00
|
|
|
pub is_macro_use: bool,
|
2020-06-22 13:07:06 +00:00
|
|
|
pub ast_id: FileAstId<ast::ExternCrateItem>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Function {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-17 10:24:05 +00:00
|
|
|
pub generic_params: GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub has_self_param: bool,
|
2020-06-22 14:41:10 +00:00
|
|
|
pub is_unsafe: bool,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub params: Vec<TypeRef>,
|
|
|
|
pub ret_type: TypeRef,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::FnDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Struct {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-17 10:24:05 +00:00
|
|
|
pub generic_params: GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub fields: Fields,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::StructDef>,
|
|
|
|
pub kind: StructDefKind,
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-06-15 17:16:02 +00:00
|
|
|
pub enum StructDefKind {
|
|
|
|
/// `struct S { ... }` - type namespace only.
|
|
|
|
Record,
|
|
|
|
/// `struct S(...);`
|
|
|
|
Tuple,
|
|
|
|
/// `struct S;`
|
|
|
|
Unit,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Union {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-17 10:24:05 +00:00
|
|
|
pub generic_params: GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub fields: Fields,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::UnionDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Enum {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-17 10:24:05 +00:00
|
|
|
pub generic_params: GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub variants: Range<Idx<Variant>>,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::EnumDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Const {
|
|
|
|
/// const _: () = ();
|
|
|
|
pub name: Option<Name>,
|
|
|
|
pub visibility: RawVisibility,
|
|
|
|
pub type_ref: TypeRef,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::ConstDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Static {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-22 14:41:10 +00:00
|
|
|
pub mutable: bool,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub type_ref: TypeRef,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::StaticDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Trait {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-17 10:24:05 +00:00
|
|
|
pub generic_params: GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub auto: bool,
|
|
|
|
pub items: Vec<AssocItem>,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::TraitDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Impl {
|
2020-06-17 10:24:05 +00:00
|
|
|
pub generic_params: GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub target_trait: Option<TypeRef>,
|
|
|
|
pub target_type: TypeRef,
|
|
|
|
pub is_negative: bool,
|
|
|
|
pub items: Vec<AssocItem>,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::ImplDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct TypeAlias {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-22 14:41:10 +00:00
|
|
|
/// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
|
|
|
|
pub bounds: Vec<TypeBound>,
|
2020-06-17 10:24:05 +00:00
|
|
|
pub generic_params: GenericParams,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub type_ref: Option<TypeRef>,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub ast_id: FileAstId<ast::TypeAliasDef>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Mod {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibility,
|
2020-06-15 17:16:02 +00:00
|
|
|
pub kind: ModKind,
|
|
|
|
pub ast_id: FileAstId<ast::Module>,
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-06-12 21:24:26 +00:00
|
|
|
pub enum ModKind {
|
2020-06-15 17:16:02 +00:00
|
|
|
/// `mod m { ... }`
|
2020-06-12 21:24:26 +00:00
|
|
|
Inline { items: Vec<ModItem> },
|
|
|
|
|
2020-06-15 17:16:02 +00:00
|
|
|
/// `mod m;`
|
2020-06-12 21:24:26 +00:00
|
|
|
Outline {},
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct MacroCall {
|
2020-06-15 17:16:02 +00:00
|
|
|
/// For `macro_rules!` declarations, this is the name of the declared macro.
|
2020-03-25 14:33:01 +00:00
|
|
|
pub name: Option<Name>,
|
2020-06-15 17:16:02 +00:00
|
|
|
/// Path to the called macro.
|
2020-03-25 14:33:01 +00:00
|
|
|
pub path: ModPath,
|
2020-06-12 11:58:02 +00:00
|
|
|
/// Has `#[macro_export]`.
|
|
|
|
pub is_export: bool,
|
|
|
|
/// Has `#[macro_export(local_inner_macros)]`.
|
|
|
|
pub is_local_inner: bool,
|
|
|
|
/// Has `#[rustc_builtin_macro]`.
|
|
|
|
pub is_builtin: bool,
|
2020-03-25 14:33:01 +00:00
|
|
|
pub ast_id: FileAstId<ast::MacroCall>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array
|
|
|
|
// lengths, but we don't do much with them yet.
|
2020-06-17 16:43:41 +00:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Expr;
|
|
|
|
|
2020-06-11 17:46:56 +00:00
|
|
|
macro_rules! impl_froms {
|
|
|
|
($e:ident { $($v:ident ($t:ty)),* $(,)? }) => {
|
|
|
|
$(
|
|
|
|
impl From<$t> for $e {
|
|
|
|
fn from(it: $t) -> $e {
|
|
|
|
$e::$v(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 16:43:41 +00:00
|
|
|
impl ModItem {
|
|
|
|
pub fn as_assoc_item(&self) -> Option<AssocItem> {
|
|
|
|
match self {
|
|
|
|
ModItem::Import(_)
|
2020-06-22 13:07:06 +00:00
|
|
|
| ModItem::ExternCrate(_)
|
2020-06-17 16:43:41 +00:00
|
|
|
| ModItem::Struct(_)
|
|
|
|
| ModItem::Union(_)
|
|
|
|
| ModItem::Enum(_)
|
|
|
|
| ModItem::Static(_)
|
|
|
|
| ModItem::Trait(_)
|
|
|
|
| ModItem::Impl(_)
|
|
|
|
| ModItem::Mod(_) => None,
|
|
|
|
ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
|
|
|
|
ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
|
|
|
|
ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
|
|
|
|
ModItem::Function(func) => Some(AssocItem::Function(*func)),
|
|
|
|
}
|
|
|
|
}
|
2020-06-22 13:07:06 +00:00
|
|
|
|
|
|
|
pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
|
|
|
|
N::id_from_mod_item(self)
|
|
|
|
}
|
2020-06-17 16:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub enum AssocItem {
|
2020-06-16 17:20:29 +00:00
|
|
|
Function(FileItemTreeId<Function>),
|
|
|
|
TypeAlias(FileItemTreeId<TypeAlias>),
|
|
|
|
Const(FileItemTreeId<Const>),
|
|
|
|
MacroCall(FileItemTreeId<MacroCall>),
|
2020-03-25 14:33:01 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 17:46:56 +00:00
|
|
|
impl_froms!(AssocItem {
|
2020-06-16 17:20:29 +00:00
|
|
|
Function(FileItemTreeId<Function>),
|
|
|
|
TypeAlias(FileItemTreeId<TypeAlias>),
|
|
|
|
Const(FileItemTreeId<Const>),
|
|
|
|
MacroCall(FileItemTreeId<MacroCall>),
|
2020-06-11 17:46:56 +00:00
|
|
|
});
|
|
|
|
|
2020-06-22 13:07:06 +00:00
|
|
|
impl From<AssocItem> for ModItem {
|
|
|
|
fn from(item: AssocItem) -> Self {
|
|
|
|
match item {
|
|
|
|
AssocItem::Function(it) => it.into(),
|
|
|
|
AssocItem::TypeAlias(it) => it.into(),
|
|
|
|
AssocItem::Const(it) => it.into(),
|
|
|
|
AssocItem::MacroCall(it) => it.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:46:56 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-03-25 14:33:01 +00:00
|
|
|
pub struct Variant {
|
|
|
|
pub name: Name,
|
|
|
|
pub fields: Fields,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Fields {
|
|
|
|
Record(Range<Idx<Field>>),
|
|
|
|
Tuple(Range<Idx<Field>>),
|
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single field of an enum variant or struct
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Field {
|
|
|
|
pub name: Name,
|
|
|
|
pub type_ref: TypeRef,
|
|
|
|
pub visibility: RawVisibility,
|
|
|
|
}
|