hao 0.0.18

Read, edit, create and write .net assemblies
Documentation
//! The table macros for this module:
//! - `well_known_namespaces!` generates the `WellKnown` enum and, per sub-enum,
//!   the `from_full_name`/`full_name` arms and the `from_type_name`/`name`/
//!   `TypeName` impls.
//! - `well_known_methods!` (with its `wk_type!`/`wk_is_instance!`/`wk_new_sig!`
//!   helpers) generates `WellKnownMethod` and the per-type method sub-enums.

macro_rules! well_known_namespaces {
    ( $( $ns:literal in $module:ident => $variant:ident($sub:ident) ),+ $(,)? ) => {
        /// A well-known .NET type, grouped by namespace. `from_full_name`,
        /// `full_name` and `name` strip the generic arity suffix (e.g.
        /// `Nullable\`1` -> `Nullable`).
        #[derive(Debug, Copy, Clone, PartialEq)]
        #[cfg_attr(feature = "reflect", derive(facet::Facet), repr(C))]
        pub enum WellKnown {
            $( $variant($sub), )+
        }

        impl WellKnown {
            pub fn from_full_name(namespace: &str, name: &str) -> Option<Self> {
                Some(match namespace {
                    $( $ns => Self::$variant($sub::from_type_name(name)?), )+
                    _ => return None,
                })
            }

            pub fn full_name(&self) -> (&'static str, &'static str) {
                match self {
                    $( Self::$variant(ty) => ($ns, ty.name()), )+
                }
            }

            pub fn namespace(&self) -> &'static str {
                self.full_name().0
            }

            pub fn type_name(&self) -> &'static str {
                self.full_name().1
            }

            /// The well-known assembly this type's namespace lives in.
            ///
            /// Mapped at the namespace level: a namespace whose types span
            /// several assemblies (e.g. `System`, where `Console` lives in
            /// `System.Console`) reports its primary/corlib assembly. Use an
            /// explicit [`WellKnownModule`] where an outlier needs a different ref.
            pub fn module(&self) -> WellKnownModule {
                match self {
                    $( Self::$variant(_) => WellKnownModule::$module, )+
                }
            }
        }

        $(
            impl $sub {
                /// Parses a type name (with or without generic arity) into this
                /// well-known type.
                pub fn from_type_name(name: &str) -> Option<Self> {
                    name.parse().ok()
                }

                /// The type name without the generic arity suffix.
                pub fn name(&self) -> &'static str {
                    strip_arity(self.into())
                }
            }

            impl TypeName for $sub {
                fn type_name(&self) -> (Cow<'_, str>, Cow<'_, str>) {
                    let (namespace, name) = WellKnown::$variant(*self).full_name();
                    (Cow::Borrowed(namespace), Cow::Borrowed(name))
                }
            }
        )+
    };
}

/// Expands a well-known assembly's public-key token keyword to its 8 bytes.
/// `Ecma` is the ECMA/framework token almost every `System.*` / `netstandard`
/// assembly is signed with; `Microsoft` is .NET Framework's `mscorlib`; and
/// `CoreLib` is CoreCLR's `System.Private.CoreLib`.
macro_rules! wk_module_token {
    (Ecma) => {
        [0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a]
    };
    (Microsoft) => {
        [0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89]
    };
    (CoreLib) => {
        [0x7c, 0xec, 0x85, 0xd7, 0xbe, 0xa7, 0x79, 0x8e]
    };
}

/// Generates [`WellKnownModule`] (one variant per well-known assembly) and its
/// `assembly_name`/`public_key_token` impls from a declarative table. Each row
/// is `Variant => "Assembly.Name" : <TokenKeyword>` (see [`wk_module_token!`]).
macro_rules! well_known_modules {
    ( $( $variant:ident => $name:literal : $token:ident ),+ $(,)? ) => {
        /// A well-known .NET assembly a module references. Carries the stable
        /// assembly name and public-key token; the version is version-dependent
        /// and supplied separately (see [`AssemblyName`] impls, e.g.
        /// `(WellKnownModule, Version)`).
        #[allow(non_camel_case_types)]
        #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, EnumString, IntoStaticStr)]
        #[cfg_attr(feature = "reflect", derive(facet::Facet), repr(u8))]
        pub enum WellKnownModule {
            $( $variant, )+
        }

        impl WellKnownModule {
            /// The assembly's simple name, e.g. `"System.Runtime"`.
            pub fn assembly_name(&self) -> &'static str {
                match self {
                    $( Self::$variant => $name, )+
                }
            }

            /// The assembly's 8-byte public-key token (stable across .NET versions).
            pub fn public_key_token(&self) -> [u8; 8] {
                match self {
                    $( Self::$variant => wk_module_token!($token), )+
                }
            }
        }
    };
}

