hao 0.0.19

Read, edit, create and write .net assemblies
Documentation
//! A small vocabulary of common BCL methods that carry their signature, so a
//! caller imports e.g. `Console.WriteLine(string)` in one line without writing
//! the [`MethodSig`](crate::dotnet::signature::MethodSig) by hand.
//!
//! Mirrors [`WellKnown`](super::WellKnown): a top-level
//! [`WellKnownMethod`] keyed by declaring type, each wrapping a per-type method
//! sub-enum, all static metadata. Import a member into a module with
//! [`Module::import_well_known_method`](crate::Module::import_well_known_method).
//!
//! The whole vocabulary is declared by the [`well_known_methods!`] table below
//! (see the macro for the full syntax): a group per declaring type,
//! `Group(SubEnum) = <WellKnown> => { .. }`, and in it
//! a line per method, `Variant = Static|Instance "<.NET name>" (params) -> ret`,
//! where a type is a primitive (`void`, `bool`, `char`, `i32`, `i64`, `f64`,
//! `nint`), `string`, `object`, a class type `{ <WellKnown> }`, a value type
//! `(valuetype { <WellKnown> })` or an array `[..]`.

use super::{
    SystemCollectionsType, SystemReflectionType, SystemRuntimeCompilerServicesType,
    SystemRuntimeInteropServicesType, SystemTextType, SystemType, WellKnown,
};
/// A type in a well-known method's signature, as static metadata. Importing
/// the method ([`Module::import_well_known_method`](crate::Module::import_well_known_method))
/// turns class and value types into references in the module.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum WellKnownSigType {
    /// `void`, as a return type.
    Void,
    /// `bool`.
    Bool,
    /// `char`.
    Char,
    /// `int`.
    I32,
    /// `long`.
    I64,
    /// `double`.
    F64,
    /// `nint` (`IntPtr`).
    NInt,
    /// `string`.
    String,
    /// `object`.
    Object,
    /// A reference type.
    Class(WellKnown),
    /// A value type (struct or enum).
    ValueType(WellKnown),
    /// A single-dimensional, zero-based array.
    Array(&'static WellKnownSigType),
}

