1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::collections::HashSet;
use crate::{Hash, Item, ValueType};
use std::fmt;
use std::sync::Arc;

/// Metadata about a closure.
#[derive(Debug, Clone)]
pub struct MetaClosureCapture {
    /// Identity of the captured variable.
    pub ident: String,
}

/// Metadata about an item in the context.
#[derive(Debug, Clone)]
pub enum Meta {
    /// Metadata about a tuple.
    MetaTuple {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The underlying tuple.
        tuple: MetaTuple,
    },
    /// Metadata about a tuple variant.
    MetaVariantTuple {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The item of the enum.
        enum_item: Item,
        /// The underlying tuple.
        tuple: MetaTuple,
    },
    /// Metadata about an object.
    MetaStruct {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The underlying object.
        object: MetaStruct,
    },
    /// Metadata about a variant object.
    MetaVariantStruct {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The item of the enum.
        enum_item: Item,
        /// The underlying object.
        object: MetaStruct,
    },
    /// An enum item.
    MetaEnum {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The item of the enum.
        item: Item,
    },
    /// A function declaration.
    MetaFunction {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The item of the function declaration.
        item: Item,
    },
    /// A closure.
    MetaClosure {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The item of the closure.
        item: Item,
        /// Sequence of captured variables.
        captures: Arc<Vec<MetaClosureCapture>>,
    },
    /// An async block.
    MetaAsyncBlock {
        /// The value type associated with this meta item.
        value_type: ValueType,
        /// The item of the closure.
        item: Item,
        /// Sequence of captured variables.
        captures: Arc<Vec<MetaClosureCapture>>,
    },
}

impl Meta {
    /// Get the item of the meta.
    pub fn item(&self) -> &Item {
        match self {
            Meta::MetaTuple { tuple, .. } => &tuple.item,
            Meta::MetaVariantTuple { tuple, .. } => &tuple.item,
            Meta::MetaStruct { object, .. } => &object.item,
            Meta::MetaVariantStruct { object, .. } => &object.item,
            Meta::MetaEnum { item, .. } => item,
            Meta::MetaFunction { item, .. } => item,
            Meta::MetaClosure { item, .. } => item,
            Meta::MetaAsyncBlock { item, .. } => item,
        }
    }

    /// Get the value type of the meta item.
    pub fn value_type(&self) -> Option<ValueType> {
        match self {
            Self::MetaTuple { value_type, .. } => Some(*value_type),
            Self::MetaVariantTuple { .. } => None,
            Self::MetaStruct { value_type, .. } => Some(*value_type),
            Self::MetaVariantStruct { .. } => None,
            Self::MetaEnum { value_type, .. } => Some(*value_type),
            Self::MetaFunction { value_type, .. } => Some(*value_type),
            Self::MetaClosure { value_type, .. } => Some(*value_type),
            Self::MetaAsyncBlock { value_type, .. } => Some(*value_type),
        }
    }
}

impl fmt::Display for Meta {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::MetaTuple { tuple, .. } => {
                write!(fmt, "struct {}", tuple.item)?;
            }
            Self::MetaVariantTuple { tuple, .. } => {
                write!(fmt, "variant {}", tuple.item)?;
            }
            Self::MetaStruct { object, .. } => {
                write!(fmt, "struct {}", object.item)?;
            }
            Self::MetaVariantStruct { object, .. } => {
                write!(fmt, "variant {}", object.item)?;
            }
            Self::MetaEnum { item, .. } => {
                write!(fmt, "enum {}", item)?;
            }
            Self::MetaFunction { item, .. } => {
                write!(fmt, "fn {}", item)?;
            }
            Self::MetaClosure { item, .. } => {
                write!(fmt, "closure {}", item)?;
            }
            Self::MetaAsyncBlock { item, .. } => {
                write!(fmt, "async block {}", item)?;
            }
        }

        Ok(())
    }
}

/// The metadata about a type.
#[derive(Debug, Clone)]
pub struct MetaExternal {
    /// The path to the type.
    pub item: Item,
}

/// The metadata about a type.
#[derive(Debug, Clone)]
pub struct MetaStruct {
    /// The path to the object.
    pub item: Item,
    /// Fields associated with the type.
    pub fields: Option<HashSet<String>>,
}

/// The metadata about a variant.
#[derive(Debug, Clone)]
pub struct MetaTuple {
    /// The path to the tuple.
    pub item: Item,
    /// The number of arguments the variant takes.
    pub args: usize,
    /// Hash of the constructor function.
    pub hash: Hash,
}