/// Expands a type token to a `TypeSig`. A class type (`{ <WellKnown> }`) resolves
/// against the module and may early-return via `?`, so it is only valid inside a
/// function returning `Result`.
macro_rules! wk_type {
    ($m:ident, void) => {
        TypeSig::VOID
    };
    ($m:ident, bool) => {
        TypeSig::BOOL
    };
    ($m:ident, char) => {
        TypeSig::CHAR
    };
    ($m:ident, i32) => {
        TypeSig::INT32
    };
    ($m:ident, i64) => {
        TypeSig::INT64
    };
    ($m:ident, f64) => {
        TypeSig::FLOAT64
    };
    ($m:ident, nint) => {
        TypeSig::NATIVE_INT
    };
    ($m:ident, string) => {
        TypeSig::STRING
    };
    ($m:ident, object) => {
        TypeSig::OBJECT
    };
    ($m:ident, { $wk:expr }) => {
        class($m, $wk)?
    };
    // valuetype wrapped in parens: (valuetype { WellKnown::... })
    ($m:ident, (valuetype { $wk:expr })) => {
        value_type($m, $wk)?
    };
    // array wrapped in brackets: [{ WellKnown::... }] or [i32] etc.
    ($m:ident, [$($inner:tt)+]) => {
        TypeSig::sz_array(wk_type!($m, $($inner)+))
    };
}

macro_rules! wk_is_instance {
    (Static) => {
        false
    };
    (Instance) => {
        true
    };
}

/// Maps any parameter token to `()` so parameters can be counted with
/// `<[()]>::len`.
macro_rules! wk_unit {
    ($p:tt) => {
        ()
    };
}

macro_rules! wk_new_sig {
    (Static, $ret:expr, $params:expr) => {
        MethodSig::new_static($ret, $params)
    };
    (Instance, $ret:expr, $params:expr) => {
        MethodSig::new_instance($ret, $params)
    };
}

/// Generates [`WellKnownMethod`], every per-type method sub-enum, and their
/// `method_name`/`is_instance`/`signature` impls from a declarative table.
///
/// Each parameter and the return type is a single token tree consumed by
/// `wk_type!`. Primitives (`void`, `bool`, `i32`, `nint`, …), `string` and
/// `object` are bare keywords.  Class types are `{ WellKnown::… }`, value
/// types `(valuetype { WellKnown::… })`, and arrays `[inner]` where `inner`
/// is any of the above forms.
macro_rules! well_known_methods {
    ( $(
        $group:ident($sub:ident) = $declaring:expr => {
            $( $variant:ident = $kind:ident $name:literal ( $($param:tt),* $(,)? ) -> $ret:tt ; )+
        }
    )+ ) => {
        /// A common BCL method, keyed by declaring type. Each variant wraps a
        /// per-type method sub-enum, e.g.
        /// `WellKnownMethod::Console(ConsoleMethod::WriteLineStr)`.
        #[derive(Debug, Copy, Clone, PartialEq, Eq)]
        #[cfg_attr(feature = "reflect", derive(facet::Facet), repr(C))]
        pub enum WellKnownMethod {
            $( $group($sub), )+
        }

        impl WellKnownMethod {
            /// The declaring type, as a [`WellKnown`] (which is itself a `TypeName`).
            pub fn declaring(self) -> WellKnown {
                match self { $( Self::$group(_) => $declaring, )+ }
            }

            /// The .NET method name, e.g. `"WriteLine"`, `".ctor"`, or a property
            /// getter like `"get_NewLine"`.
            pub fn method_name(self) -> &'static str {
                match self { $( Self::$group(m) => m.method_name(), )+ }
            }

            /// Whether the method is an instance method (vs. static).
            /// Constructors are instance methods.
            pub fn is_instance(self) -> bool {
                match self { $( Self::$group(m) => m.is_instance(), )+ }
            }

            /// The number of declared parameters (`this` not counted).
            pub fn param_count(self) -> usize {
                match self { $( Self::$group(m) => m.param_count(), )+ }
            }

            fn signature(self, module: &mut Module) -> Result<MethodSig> {
                match self { $( Self::$group(m) => m.signature(module), )+ }
            }
        }

        // A bare method sub-enum coerces to its `WellKnownMethod`, so
        // `import_well_known_method(MathMethod::MaxI32)` works without spelling
        // out the group (mirroring how a `SystemType` is a `TypeName`).
        $(
            impl From<$sub> for WellKnownMethod {
                fn from(method: $sub) -> Self {
                    WellKnownMethod::$group(method)
                }
            }
        )+

        $(
            #[derive(Debug, Copy, Clone, PartialEq, Eq)]
            #[cfg_attr(feature = "reflect", derive(facet::Facet), repr(u8))]
            pub enum $sub {
                $( $variant, )+
            }

            impl $sub {
                fn method_name(self) -> &'static str {
                    match self { $( Self::$variant => $name, )+ }
                }
                fn is_instance(self) -> bool {
                    match self { $( Self::$variant => wk_is_instance!($kind), )+ }
                }
                fn param_count(self) -> usize {
                    match self {
                        $( Self::$variant => <[()]>::len(&[ $( wk_unit!($param) ),* ]), )+
                    }
                }
                #[allow(unused_variables)]
                fn signature(self, module: &mut Module) -> Result<MethodSig> {
                    Ok(match self {
                        $(
                            Self::$variant => wk_new_sig!(
                                $kind,
                                wk_type!(module, $ret),
                                vec![ $( wk_type!(module, $param) ),* ]
                            ),
                        )+
                    })
                }
            }
        )+
    };
}