// The vocabulary. Which assembly a declaring type lives in comes from the
// type (`WellKnown::module`), not from here.
well_known_methods! {
    Console(ConsoleMethod) = WellKnown::System(SystemType::Console) => {
        WriteLine     = Static "WriteLine" ()        -> void;
        WriteLineStr  = Static "WriteLine" (string)  -> void;
        WriteLineObj  = Static "WriteLine" (object)  -> void;
        WriteLineI32  = Static "WriteLine" (i32)     -> void;
        WriteLineBool = Static "WriteLine" (bool)    -> void;
        WriteLineChar = Static "WriteLine" (char)    -> void;
        WriteStr      = Static "Write"     (string)  -> void;
        ReadLine      = Static "ReadLine"  ()        -> string;
    }
    Object(ObjectMethod) = WellKnown::System(SystemType::Object) => {
        ToString        = Instance "ToString"        ()               -> string;
        GetHashCode     = Instance "GetHashCode"     ()               -> i32;
        GetType         = Instance "GetType"         ()               -> { WellKnown::System(SystemType::Type) };
        EqualsObj       = Instance "Equals"          (object)         -> bool;
        ReferenceEquals = Static   "ReferenceEquals" (object, object) -> bool;
    }
    Type(TypeMethod) = WellKnown::System(SystemType::Type) => {
        GetFullName  = Instance "get_FullName" ()       -> string;
        GetName      = Instance "get_Name"     ()       -> string;
        GetMethodStr = Instance "GetMethod"    (string) -> { WellKnown::SystemReflection(SystemReflectionType::MethodInfo) };
    }
    String(StringMethod) = WellKnown::System(SystemType::String) => {
        Concat2       = Static   "Concat"        (string, string) -> string;
        FormatObj     = Static   "Format"        (string, object) -> string;
        IsNullOrEmpty = Static   "IsNullOrEmpty" (string)         -> bool;
        EqualsStr     = Instance "Equals"        (string)         -> bool;
        GetLength     = Instance "get_Length"    ()               -> i32;
        SubstringI32  = Instance "Substring"     (i32)            -> string;
        ToUpper       = Instance "ToUpper"       ()               -> string;
        ToLower       = Instance "ToLower"       ()               -> string;
        ContainsStr   = Instance "Contains"      (string)         -> bool;
        ReplaceStr    = Instance "Replace"       (string, string) -> string;
    }
    StringBuilder(StringBuilderMethod) = WellKnown::SystemText(SystemTextType::StringBuilder) => {
        New           = Instance ".ctor"      ()       -> void;
        NewStr        = Instance ".ctor"      (string) -> void;
        AppendStr     = Instance "Append"     (string) -> { WellKnown::SystemText(SystemTextType::StringBuilder) };
        AppendChar    = Instance "Append"     (char)   -> { WellKnown::SystemText(SystemTextType::StringBuilder) };
        AppendI32     = Instance "Append"     (i32)    -> { WellKnown::SystemText(SystemTextType::StringBuilder) };
        AppendObj     = Instance "Append"     (object) -> { WellKnown::SystemText(SystemTextType::StringBuilder) };
        AppendLineStr = Instance "AppendLine" (string) -> { WellKnown::SystemText(SystemTextType::StringBuilder) };
        Clear         = Instance "Clear"      ()       -> { WellKnown::SystemText(SystemTextType::StringBuilder) };
        ToString      = Instance "ToString"   ()       -> string;
    }
    Math(MathMethod) = WellKnown::System(SystemType::Math) => {
        MaxI32 = Static "Max" (i32, i32) -> i32;
        MinI32 = Static "Min" (i32, i32) -> i32;
        AbsI32 = Static "Abs" (i32)      -> i32;
    }
    Convert(ConvertMethod) = WellKnown::System(SystemType::Convert) => {
        ToStringObj  = Static "ToString"  (object) -> string;
        ToInt32Str   = Static "ToInt32"   (string) -> i32;
        ToBooleanStr = Static "ToBoolean" (string) -> bool;
    }
    Environment(EnvironmentMethod) = WellKnown::System(SystemType::Environment) => {
        GetNewLine                = Static "get_NewLine"            ()       -> string;
        GetMachineName            = Static "get_MachineName"        ()       -> string;
        GetProcessorCount         = Static "get_ProcessorCount"     ()       -> i32;
        GetEnvironmentVariableStr = Static "GetEnvironmentVariable" (string) -> string;
        Exit                      = Static "Exit"                   (i32)    -> void;
    }
    Gc(GcMethod) = WellKnown::System(SystemType::GC) => {
        Collect              = Static "Collect"              ()    -> void;
        CollectGen           = Static "Collect"              (i32) -> void;
        SuppressFinalize     = Static "SuppressFinalize"     (object) -> void;
        TryStartNoGCRegion   = Static "TryStartNoGCRegion"  (i64) -> bool;
        EndNoGCRegion        = Static "EndNoGCRegion"        ()    -> void;
    }
    Marshal(MarshalMethod) = WellKnown::SystemRuntimeInteropServices(SystemRuntimeInteropServicesType::Marshal) => {
        PtrToStringAuto      = Static "PtrToStringAuto"      (nint)   -> string;
        StringToCoTaskMemUni = Static "StringToCoTaskMemUni" (string) -> nint;
        FreeCoTaskMem        = Static "FreeCoTaskMem"        (nint)   -> void;
        UnsafeAddrOfPinnedArrayElement = Static "UnsafeAddrOfPinnedArrayElement" ({ WellKnown::System(SystemType::Array) }, i32) -> nint;
    }
    Array(ArrayMethod) = WellKnown::System(SystemType::Array) => {
        GetLength = Instance "get_Length" () -> i32;
    }
    Exception(ExceptionMethod) = WellKnown::System(SystemType::Exception) => {
        GetHResult = Instance "get_HResult" () -> i32;
    }
    Assembly(AssemblyMethod) = WellKnown::SystemReflection(SystemReflectionType::Assembly) => {
        LoadFrom   = Static   "LoadFrom" (string) -> { WellKnown::SystemReflection(SystemReflectionType::Assembly) };
        GetName    = Instance "GetName"  ()       -> { WellKnown::SystemReflection(SystemReflectionType::AssemblyName) };
        GetTypeStr = Instance "GetType"  (string) -> { WellKnown::System(SystemType::Type) };
        GetTypes   = Instance "GetTypes" ()       -> [{ WellKnown::System(SystemType::Type) }];
    }
    TypeExt(TypeExtMethod) = WellKnown::System(SystemType::Type) => {
        GetTypeStatic = Static   "GetType"    (string) -> { WellKnown::System(SystemType::Type) };
        GetMethodBF   = Instance "GetMethod"  (string, (valuetype { WellKnown::SystemReflection(SystemReflectionType::BindingFlags) })) -> { WellKnown::SystemReflection(SystemReflectionType::MethodInfo) };
        GetMethodsBF  = Instance "GetMethods" ((valuetype { WellKnown::SystemReflection(SystemReflectionType::BindingFlags) })) -> [{ WellKnown::SystemReflection(SystemReflectionType::MethodInfo) }];
    }
    MethodBase(MethodBaseMethod) = WellKnown::SystemReflection(SystemReflectionType::MethodBase) => {
        GetMethodHandle = Instance "get_MethodHandle" () -> (valuetype { WellKnown::System(SystemType::RuntimeMethodHandle) });
        GetParameters   = Instance "GetParameters"    () -> [{ WellKnown::SystemReflection(SystemReflectionType::ParameterInfo) }];
        GetIsGenericMethodDefinition = Instance "get_IsGenericMethodDefinition" () -> bool;
    }
    ParameterInfo(ParameterInfoMethod) = WellKnown::SystemReflection(SystemReflectionType::ParameterInfo) => {
        GetParameterType = Instance "get_ParameterType" () -> { WellKnown::System(SystemType::Type) };
    }
    RuntimeMethodHandle(RuntimeMethodHandleMethod) = WellKnown::System(SystemType::RuntimeMethodHandle) => {
        GetFunctionPointer = Instance "GetFunctionPointer" () -> nint;
    }
    RuntimeHelpers(RuntimeHelpersMethod) = WellKnown::SystemRuntimeCompilerServices(SystemRuntimeCompilerServicesType::RuntimeHelpers) => {
        PrepareMethod = Static "PrepareMethod" ((valuetype { WellKnown::System(SystemType::RuntimeMethodHandle) })) -> void;
    }
    AppDomain(AppDomainMethod) = WellKnown::System(SystemType::AppDomain) => {
        GetCurrentDomain = Static   "get_CurrentDomain" () -> { WellKnown::System(SystemType::AppDomain) };
        GetAssemblies    = Instance "GetAssemblies"     () -> [{ WellKnown::SystemReflection(SystemReflectionType::Assembly) }];
    }
    AssemblyName(AssemblyNameMethod) = WellKnown::SystemReflection(SystemReflectionType::AssemblyName) => {
        GetName = Instance "get_Name" () -> string;
    }
    MemberInfo(MemberInfoMethod) = WellKnown::SystemReflection(SystemReflectionType::MemberInfo) => {
        GetName = Instance "get_Name" () -> string;
    }
    StringExt(StringExtMethod) = WellKnown::System(SystemType::String) => {
        OpEquality = Static "op_Equality" (string, string) -> bool;
    }
    ArrayList(ArrayListMethod) = WellKnown::SystemCollections(SystemCollectionsType::ArrayList) => {
        New     = Instance ".ctor"   ()       -> void;
        Add     = Instance "Add"     (object) -> i32;
        ToArray = Instance "ToArray" ()       -> [object];
    }
    IEnumerable(IEnumerableMethod) = WellKnown::SystemCollections(SystemCollectionsType::IEnumerable) => {
        GetEnumerator = Instance "GetEnumerator" () -> { WellKnown::SystemCollections(SystemCollectionsType::IEnumerator) };
    }
    IEnumerator(IEnumeratorMethod) = WellKnown::SystemCollections(SystemCollectionsType::IEnumerator) => {
        MoveNext   = Instance "MoveNext"    () -> bool;
        GetCurrent = Instance "get_Current" () -> object;
    }
}

