macro_rules! well_known_namespaces {
( $( $ns:literal in $module:ident => $variant:ident($sub:ident) ),+ $(,)? ) => {
#[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
}
pub fn module(&self) -> WellKnownModule {
match self {
$( Self::$variant(_) => WellKnownModule::$module, )+
}
}
}
$(
impl $sub {
pub fn from_type_name(name: &str) -> Option<Self> {
name.parse().ok()
}
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))
}
}
)+
};
}
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]
};
}
macro_rules! well_known_modules {
( $( $variant:ident => $name:literal : $token:ident ),+ $(,)? ) => {
#[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 {
pub fn assembly_name(&self) -> &'static str {
match self {
$( Self::$variant => $name, )+
}
}
pub fn public_key_token(&self) -> [u8; 8] {
match self {
$( Self::$variant => wk_module_token!($token), )+
}
}
}
};
}
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)?
};
($m:ident, (valuetype { $wk:expr })) => {
value_type($m, $wk)?
};
($m:ident, [$($inner:tt)+]) => {
TypeSig::sz_array(wk_type!($m, $($inner)+))
};
}
macro_rules! wk_is_instance {
(Static) => {
false
};
(Instance) => {
true
};
}
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)
};
}
macro_rules! well_known_methods {
( $(
$group:ident($sub:ident) = $declaring:expr => {
$( $variant:ident = $kind:ident $name:literal ( $($param:tt),* $(,)? ) -> $ret:tt ; )+
}
)+ ) => {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet), repr(C))]
pub enum WellKnownMethod {
$( $group($sub), )+
}
impl WellKnownMethod {
pub fn declaring(self) -> WellKnown {
match self { $( Self::$group(_) => $declaring, )+ }
}
pub fn method_name(self) -> &'static str {
match self { $( Self::$group(m) => m.method_name(), )+ }
}
pub fn is_instance(self) -> bool {
match self { $( Self::$group(m) => m.is_instance(), )+ }
}
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), )+ }
}
}
$(
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) ),* ]
),
)+
})
}
}
)+
};
}