use super::{
well_known::{SystemType, TypeName, WellKnown},
AssemblyRef, CustomAttribute, Entry, EntryName, Method, ModuleDef, TypeDef,
};
use crate::{
dotnet::signature::{MemberRefSig, TypeSig},
sync::Shared,
Module,
};
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum TypeDefOrRef {
TypeDef(Entry<TypeDef>),
TypeRef(Entry<TypeRef>),
TypeSpec(Entry<TypeSpec>),
}
impl TypeDefOrRef {
pub fn full_name(&self) -> String {
match self {
Self::TypeDef(t) => t.get().full_name(),
Self::TypeRef(t) => t.get().full_name(),
Self::TypeSpec(t) => format!("{:?}", t.get().signature),
}
}
pub fn name(&self) -> String {
match self {
Self::TypeDef(t) => t.get().name.clone(),
Self::TypeRef(t) => t.get().name.clone(),
Self::TypeSpec(_) => self.full_name(),
}
}
pub fn namespace(&self) -> String {
match self {
Self::TypeDef(t) => t.get().namespace.clone(),
Self::TypeRef(t) => t.get().namespace.clone(),
Self::TypeSpec(_) => String::new(),
}
}
pub fn full_name_is(&self, ty: impl TypeName) -> bool {
match self {
Self::TypeDef(t) => t.get().full_name_is(ty),
Self::TypeRef(t) => t.get().full_name_is(ty),
Self::TypeSpec(_) => false,
}
}
pub fn is_system_type(&self, system_type: SystemType) -> bool {
self.full_name_is(WellKnown::System(system_type))
}
pub fn resolve(&self) -> Option<Entry<TypeDef>> {
match self {
Self::TypeDef(t) => Some(t.clone()),
Self::TypeRef(t) => t.get().resolve(),
Self::TypeSpec(_) => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum ResolutionScope {
Module(Entry<ModuleDef>),
ModuleRef(Entry<ModuleRef>),
AssemblyRef(Entry<AssemblyRef>),
TypeRef(Entry<TypeRef>),
}
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct TypeRef {
pub scope: Option<ResolutionScope>,
pub name: String,
pub namespace: String,
pub custom_attributes: Vec<CustomAttribute>,
}
impl TypeRef {
pub fn new(
scope: ResolutionScope,
namespace: impl Into<String>,
name: impl Into<String>,
) -> Self {
Self {
scope: Some(scope),
name: name.into(),
namespace: namespace.into(),
custom_attributes: Vec::new(),
}
}
pub fn full_name(&self) -> String {
match &self.scope {
Some(ResolutionScope::TypeRef(parent)) => {
format!("{}/{}", parent.get().full_name(), self.name)
}
_ if self.namespace.is_empty() => self.name.clone(),
_ => format!("{}.{}", self.namespace, self.name),
}
}
pub fn full_name_is(&self, ty: impl TypeName) -> bool {
let (namespace, name) = ty.type_name();
self.namespace.as_str() == &*namespace && self.name.as_str() == &*name
}
pub fn is_corlib(&self) -> bool {
matches!(&self.scope, Some(ResolutionScope::AssemblyRef(r)) if r.get().is_corlib())
}
pub fn well_known(&self) -> Option<WellKnown> {
if self.is_corlib() {
WellKnown::from_full_name(&self.namespace, &self.name)
} else {
None
}
}
pub fn resolve(&self) -> Option<Entry<TypeDef>> {
let module: Shared<Module> = match &self.scope {
Some(ResolutionScope::AssemblyRef(asm)) => asm.get().resolved.clone()?,
Some(ResolutionScope::TypeRef(parent)) => {
let parent = parent.get().resolve()?;
let parent = parent.get();
return parent
.nested_types
.iter()
.find(|t| t.get().name == self.name)
.cloned();
}
_ => return None,
};
module.resolve_type(&self.namespace, &self.name)
}
}
impl EntryName for TypeRef {
fn entry_name(&self) -> String {
self.full_name()
}
}
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct TypeSpec {
pub signature: TypeSig,
pub custom_attributes: Vec<CustomAttribute>,
}
impl EntryName for TypeSpec {
fn entry_name(&self) -> String {
format!("{:?}", self.signature)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum MemberRefParent {
TypeDef(Entry<TypeDef>),
TypeRef(Entry<TypeRef>),
ModuleRef(Entry<ModuleRef>),
Method(Entry<Method>),
TypeSpec(Entry<TypeSpec>),
}
impl MemberRefParent {
pub fn as_type(&self) -> Option<TypeDefOrRef> {
match self {
Self::TypeDef(t) => Some(TypeDefOrRef::TypeDef(t.clone())),
Self::TypeRef(t) => Some(TypeDefOrRef::TypeRef(t.clone())),
Self::TypeSpec(t) => Some(TypeDefOrRef::TypeSpec(t.clone())),
_ => None,
}
}
}
impl Default for MemberRefParent {
fn default() -> Self {
Self::TypeRef(Entry::new(TypeRef::default()))
}
}
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct MemberRef {
pub parent: MemberRefParent,
pub name: String,
pub signature: MemberRefSig,
pub custom_attributes: Vec<CustomAttribute>,
}
impl EntryName for MemberRef {
fn entry_name(&self) -> String {
let parent = self
.parent
.as_type()
.map(|t| t.full_name())
.unwrap_or_default();
format!("{}::{}", parent, self.name)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum MethodDefOrRef {
Method(Entry<Method>),
MemberRef(Entry<MemberRef>),
}
impl MethodDefOrRef {
pub fn name(&self) -> String {
match self {
Self::Method(m) => m.get().name.clone(),
Self::MemberRef(m) => m.get().name.clone(),
}
}
}
impl Default for MethodDefOrRef {
fn default() -> Self {
Self::MemberRef(Entry::new(MemberRef::default()))
}
}
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct MethodSpec {
pub method: MethodDefOrRef,
pub instantiation: Vec<TypeSig>,
pub custom_attributes: Vec<CustomAttribute>,
}
impl EntryName for MethodSpec {
fn entry_name(&self) -> String {
self.method.name()
}
}
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct ModuleRef {
pub name: String,
pub custom_attributes: Vec<CustomAttribute>,
}
impl EntryName for ModuleRef {
fn entry_name(&self) -> String {
self.name.clone()
}
}