#[cfg(test)]
mod tests {
    use super::super::{SystemCollectionsGenericType, WellKnownModule};
    use super::*;
    use crate::dotnet::il::MemberToken;
    use crate::dotnet::module::Module;
    use crate::dotnet::module::{ModuleKind, TargetRuntime};

    /// The assembly a method's declaring type is referenced from, once it's
    /// imported into a new module.
    fn declaring_assembly(runtime: TargetRuntime, method: WellKnownMethod) -> String {
        let mut module = Module::new("Test", ModuleKind::Dll, runtime);
        module.import_well_known_method(method).unwrap();
        let (namespace, name) = method.declaring().full_name();
        let ty = module
            .type_refs
            .iter()
            .find(|t| t.get().namespace == namespace && t.get().name == name)
            .expect("the declaring type was referenced")
            .clone();
        let scope = ty.get().scope.clone();
        match scope {
            Some(crate::dotnet::entries::ResolutionScope::AssemblyRef(a)) => a.get().name.clone(),
            _ => panic!("an assembly scope"),
        }
    }

    #[test]
    fn generic_types_are_referenced_with_their_arity() {
        let mut module = Module::new("Test", ModuleKind::Dll, TargetRuntime::NetCore(9));
        module
            .corlib_type(SystemCollectionsGenericType::List_1)
            .unwrap();
        assert!(
            module
                .type_refs
                .iter()
                .any(|t| t.get().namespace == "System.Collections.Generic"
                    && t.get().name == "List`1"),
            "List<T> is referenced as List`1"
        );
    }

