diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 1fc74cb3b0c..f227c7a7dd8 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -62,19 +62,88 @@ enum HirFileIdRepr { FileId(FileId), MacroFile(MacroFile), } - impl From for HirFileId { fn from(id: FileId) -> Self { HirFileId(HirFileIdRepr::FileId(id)) } } - impl From for HirFileId { fn from(id: MacroFile) -> Self { HirFileId(HirFileIdRepr::MacroFile(id)) } } +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct MacroFile { + pub macro_call_id: MacroCallId, +} + +/// `MacroCallId` identifies a particular macro invocation, like +/// `println!("Hello, {}", world)`. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct MacroCallId(salsa::InternId); +impl_intern_key!(MacroCallId); + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct MacroCallLoc { + pub def: MacroDefId, + pub(crate) krate: CrateId, + eager: Option, + pub kind: MacroCallKind, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct MacroDefId { + pub krate: CrateId, + pub kind: MacroDefKind, + pub local_inner: bool, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum MacroDefKind { + Declarative(AstId), + BuiltIn(BuiltinFnLikeExpander, AstId), + // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander + BuiltInAttr(BuiltinAttrExpander, AstId), + BuiltInDerive(BuiltinDeriveExpander, AstId), + BuiltInEager(EagerExpander, AstId), + ProcMacro(ProcMacroExpander, ProcMacroKind, AstId), +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +struct EagerCallInfo { + /// NOTE: This can be *either* the expansion result, *or* the argument to the eager macro! + arg_or_expansion: Arc, + included_file: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub enum MacroCallKind { + FnLike { + ast_id: AstId, + expand_to: ExpandTo, + }, + Derive { + ast_id: AstId, + derive_name: String, + /// Syntactical index of the invoking `#[derive]` attribute. + /// + /// Outer attributes are counted first, then inner attributes. This does not support + /// out-of-line modules, which may have attributes spread across 2 files! + derive_attr_index: u32, + }, + Attr { + ast_id: AstId, + attr_name: String, + attr_args: (tt::Subtree, mbe::TokenMap), + /// Syntactical index of the invoking `#[attribute]`. + /// + /// Outer attributes are counted first, then inner attributes. This does not support + /// out-of-line modules, which may have attributes spread across 2 files! + invoc_attr_index: u32, + }, +} + impl HirFileId { /// For macro-expansion files, returns the file original source file the /// expansion originated from. @@ -215,25 +284,6 @@ impl HirFileId { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct MacroFile { - pub macro_call_id: MacroCallId, -} - -/// `MacroCallId` identifies a particular macro invocation, like -/// `println!("Hello, {}", world)`. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct MacroCallId(salsa::InternId); -impl_intern_key!(MacroCallId); - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct MacroDefId { - pub krate: CrateId, - pub kind: MacroDefKind, - - pub local_inner: bool, -} - impl MacroDefId { pub fn as_lazy_macro( self, @@ -261,59 +311,6 @@ impl MacroDefId { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum MacroDefKind { - Declarative(AstId), - BuiltIn(BuiltinFnLikeExpander, AstId), - // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander - BuiltInAttr(BuiltinAttrExpander, AstId), - BuiltInDerive(BuiltinDeriveExpander, AstId), - BuiltInEager(EagerExpander, AstId), - ProcMacro(ProcMacroExpander, ProcMacroKind, AstId), -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -struct EagerCallInfo { - /// NOTE: This can be *either* the expansion result, *or* the argument to the eager macro! - arg_or_expansion: Arc, - included_file: Option, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct MacroCallLoc { - pub def: MacroDefId, - pub(crate) krate: CrateId, - eager: Option, - pub kind: MacroCallKind, -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum MacroCallKind { - FnLike { - ast_id: AstId, - expand_to: ExpandTo, - }, - Derive { - ast_id: AstId, - derive_name: String, - /// Syntactical index of the invoking `#[derive]` attribute. - /// - /// Outer attributes are counted first, then inner attributes. This does not support - /// out-of-line modules, which may have attributes spread across 2 files! - derive_attr_index: u32, - }, - Attr { - ast_id: AstId, - attr_name: String, - attr_args: (tt::Subtree, mbe::TokenMap), - /// Syntactical index of the invoking `#[attribute]`. - /// - /// Outer attributes are counted first, then inner attributes. This does not support - /// out-of-line modules, which may have attributes spread across 2 files! - invoc_attr_index: u32, - }, -} - // FIXME: attribute indices do not account for `cfg_attr`, which means that we'll strip the whole // `cfg_attr` instead of just one of the attributes it expands to