use super::{
SystemCollectionsType, SystemReflectionType, SystemRuntimeCompilerServicesType,
SystemRuntimeInteropServicesType, SystemTextType, SystemType, WellKnown,
};
use crate::dotnet::entries::TypeDefOrRef;
use crate::dotnet::module::Module;
use crate::dotnet::signature::{MethodSig, TypeSig};
use crate::error::Result;
fn resolve_type(module: &mut Module, ty: WellKnown) -> Result<TypeDefOrRef> {
match module.import_type(ty) {
Ok(resolved) => Ok(resolved),
Err(_) => module.corlib_type(ty),
}
}
fn class(module: &mut Module, ty: WellKnown) -> Result<TypeSig> {
Ok(TypeSig::Class(resolve_type(module, ty)?))
}
fn value_type(module: &mut Module, ty: WellKnown) -> Result<TypeSig> {
Ok(TypeSig::value_type(resolve_type(module, ty)?))
}
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) }];
}
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;
}
}
impl WellKnownMethod {
pub fn resolve(
self,
module: &mut Module,
) -> Result<(TypeDefOrRef, &'static str, MethodSig)> {
let ty = resolve_type(module, self.declaring())?;
let signature = self.signature(module)?;
Ok((ty, self.method_name(), signature))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dotnet::il::MemberToken;
use crate::dotnet::module::{ModuleKind, TargetRuntime};
#[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),
WellKnownMethod::Object(ObjectMethod::GetType),
WellKnownMethod::Type(TypeMethod::GetMethodStr),
WellKnownMethod::Type(TypeMethod::GetFullName),
WellKnownMethod::String(StringMethod::FormatObj),
WellKnownMethod::String(StringMethod::ReplaceStr),
WellKnownMethod::StringBuilder(StringBuilderMethod::NewStr),
WellKnownMethod::StringBuilder(StringBuilderMethod::AppendLineStr),
WellKnownMethod::Math(MathMethod::AbsI32),
WellKnownMethod::Convert(ConvertMethod::ToInt32Str),
WellKnownMethod::Environment(EnvironmentMethod::GetMachineName),
WellKnownMethod::Gc(GcMethod::SuppressFinalize),
WellKnownMethod::Marshal(MarshalMethod::PtrToStringAuto),
WellKnownMethod::RuntimeHelpers(RuntimeHelpersMethod::PrepareMethod),
WellKnownMethod::MethodBase(MethodBaseMethod::GetMethodHandle),
WellKnownMethod::RuntimeMethodHandle(RuntimeMethodHandleMethod::GetFunctionPointer),
WellKnownMethod::TypeExt(TypeExtMethod::GetMethodBF),
WellKnownMethod::AppDomain(AppDomainMethod::GetAssemblies),
WellKnownMethod::TypeExt(TypeExtMethod::GetMethodsBF),
WellKnownMethod::MethodBase(MethodBaseMethod::GetParameters),
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),
WellKnownMethod::ArrayList(ArrayListMethod::New),
WellKnownMethod::ArrayList(ArrayListMethod::Add),
WellKnownMethod::ArrayList(ArrayListMethod::ToArray),
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());
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"
);
}
}