    #[test]
    fn types_outside_corlib_come_from_their_assembly() {
        let write_line = WellKnownMethod::Console(ConsoleMethod::WriteLineStr);
        let marshal = WellKnownMethod::Marshal(MarshalMethod::FreeCoTaskMem);
        let to_string = WellKnownMethod::Object(ObjectMethod::ToString);
        // .NET reference assemblies: Console isn't in System.Runtime.
        assert_eq!(
            declaring_assembly(TargetRuntime::NetCore(9), write_line),
            "System.Console"
        );
        assert_eq!(
            declaring_assembly(TargetRuntime::NetCore(9), marshal),
            "System.Runtime.InteropServices"
        );
        assert_eq!(
            declaring_assembly(TargetRuntime::NetCore(9), to_string),
            "System.Runtime"
        );
        // System.Private.CoreLib doesn't have Console either.
        assert_eq!(
            declaring_assembly(TargetRuntime::NetCoreImplementation(9), write_line),
            "System.Console"
        );
        // The System.Collections types are in System.Runtime, not their
        // namespace's System.Collections.
        let array_list = WellKnownMethod::ArrayList(ArrayListMethod::New);
        assert_eq!(
            array_list.declaring().module(),
            WellKnownModule::SystemRuntime
        );
        assert_eq!(
            declaring_assembly(TargetRuntime::NetCore(9), array_list),
            "System.Runtime"
        );
        // .NET Framework: everything is in mscorlib.
        assert_eq!(
            declaring_assembly(TargetRuntime::NetFramework, write_line),
            "mscorlib"
        );
    }

