2020-03-25 09:33:01 -05:00
|
|
|
//! A simplified AST that only contains items.
|
|
|
|
|
2020-06-16 07:52:43 -05:00
|
|
|
mod lower;
|
|
|
|
|
|
|
|
use std::{
|
2020-06-25 09:47:21 -05:00
|
|
|
any::type_name,
|
2020-06-16 12:20:29 -05:00
|
|
|
fmt::{self, Debug},
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
marker::PhantomData,
|
2020-06-16 07:52:43 -05:00
|
|
|
ops::{Index, Range},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
2020-12-18 13:37:26 -06:00
|
|
|
use ast::{AstNode, NameOwner, StructKind};
|
2020-12-17 17:23:46 -06:00
|
|
|
use base_db::CrateId;
|
2020-06-16 07:52:43 -05:00
|
|
|
use either::Either;
|
2020-03-25 09:33:01 -05:00
|
|
|
use hir_expand::{
|
2020-06-16 07:52:43 -05:00
|
|
|
ast_id_map::FileAstId,
|
2020-03-25 09:33:01 -05:00
|
|
|
hygiene::Hygiene,
|
|
|
|
name::{name, AsName, Name},
|
2020-06-12 06:58:02 -05:00
|
|
|
HirFileId, InFile,
|
2020-03-25 09:33:01 -05:00
|
|
|
};
|
2021-01-14 18:02:08 -06:00
|
|
|
use la_arena::{Arena, Idx, RawIdx};
|
2021-01-21 10:04:50 -06:00
|
|
|
use profile::Count;
|
2020-06-16 07:52:43 -05:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-06-24 09:50:23 -05:00
|
|
|
use smallvec::SmallVec;
|
2021-02-01 06:19:55 -06:00
|
|
|
use syntax::{ast, match_ast, SyntaxKind};
|
2020-03-25 09:33:01 -05:00
|
|
|
|
|
|
|
use crate::{
|
2020-12-17 17:23:46 -06:00
|
|
|
attr::{Attrs, RawAttrs},
|
2020-06-11 12:46:56 -05:00
|
|
|
db::DefDatabase,
|
2020-06-17 05:24:05 -05:00
|
|
|
generics::GenericParams,
|
2020-06-24 08:36:18 -05:00
|
|
|
path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind},
|
2020-03-25 09:33:01 -05:00
|
|
|
type_ref::{Mutability, TypeBound, TypeRef},
|
|
|
|
visibility::RawVisibility,
|
|
|
|
};
|
2020-06-24 08:36:18 -05:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub struct RawVisibilityId(u32);
|
|
|
|
|
|
|
|
impl RawVisibilityId {
|
|
|
|
pub const PUB: Self = RawVisibilityId(u32::max_value());
|
|
|
|
pub const PRIV: Self = RawVisibilityId(u32::max_value() - 1);
|
|
|
|
pub const PUB_CRATE: Self = RawVisibilityId(u32::max_value() - 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for RawVisibilityId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut f = f.debug_tuple("RawVisibilityId");
|
|
|
|
match *self {
|
|
|
|
Self::PUB => f.field(&"pub"),
|
|
|
|
Self::PRIV => f.field(&"pub(self)"),
|
|
|
|
Self::PUB_CRATE => f.field(&"pub(crate)"),
|
|
|
|
_ => f.field(&self.0),
|
|
|
|
};
|
|
|
|
f.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:54:35 -05:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
|
|
|
pub struct GenericParamsId(u32);
|
|
|
|
|
|
|
|
impl GenericParamsId {
|
|
|
|
pub const EMPTY: Self = GenericParamsId(u32::max_value());
|
|
|
|
}
|
|
|
|
|
2020-06-23 12:42:19 -05:00
|
|
|
/// The item tree of a source file.
|
2021-01-25 06:12:53 -06:00
|
|
|
#[derive(Debug, Default, Eq, PartialEq)]
|
2020-06-23 12:42:19 -05:00
|
|
|
pub struct ItemTree {
|
2021-01-21 10:04:50 -06:00
|
|
|
_c: Count<Self>,
|
|
|
|
|
2020-06-23 12:42:19 -05:00
|
|
|
top_level: SmallVec<[ModItem; 1]>,
|
2020-12-17 17:23:46 -06:00
|
|
|
attrs: FxHashMap<AttrOwner, RawAttrs>,
|
2020-06-23 12:42:19 -05:00
|
|
|
|
|
|
|
data: Option<Box<ItemTreeData>>,
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:33:01 -05:00
|
|
|
impl ItemTree {
|
2021-01-20 07:49:04 -06:00
|
|
|
pub(crate) fn item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
|
2020-08-12 09:32:36 -05:00
|
|
|
let _p = profile::span("item_tree_query").detail(|| format!("{:?}", file_id));
|
2020-06-11 12:46:56 -05:00
|
|
|
let syntax = if let Some(node) = db.parse_or_expand(file_id) {
|
2021-02-01 06:19:55 -06:00
|
|
|
if node.kind() == SyntaxKind::ERROR {
|
|
|
|
// FIXME: not 100% sure why these crop up, but return an empty tree to avoid a panic
|
|
|
|
return Default::default();
|
|
|
|
}
|
2020-06-11 12:46:56 -05:00
|
|
|
node
|
|
|
|
} else {
|
2021-01-25 06:12:53 -06:00
|
|
|
return Default::default();
|
2020-06-11 12:46:56 -05:00
|
|
|
};
|
|
|
|
|
2020-06-15 12:16:02 -05:00
|
|
|
let hygiene = Hygiene::new(db.upcast(), file_id);
|
2020-06-23 06:46:38 -05:00
|
|
|
let ctx = lower::Ctx::new(db, hygiene.clone(), file_id);
|
2020-06-15 12:16:02 -05:00
|
|
|
let mut top_attrs = None;
|
2020-06-23 06:46:38 -05:00
|
|
|
let mut item_tree = match_ast! {
|
2020-06-11 12:46:56 -05:00
|
|
|
match syntax {
|
|
|
|
ast::SourceFile(file) => {
|
2020-12-17 17:23:46 -06:00
|
|
|
top_attrs = Some(RawAttrs::new(&file, &hygiene));
|
2020-06-23 06:46:38 -05:00
|
|
|
ctx.lower_module_items(&file)
|
|
|
|
},
|
|
|
|
ast::MacroItems(items) => {
|
|
|
|
ctx.lower_module_items(&items)
|
|
|
|
},
|
2020-12-15 00:39:15 -06:00
|
|
|
ast::MacroStmts(stmts) => {
|
2021-01-28 11:00:10 -06:00
|
|
|
// The produced statements can include items, which should be added as top-level
|
|
|
|
// items.
|
|
|
|
ctx.lower_macro_stmts(stmts)
|
2020-12-15 00:39:15 -06:00
|
|
|
},
|
2020-06-23 06:46:38 -05:00
|
|
|
ast::Expr(e) => {
|
2021-01-28 11:00:10 -06:00
|
|
|
// Macros can expand to expressions. We return an empty item tree in this case, but
|
|
|
|
// still need to collect inner items.
|
2020-06-23 06:46:38 -05:00
|
|
|
ctx.lower_inner_items(e.syntax())
|
|
|
|
},
|
|
|
|
_ => {
|
2021-01-28 11:00:10 -06:00
|
|
|
panic!("cannot create item tree from {:?} {}", syntax, syntax);
|
2020-06-11 12:46:56 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-23 12:42:19 -05:00
|
|
|
if let Some(attrs) = top_attrs {
|
|
|
|
item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
|
|
|
|
}
|
2020-06-24 09:14:58 -05:00
|
|
|
item_tree.shrink_to_fit();
|
2020-06-22 08:07:06 -05:00
|
|
|
Arc::new(item_tree)
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:14:58 -05:00
|
|
|
fn shrink_to_fit(&mut self) {
|
|
|
|
if let Some(data) = &mut self.data {
|
|
|
|
let ItemTreeData {
|
|
|
|
imports,
|
|
|
|
extern_crates,
|
|
|
|
functions,
|
|
|
|
structs,
|
|
|
|
fields,
|
|
|
|
unions,
|
|
|
|
enums,
|
|
|
|
variants,
|
|
|
|
consts,
|
|
|
|
statics,
|
|
|
|
traits,
|
|
|
|
impls,
|
|
|
|
type_aliases,
|
|
|
|
mods,
|
|
|
|
macro_calls,
|
2020-12-15 08:37:37 -06:00
|
|
|
macro_rules,
|
2020-12-15 11:43:19 -06:00
|
|
|
macro_defs,
|
2020-06-24 09:14:58 -05:00
|
|
|
vis,
|
|
|
|
generics,
|
2021-02-04 12:19:51 -06:00
|
|
|
type_refs,
|
2021-01-20 07:49:04 -06:00
|
|
|
inner_items,
|
2020-06-24 09:14:58 -05:00
|
|
|
} = &mut **data;
|
|
|
|
|
|
|
|
imports.shrink_to_fit();
|
|
|
|
extern_crates.shrink_to_fit();
|
|
|
|
functions.shrink_to_fit();
|
|
|
|
structs.shrink_to_fit();
|
|
|
|
fields.shrink_to_fit();
|
|
|
|
unions.shrink_to_fit();
|
|
|
|
enums.shrink_to_fit();
|
|
|
|
variants.shrink_to_fit();
|
|
|
|
consts.shrink_to_fit();
|
|
|
|
statics.shrink_to_fit();
|
|
|
|
traits.shrink_to_fit();
|
|
|
|
impls.shrink_to_fit();
|
|
|
|
type_aliases.shrink_to_fit();
|
|
|
|
mods.shrink_to_fit();
|
|
|
|
macro_calls.shrink_to_fit();
|
2020-12-15 08:37:37 -06:00
|
|
|
macro_rules.shrink_to_fit();
|
2020-12-15 11:43:19 -06:00
|
|
|
macro_defs.shrink_to_fit();
|
2020-06-24 09:14:58 -05:00
|
|
|
|
|
|
|
vis.arena.shrink_to_fit();
|
|
|
|
generics.arena.shrink_to_fit();
|
2021-02-04 12:19:51 -06:00
|
|
|
type_refs.arena.shrink_to_fit();
|
|
|
|
type_refs.map.shrink_to_fit();
|
2021-01-20 07:49:04 -06:00
|
|
|
|
|
|
|
inner_items.shrink_to_fit();
|
2020-06-24 09:14:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:46:56 -05:00
|
|
|
/// Returns an iterator over all items located at the top level of the `HirFileId` this
|
|
|
|
/// `ItemTree` was created from.
|
2020-06-12 16:24:26 -05:00
|
|
|
pub fn top_level_items(&self) -> &[ModItem] {
|
|
|
|
&self.top_level
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
2020-06-15 12:16:02 -05:00
|
|
|
|
|
|
|
/// Returns the inner attributes of the source file.
|
2020-12-17 17:23:46 -06:00
|
|
|
pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
|
|
|
|
self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&RawAttrs::EMPTY).clone().filter(db, krate)
|
2020-06-15 12:16:02 -05:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:23:46 -06:00
|
|
|
pub(crate) fn raw_attrs(&self, of: AttrOwner) -> &RawAttrs {
|
|
|
|
self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn attrs(&self, db: &dyn DefDatabase, krate: CrateId, of: AttrOwner) -> Attrs {
|
|
|
|
self.raw_attrs(of).clone().filter(db, krate)
|
2020-06-15 12:16:02 -05:00
|
|
|
}
|
2020-06-22 08:07:06 -05:00
|
|
|
|
|
|
|
pub fn all_inner_items(&self) -> impl Iterator<Item = ModItem> + '_ {
|
2021-01-20 07:49:04 -06:00
|
|
|
match &self.data {
|
|
|
|
Some(data) => Some(data.inner_items.values().flatten().copied()).into_iter().flatten(),
|
|
|
|
None => None.into_iter().flatten(),
|
|
|
|
}
|
2020-06-22 08:07:06 -05:00
|
|
|
}
|
|
|
|
|
2021-01-21 08:22:17 -06:00
|
|
|
pub fn inner_items_of_block(&self, block: FileAstId<ast::BlockExpr>) -> &[ModItem] {
|
|
|
|
match &self.data {
|
|
|
|
Some(data) => data.inner_items.get(&block).map(|it| &**it).unwrap_or(&[]),
|
|
|
|
None => &[],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 12:42:19 -05:00
|
|
|
fn data(&self) -> &ItemTreeData {
|
|
|
|
self.data.as_ref().expect("attempted to access data of empty ItemTree")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn data_mut(&mut self) -> &mut ItemTreeData {
|
|
|
|
self.data.get_or_insert_with(Box::default)
|
|
|
|
}
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-24 09:50:23 -05:00
|
|
|
#[derive(Default, Debug, Eq, PartialEq)]
|
|
|
|
struct ItemVisibilities {
|
|
|
|
arena: Arena<RawVisibility>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemVisibilities {
|
|
|
|
fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId {
|
|
|
|
match &vis {
|
|
|
|
RawVisibility::Public => RawVisibilityId::PUB,
|
2021-02-04 13:49:24 -06:00
|
|
|
RawVisibility::Module(path) if path.segments().is_empty() => match &path.kind {
|
2020-06-24 09:50:23 -05:00
|
|
|
PathKind::Super(0) => RawVisibilityId::PRIV,
|
|
|
|
PathKind::Crate => RawVisibilityId::PUB_CRATE,
|
|
|
|
_ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
|
|
|
|
},
|
|
|
|
_ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static VIS_PUB: RawVisibility = RawVisibility::Public;
|
2021-02-04 13:49:24 -06:00
|
|
|
static VIS_PRIV: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)));
|
|
|
|
static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate));
|
2020-06-24 09:50:23 -05:00
|
|
|
|
|
|
|
#[derive(Default, Debug, Eq, PartialEq)]
|
|
|
|
struct GenericParamsStorage {
|
|
|
|
arena: Arena<GenericParams>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericParamsStorage {
|
|
|
|
fn alloc(&mut self, params: GenericParams) -> GenericParamsId {
|
2020-12-13 04:34:44 -06:00
|
|
|
if params.types.is_empty()
|
|
|
|
&& params.lifetimes.is_empty()
|
2021-01-01 03:06:42 -06:00
|
|
|
&& params.consts.is_empty()
|
2020-12-13 04:34:44 -06:00
|
|
|
&& params.where_predicates.is_empty()
|
|
|
|
{
|
2020-06-24 09:50:23 -05:00
|
|
|
return GenericParamsId::EMPTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
GenericParamsId(self.arena.alloc(params).into_raw().into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 03:06:42 -06:00
|
|
|
static EMPTY_GENERICS: GenericParams = GenericParams {
|
|
|
|
types: Arena::new(),
|
|
|
|
lifetimes: Arena::new(),
|
|
|
|
consts: Arena::new(),
|
|
|
|
where_predicates: Vec::new(),
|
|
|
|
};
|
2020-06-24 09:50:23 -05:00
|
|
|
|
2021-02-04 12:19:51 -06:00
|
|
|
/// `TypeRef` interner.
|
|
|
|
#[derive(Default, Debug, Eq, PartialEq)]
|
|
|
|
struct TypeRefStorage {
|
|
|
|
arena: Arena<Arc<TypeRef>>,
|
|
|
|
map: FxHashMap<Arc<TypeRef>, Idx<Arc<TypeRef>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeRefStorage {
|
|
|
|
// Note: We lie about the `Idx<TypeRef>` to hide the interner details.
|
|
|
|
|
|
|
|
fn intern(&mut self, ty: TypeRef) -> Idx<TypeRef> {
|
|
|
|
if let Some(id) = self.map.get(&ty) {
|
|
|
|
return Idx::from_raw(id.into_raw());
|
|
|
|
}
|
|
|
|
|
|
|
|
let ty = Arc::new(ty);
|
|
|
|
let idx = self.arena.alloc(ty.clone());
|
|
|
|
self.map.insert(ty, idx);
|
|
|
|
Idx::from_raw(idx.into_raw())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lookup(&self, id: Idx<TypeRef>) -> &TypeRef {
|
|
|
|
&self.arena[Idx::from_raw(id.into_raw())]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:50:23 -05:00
|
|
|
#[derive(Default, Debug, Eq, PartialEq)]
|
|
|
|
struct ItemTreeData {
|
|
|
|
imports: Arena<Import>,
|
|
|
|
extern_crates: Arena<ExternCrate>,
|
|
|
|
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>,
|
2020-12-15 08:37:37 -06:00
|
|
|
macro_rules: Arena<MacroRules>,
|
2020-12-15 11:43:19 -06:00
|
|
|
macro_defs: Arena<MacroDef>,
|
2020-06-24 09:50:23 -05:00
|
|
|
|
|
|
|
vis: ItemVisibilities,
|
|
|
|
generics: GenericParamsStorage,
|
2021-02-04 12:19:51 -06:00
|
|
|
type_refs: TypeRefStorage,
|
2021-01-20 07:49:04 -06:00
|
|
|
|
|
|
|
inner_items: FxHashMap<FileAstId<ast::BlockExpr>, SmallVec<[ModItem; 1]>>,
|
2020-06-24 09:50:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Hash)]
|
2020-06-25 07:39:27 -05:00
|
|
|
pub enum AttrOwner {
|
2020-06-24 09:50:23 -05:00
|
|
|
/// Attributes on an item.
|
|
|
|
ModItem(ModItem),
|
|
|
|
/// Inner attributes of the source file.
|
|
|
|
TopLevel,
|
2020-06-25 07:39:27 -05:00
|
|
|
|
|
|
|
Variant(Idx<Variant>),
|
|
|
|
Field(Idx<Field>),
|
2020-06-24 09:50:23 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 07:39:27 -05:00
|
|
|
macro_rules! from_attrs {
|
|
|
|
( $( $var:ident($t:ty) ),+ ) => {
|
|
|
|
$(
|
|
|
|
impl From<$t> for AttrOwner {
|
|
|
|
fn from(t: $t) -> AttrOwner {
|
|
|
|
AttrOwner::$var(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
from_attrs!(ModItem(ModItem), Variant(Idx<Variant>), Field(Idx<Field>));
|
|
|
|
|
|
|
|
/// Trait implemented by all item nodes in the item tree.
|
2020-06-17 11:43:41 -05:00
|
|
|
pub trait ItemTreeNode: Clone {
|
2020-07-29 17:23:03 -05:00
|
|
|
type Source: AstNode + Into<ast::Item>;
|
2020-06-23 11:46:08 -05:00
|
|
|
|
|
|
|
fn ast_id(&self) -> FileAstId<Self::Source>;
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
/// Looks up an instance of `Self` in an item tree.
|
2020-06-16 12:20:29 -05:00
|
|
|
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
|
2020-06-22 08:07:06 -05: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 12:15:54 -05:00
|
|
|
|
|
|
|
/// Upcasts a `FileItemTreeId` to a generic `ModItem`.
|
|
|
|
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
|
2020-06-16 12:20:29 -05: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 11:41:32 -05: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 12:20:29 -05:00
|
|
|
}
|
2020-06-23 11:41:32 -05:00
|
|
|
)+
|
2020-06-22 08:07:06 -05:00
|
|
|
|
2020-06-23 11:41:32 -05:00
|
|
|
$(
|
|
|
|
impl ItemTreeNode for $typ {
|
2020-06-23 11:46:08 -05:00
|
|
|
type Source = $ast;
|
|
|
|
|
|
|
|
fn ast_id(&self) -> FileAstId<Self::Source> {
|
|
|
|
self.ast_id
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:41:32 -05:00
|
|
|
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
|
2020-06-23 12:42:19 -05:00
|
|
|
&tree.data().$fld[index]
|
2020-06-23 11:41:32 -05:00
|
|
|
}
|
2020-06-22 08:07:06 -05:00
|
|
|
|
2020-06-23 11:41:32 -05: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 08:07:06 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 12:15:54 -05:00
|
|
|
|
2020-06-23 11:41:32 -05:00
|
|
|
impl Index<Idx<$typ>> for ItemTree {
|
|
|
|
type Output = $typ;
|
|
|
|
|
|
|
|
fn index(&self, index: Idx<$typ>) -> &Self::Output {
|
2020-06-23 12:42:19 -05:00
|
|
|
&self.data().$fld[index]
|
2020-06-23 11:41:32 -05:00
|
|
|
}
|
2020-06-17 11:43:41 -05:00
|
|
|
}
|
2020-06-23 11:41:32 -05:00
|
|
|
)+
|
|
|
|
};
|
2020-06-17 11:43:41 -05:00
|
|
|
}
|
|
|
|
|
2020-06-23 11:41:32 -05:00
|
|
|
mod_items! {
|
2020-07-30 07:12:04 -05:00
|
|
|
Import in imports -> ast::Use,
|
2020-07-30 05:26:57 -05:00
|
|
|
ExternCrate in extern_crates -> ast::ExternCrate,
|
2020-07-30 07:51:08 -05:00
|
|
|
Function in functions -> ast::Fn,
|
2020-07-30 10:50:40 -05:00
|
|
|
Struct in structs -> ast::Struct,
|
2020-07-30 10:36:46 -05:00
|
|
|
Union in unions -> ast::Union,
|
2020-07-30 10:52:53 -05:00
|
|
|
Enum in enums -> ast::Enum,
|
2020-07-30 11:02:20 -05:00
|
|
|
Const in consts -> ast::Const,
|
|
|
|
Static in statics -> ast::Static,
|
2020-07-30 11:17:28 -05:00
|
|
|
Trait in traits -> ast::Trait,
|
2020-07-30 11:28:28 -05:00
|
|
|
Impl in impls -> ast::Impl,
|
2020-07-30 08:25:46 -05:00
|
|
|
TypeAlias in type_aliases -> ast::TypeAlias,
|
2020-06-23 11:41:32 -05:00
|
|
|
Mod in mods -> ast::Module,
|
|
|
|
MacroCall in macro_calls -> ast::MacroCall,
|
2020-12-15 08:37:37 -06:00
|
|
|
MacroRules in macro_rules -> ast::MacroRules,
|
2020-12-15 11:43:19 -06:00
|
|
|
MacroDef in macro_defs -> ast::MacroDef,
|
2020-06-17 11:43:41 -05:00
|
|
|
}
|
|
|
|
|
2020-03-25 09:33:01 -05: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 {
|
2020-06-23 12:42:19 -05:00
|
|
|
&self.data().$fld[index]
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:05:40 -06:00
|
|
|
impl_index!(fields: Field, variants: Variant);
|
2020-03-25 09:33:01 -05:00
|
|
|
|
2020-06-24 08:36:18 -05:00
|
|
|
impl Index<RawVisibilityId> for ItemTree {
|
|
|
|
type Output = RawVisibility;
|
|
|
|
fn index(&self, index: RawVisibilityId) -> &Self::Output {
|
|
|
|
match index {
|
|
|
|
RawVisibilityId::PRIV => &VIS_PRIV,
|
|
|
|
RawVisibilityId::PUB => &VIS_PUB,
|
|
|
|
RawVisibilityId::PUB_CRATE => &VIS_PUB_CRATE,
|
|
|
|
_ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 08:54:35 -05:00
|
|
|
impl Index<GenericParamsId> for ItemTree {
|
|
|
|
type Output = GenericParams;
|
|
|
|
|
|
|
|
fn index(&self, index: GenericParamsId) -> &Self::Output {
|
|
|
|
match index {
|
|
|
|
GenericParamsId::EMPTY => &EMPTY_GENERICS,
|
|
|
|
_ => &self.data().generics.arena[Idx::from_raw(index.0.into())],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 12:19:51 -06:00
|
|
|
impl Index<Idx<TypeRef>> for ItemTree {
|
|
|
|
type Output = TypeRef;
|
|
|
|
|
|
|
|
fn index(&self, id: Idx<TypeRef>) -> &Self::Output {
|
|
|
|
self.data().type_refs.lookup(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 12:20:29 -05: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 08:07:06 -05:00
|
|
|
/// A desugared `use` import.
|
2020-06-12 16:24:26 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Import {
|
|
|
|
pub path: ModPath,
|
|
|
|
pub alias: Option<ImportAlias>,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-03-25 09:33:01 -05:00
|
|
|
pub is_glob: bool,
|
2020-06-22 08:07:06 -05: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.
|
2020-07-30 07:12:04 -05:00
|
|
|
pub ast_id: FileAstId<ast::Use>,
|
2020-09-16 05:35:09 -05:00
|
|
|
/// Index of this `Import` when the containing `Use` is visited via `ModPath::expand_use_item`.
|
|
|
|
///
|
|
|
|
/// This can be used to get the `UseTree` this `Import` corresponds to and allows emitting
|
|
|
|
/// precise diagnostics.
|
|
|
|
pub index: usize,
|
2020-06-22 08:07:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct ExternCrate {
|
2020-09-17 08:28:23 -05:00
|
|
|
pub name: Name,
|
2020-06-22 08:07:06 -05:00
|
|
|
pub alias: Option<ImportAlias>,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-07-30 05:26:57 -05:00
|
|
|
pub ast_id: FileAstId<ast::ExternCrate>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Function {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-24 08:54:35 -05:00
|
|
|
pub generic_params: GenericParamsId,
|
2020-03-25 09:33:01 -05:00
|
|
|
pub has_self_param: bool,
|
2020-09-03 02:55:24 -05:00
|
|
|
pub has_body: bool,
|
2020-06-22 09:41:10 -05:00
|
|
|
pub is_unsafe: bool,
|
2020-12-10 08:45:01 -06:00
|
|
|
/// Whether the function is located in an `extern` block (*not* whether it is an
|
|
|
|
/// `extern "abi" fn`).
|
|
|
|
pub is_extern: bool,
|
2021-02-04 12:19:51 -06:00
|
|
|
pub params: Box<[Idx<TypeRef>]>,
|
2020-07-14 11:23:45 -05:00
|
|
|
pub is_varargs: bool,
|
2021-02-04 12:19:51 -06:00
|
|
|
pub ret_type: Idx<TypeRef>,
|
2020-07-30 07:51:08 -05:00
|
|
|
pub ast_id: FileAstId<ast::Fn>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Struct {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-24 08:54:35 -05:00
|
|
|
pub generic_params: GenericParamsId,
|
2020-03-25 09:33:01 -05:00
|
|
|
pub fields: Fields,
|
2020-07-30 10:50:40 -05:00
|
|
|
pub ast_id: FileAstId<ast::Struct>,
|
2020-06-15 12:16:02 -05:00
|
|
|
pub kind: StructDefKind,
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-06-15 12:16:02 -05:00
|
|
|
pub enum StructDefKind {
|
|
|
|
/// `struct S { ... }` - type namespace only.
|
|
|
|
Record,
|
|
|
|
/// `struct S(...);`
|
|
|
|
Tuple,
|
|
|
|
/// `struct S;`
|
|
|
|
Unit,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Union {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-24 08:54:35 -05:00
|
|
|
pub generic_params: GenericParamsId,
|
2020-03-25 09:33:01 -05:00
|
|
|
pub fields: Fields,
|
2020-07-30 10:36:46 -05:00
|
|
|
pub ast_id: FileAstId<ast::Union>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Enum {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-24 08:54:35 -05:00
|
|
|
pub generic_params: GenericParamsId,
|
2020-06-25 09:47:21 -05:00
|
|
|
pub variants: IdRange<Variant>,
|
2020-07-30 10:52:53 -05:00
|
|
|
pub ast_id: FileAstId<ast::Enum>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Const {
|
|
|
|
/// const _: () = ();
|
|
|
|
pub name: Option<Name>,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2021-02-04 12:19:51 -06:00
|
|
|
pub type_ref: Idx<TypeRef>,
|
2020-07-30 11:02:20 -05:00
|
|
|
pub ast_id: FileAstId<ast::Const>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Static {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-22 09:41:10 -05:00
|
|
|
pub mutable: bool,
|
2020-12-10 08:45:01 -06:00
|
|
|
/// Whether the static is in an `extern` block.
|
|
|
|
pub is_extern: bool,
|
2021-02-04 12:19:51 -06:00
|
|
|
pub type_ref: Idx<TypeRef>,
|
2020-07-30 11:02:20 -05:00
|
|
|
pub ast_id: FileAstId<ast::Static>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Trait {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-24 08:54:35 -05:00
|
|
|
pub generic_params: GenericParamsId,
|
2020-03-25 09:33:01 -05:00
|
|
|
pub auto: bool,
|
2020-06-24 09:07:02 -05:00
|
|
|
pub items: Box<[AssocItem]>,
|
2020-07-30 11:17:28 -05:00
|
|
|
pub ast_id: FileAstId<ast::Trait>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Impl {
|
2020-06-24 08:54:35 -05:00
|
|
|
pub generic_params: GenericParamsId,
|
2021-02-04 12:19:51 -06:00
|
|
|
pub target_trait: Option<Idx<TypeRef>>,
|
|
|
|
pub target_type: Idx<TypeRef>,
|
2020-03-25 09:33:01 -05:00
|
|
|
pub is_negative: bool,
|
2020-06-24 09:07:02 -05:00
|
|
|
pub items: Box<[AssocItem]>,
|
2020-07-30 11:28:28 -05:00
|
|
|
pub ast_id: FileAstId<ast::Impl>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct TypeAlias {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-22 09:41:10 -05:00
|
|
|
/// Bounds on the type alias itself. Only valid in trait declarations, eg. `type Assoc: Copy;`.
|
2020-06-24 09:07:02 -05:00
|
|
|
pub bounds: Box<[TypeBound]>,
|
2020-06-24 08:54:35 -05:00
|
|
|
pub generic_params: GenericParamsId,
|
2021-02-04 12:19:51 -06:00
|
|
|
pub type_ref: Option<Idx<TypeRef>>,
|
2020-09-12 21:24:19 -05:00
|
|
|
pub is_extern: bool,
|
2020-07-30 08:25:46 -05:00
|
|
|
pub ast_id: FileAstId<ast::TypeAlias>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Mod {
|
|
|
|
pub name: Name,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-06-15 12:16:02 -05:00
|
|
|
pub kind: ModKind,
|
|
|
|
pub ast_id: FileAstId<ast::Module>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-06-12 16:24:26 -05:00
|
|
|
pub enum ModKind {
|
2020-06-15 12:16:02 -05:00
|
|
|
/// `mod m { ... }`
|
2020-06-24 09:07:02 -05:00
|
|
|
Inline { items: Box<[ModItem]> },
|
2020-06-12 16:24:26 -05:00
|
|
|
|
2020-06-15 12:16:02 -05:00
|
|
|
/// `mod m;`
|
2020-06-12 16:24:26 -05:00
|
|
|
Outline {},
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:41 -05:00
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct MacroCall {
|
2020-06-15 12:16:02 -05:00
|
|
|
/// Path to the called macro.
|
2020-03-25 09:33:01 -05:00
|
|
|
pub path: ModPath,
|
2020-12-15 08:37:37 -06:00
|
|
|
pub ast_id: FileAstId<ast::MacroCall>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct MacroRules {
|
2020-12-15 11:43:19 -06:00
|
|
|
/// The name of the declared macro.
|
2020-12-15 08:37:37 -06:00
|
|
|
pub name: Name,
|
|
|
|
pub ast_id: FileAstId<ast::MacroRules>,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-12-15 11:43:19 -06:00
|
|
|
/// "Macros 2.0" macro definition.
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
|
|
pub struct MacroDef {
|
|
|
|
pub name: Name,
|
|
|
|
pub visibility: RawVisibilityId,
|
|
|
|
pub ast_id: FileAstId<ast::MacroDef>,
|
|
|
|
}
|
|
|
|
|
2020-06-11 12:46:56 -05: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 11:43:41 -05:00
|
|
|
impl ModItem {
|
|
|
|
pub fn as_assoc_item(&self) -> Option<AssocItem> {
|
|
|
|
match self {
|
|
|
|
ModItem::Import(_)
|
2020-06-22 08:07:06 -05:00
|
|
|
| ModItem::ExternCrate(_)
|
2020-06-17 11:43:41 -05:00
|
|
|
| ModItem::Struct(_)
|
|
|
|
| ModItem::Union(_)
|
|
|
|
| ModItem::Enum(_)
|
|
|
|
| ModItem::Static(_)
|
|
|
|
| ModItem::Trait(_)
|
|
|
|
| ModItem::Impl(_)
|
2020-12-15 08:37:37 -06:00
|
|
|
| ModItem::Mod(_)
|
2020-12-15 11:43:19 -06:00
|
|
|
| ModItem::MacroRules(_)
|
|
|
|
| ModItem::MacroDef(_) => None,
|
2020-06-17 11:43:41 -05:00
|
|
|
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 08:07:06 -05:00
|
|
|
|
|
|
|
pub fn downcast<N: ItemTreeNode>(self) -> Option<FileItemTreeId<N>> {
|
|
|
|
N::id_from_mod_item(self)
|
|
|
|
}
|
2020-10-20 10:49:21 -05:00
|
|
|
|
|
|
|
pub fn ast_id(&self, tree: &ItemTree) -> FileAstId<ast::Item> {
|
|
|
|
match self {
|
|
|
|
ModItem::Import(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::ExternCrate(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Function(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Struct(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Union(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Enum(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Const(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Static(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Trait(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Impl(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::TypeAlias(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::Mod(it) => tree[it.index].ast_id().upcast(),
|
|
|
|
ModItem::MacroCall(it) => tree[it.index].ast_id().upcast(),
|
2020-12-15 08:37:37 -06:00
|
|
|
ModItem::MacroRules(it) => tree[it.index].ast_id().upcast(),
|
2020-12-15 11:43:19 -06:00
|
|
|
ModItem::MacroDef(it) => tree[it.index].ast_id().upcast(),
|
2020-10-20 10:49:21 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 11:43:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub enum AssocItem {
|
2020-06-16 12:20:29 -05:00
|
|
|
Function(FileItemTreeId<Function>),
|
|
|
|
TypeAlias(FileItemTreeId<TypeAlias>),
|
|
|
|
Const(FileItemTreeId<Const>),
|
|
|
|
MacroCall(FileItemTreeId<MacroCall>),
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:46:56 -05:00
|
|
|
impl_froms!(AssocItem {
|
2020-06-16 12:20:29 -05:00
|
|
|
Function(FileItemTreeId<Function>),
|
|
|
|
TypeAlias(FileItemTreeId<TypeAlias>),
|
|
|
|
Const(FileItemTreeId<Const>),
|
|
|
|
MacroCall(FileItemTreeId<MacroCall>),
|
2020-06-11 12:46:56 -05:00
|
|
|
});
|
|
|
|
|
2020-06-22 08:07:06 -05: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 12:46:56 -05:00
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2020-03-25 09:33:01 -05:00
|
|
|
pub struct Variant {
|
|
|
|
pub name: Name,
|
|
|
|
pub fields: Fields,
|
|
|
|
}
|
|
|
|
|
2020-06-25 09:42:59 -05:00
|
|
|
pub struct IdRange<T> {
|
2020-06-25 06:50:27 -05:00
|
|
|
range: Range<u32>,
|
2020-06-25 09:42:59 -05:00
|
|
|
_p: PhantomData<T>,
|
2020-06-25 06:50:27 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:42:59 -05:00
|
|
|
impl<T> IdRange<T> {
|
|
|
|
fn new(range: Range<Idx<T>>) -> Self {
|
|
|
|
Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData }
|
2020-06-25 06:50:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 09:42:59 -05:00
|
|
|
impl<T> Iterator for IdRange<T> {
|
|
|
|
type Item = Idx<T>;
|
2020-06-25 06:50:27 -05:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
self.range.next().map(|raw| Idx::from_raw(raw.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 09:47:21 -05:00
|
|
|
impl<T> fmt::Debug for IdRange<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_tuple(&format!("IdRange::<{}>", type_name::<T>())).field(&self.range).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Clone for IdRange<T> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self { range: self.range.clone(), _p: PhantomData }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> PartialEq for IdRange<T> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.range == other.range
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Eq for IdRange<T> {}
|
|
|
|
|
2020-03-25 09:33:01 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Fields {
|
2020-06-25 09:42:59 -05:00
|
|
|
Record(IdRange<Field>),
|
|
|
|
Tuple(IdRange<Field>),
|
2020-03-25 09:33:01 -05:00
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single field of an enum variant or struct
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Field {
|
|
|
|
pub name: Name,
|
2021-02-04 12:19:51 -06:00
|
|
|
pub type_ref: Idx<TypeRef>,
|
2020-06-24 08:36:18 -05:00
|
|
|
pub visibility: RawVisibilityId,
|
2020-03-25 09:33:01 -05:00
|
|
|
}
|