use crate::collections::HashSet;
use crate::{ConstValue, Hash, Id, Item, SourceId, Span, Type};
use std::fmt;
use std::path::PathBuf;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct CompileMetaCapture {
pub ident: Box<str>,
}
#[derive(Debug, Clone)]
pub struct CompileMeta {
pub item: Item,
pub kind: CompileMetaKind,
pub source: Option<CompileSource>,
}
#[derive(Debug, Clone)]
pub struct CompileSource {
pub source_id: SourceId,
pub span: Span,
pub path: Option<PathBuf>,
}
impl CompileMeta {
pub fn base_type_of(&self) -> Option<Type> {
match &self.kind {
CompileMetaKind::UnitStruct { type_of, .. } => Some(*type_of),
CompileMetaKind::TupleStruct { type_of, .. } => Some(*type_of),
CompileMetaKind::Struct { type_of, .. } => Some(*type_of),
CompileMetaKind::Enum { type_of, .. } => Some(*type_of),
CompileMetaKind::Function { type_of, .. } => Some(*type_of),
CompileMetaKind::Closure { type_of, .. } => Some(*type_of),
CompileMetaKind::AsyncBlock { type_of, .. } => Some(*type_of),
CompileMetaKind::UnitVariant { .. } => None,
CompileMetaKind::TupleVariant { .. } => None,
CompileMetaKind::StructVariant { .. } => None,
CompileMetaKind::Const { .. } => None,
CompileMetaKind::ConstFn { .. } => None,
CompileMetaKind::Import { .. } => None,
}
}
}
impl fmt::Display for CompileMeta {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.kind {
CompileMetaKind::UnitStruct { .. } => {
write!(fmt, "struct {}", self.item)?;
}
CompileMetaKind::TupleStruct { .. } => {
write!(fmt, "struct {}", self.item)?;
}
CompileMetaKind::Struct { .. } => {
write!(fmt, "struct {}", self.item)?;
}
CompileMetaKind::UnitVariant { .. } => {
write!(fmt, "unit variant {}", self.item)?;
}
CompileMetaKind::TupleVariant { .. } => {
write!(fmt, "variant {}", self.item)?;
}
CompileMetaKind::StructVariant { .. } => {
write!(fmt, "variant {}", self.item)?;
}
CompileMetaKind::Enum { .. } => {
write!(fmt, "enum {}", self.item)?;
}
CompileMetaKind::Function { .. } => {
write!(fmt, "fn {}", self.item)?;
}
CompileMetaKind::Closure { .. } => {
write!(fmt, "closure {}", self.item)?;
}
CompileMetaKind::AsyncBlock { .. } => {
write!(fmt, "async block {}", self.item)?;
}
CompileMetaKind::Const { .. } => {
write!(fmt, "const {}", self.item)?;
}
CompileMetaKind::ConstFn { .. } => {
write!(fmt, "const fn {}", self.item)?;
}
CompileMetaKind::Import { .. } => {
write!(fmt, "import {}", self.item)?;
}
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub enum CompileMetaKind {
UnitStruct {
type_of: Type,
empty: CompileMetaEmpty,
},
TupleStruct {
type_of: Type,
tuple: CompileMetaTuple,
},
Struct {
type_of: Type,
object: CompileMetaStruct,
},
UnitVariant {
type_of: Type,
enum_item: Item,
empty: CompileMetaEmpty,
},
TupleVariant {
type_of: Type,
enum_item: Item,
tuple: CompileMetaTuple,
},
StructVariant {
type_of: Type,
enum_item: Item,
object: CompileMetaStruct,
},
Enum {
type_of: Type,
},
Function {
type_of: Type,
},
Closure {
type_of: Type,
captures: Arc<[CompileMetaCapture]>,
do_move: bool,
},
AsyncBlock {
type_of: Type,
captures: Arc<[CompileMetaCapture]>,
do_move: bool,
},
Const {
const_value: ConstValue,
},
ConstFn {
id: Id,
},
Import {
imported: Item,
},
}
#[derive(Debug, Clone)]
pub struct CompileMetaEmpty {
pub hash: Hash,
}
#[derive(Debug, Clone)]
pub struct CompileMetaStruct {
pub fields: Option<HashSet<Box<str>>>,
}
#[derive(Debug, Clone)]
pub struct CompileMetaTuple {
pub args: usize,
pub hash: Hash,
}