    #[test]
    fn resolves_common_methods() {
        let mut module = Module::new(
            "Test",
            ModuleKind::Dll,
            TargetRuntime::NetCoreImplementation(8),
        );
        for m in [
            WellKnownMethod::Console(ConsoleMethod::WriteLineStr),
            WellKnownMethod::Console(ConsoleMethod::ReadLine),
            WellKnownMethod::Object(ObjectMethod::ToString),
            // Class-typed return (System.Type), built with the module.
            WellKnownMethod::Object(ObjectMethod::GetType),
            // Class-typed return (System.Reflection.MethodInfo).
            WellKnownMethod::Type(TypeMethod::GetMethodStr),
            WellKnownMethod::Type(TypeMethod::GetFullName),
            WellKnownMethod::String(StringMethod::FormatObj),
            WellKnownMethod::String(StringMethod::ReplaceStr),
            // Constructor + class-typed parameter/return (StringBuilder).
            WellKnownMethod::StringBuilder(StringBuilderMethod::NewStr),
            WellKnownMethod::StringBuilder(StringBuilderMethod::AppendLineStr),
            WellKnownMethod::Math(MathMethod::AbsI32),
            WellKnownMethod::Convert(ConvertMethod::ToInt32Str),
            WellKnownMethod::Environment(EnvironmentMethod::GetMachineName),
            WellKnownMethod::Gc(GcMethod::SuppressFinalize),
            // nint parameter.
            WellKnownMethod::Marshal(MarshalMethod::PtrToStringAuto),
            // Value-type parameter (RuntimeMethodHandle).
            WellKnownMethod::RuntimeHelpers(RuntimeHelpersMethod::PrepareMethod),
            // Value-type return (RuntimeMethodHandle).
            WellKnownMethod::MethodBase(MethodBaseMethod::GetMethodHandle),
            // nint return.
            WellKnownMethod::RuntimeMethodHandle(RuntimeMethodHandleMethod::GetFunctionPointer),
            // Value-type parameter (BindingFlags), string.
            WellKnownMethod::TypeExt(TypeExtMethod::GetMethodBF),
            // Array return.
            WellKnownMethod::AppDomain(AppDomainMethod::GetAssemblies),
            WellKnownMethod::TypeExt(TypeExtMethod::GetMethodsBF),
            WellKnownMethod::MethodBase(MethodBaseMethod::GetParameters),
            // Various.
            WellKnownMethod::Exception(ExceptionMethod::GetHResult),
            WellKnownMethod::Assembly(AssemblyMethod::LoadFrom),
            WellKnownMethod::Assembly(AssemblyMethod::GetName),
            WellKnownMethod::Assembly(AssemblyMethod::GetTypeStr),
            WellKnownMethod::Assembly(AssemblyMethod::GetTypes),
            WellKnownMethod::Marshal(MarshalMethod::StringToCoTaskMemUni),
            WellKnownMethod::Marshal(MarshalMethod::FreeCoTaskMem),
            WellKnownMethod::AssemblyName(AssemblyNameMethod::GetName),
            WellKnownMethod::MemberInfo(MemberInfoMethod::GetName),
            WellKnownMethod::AppDomain(AppDomainMethod::GetCurrentDomain),
            WellKnownMethod::StringExt(StringExtMethod::OpEquality),
            // Constructor, object param, array-of-object return.
            WellKnownMethod::ArrayList(ArrayListMethod::New),
            WellKnownMethod::ArrayList(ArrayListMethod::Add),
            WellKnownMethod::ArrayList(ArrayListMethod::ToArray),
            // Interface methods with a class-typed return.
            WellKnownMethod::IEnumerable(IEnumerableMethod::GetEnumerator),
            WellKnownMethod::IEnumerator(IEnumeratorMethod::MoveNext),
            WellKnownMethod::IEnumerator(IEnumeratorMethod::GetCurrent),
        ] {
            let token = module
                .import_well_known_method(m)
                .expect("well-known method resolves");
            assert!(matches!(
                token,
                MemberToken::MemberRef(_) | MemberToken::Method(_)
            ));
        }
    }

    #[test]
    fn accessors() {
        let write = WellKnownMethod::Console(ConsoleMethod::WriteLineStr);
        assert_eq!(write.method_name(), "WriteLine");
        assert!(!write.is_instance());
        assert!(WellKnownMethod::Object(ObjectMethod::ToString).is_instance());
        assert!(!WellKnownMethod::Object(ObjectMethod::ReferenceEquals).is_instance());
        // Constructors are instance methods named `.ctor`.
        let ctor = WellKnownMethod::StringBuilder(StringBuilderMethod::NewStr);
        assert_eq!(ctor.method_name(), ".ctor");
        assert!(ctor.is_instance());
        assert_eq!(
            WellKnownMethod::Environment(EnvironmentMethod::GetNewLine).method_name(),
            "get_NewLine"
        );
    }
}