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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
pub(crate) mod destruc;
pub(crate) mod error;
pub(crate) mod exports;
pub(crate) mod import;
pub(crate) mod loader;
pub(crate) mod lowering;
mod macros;
pub(crate) mod ns;
mod prim;
mod records;
pub(crate) mod scope;
mod types;
mod util;
pub(crate) mod var_id;
pub(crate) mod visitor;

use std::sync::Arc;

use arret_syntax::datum::Datum;
use arret_syntax::span::Span;

use crate::rfi;
use crate::ty;
use crate::ty::purity;
use crate::ty::record;
use crate::ty::Ty;

pub use crate::hir::var_id::{ExportId, LocalId};

/// DeclTy is a type declared by a user
///
/// The `Known` variant indicates the type is specified while `Free` indicates it must be inferred.
#[derive(PartialEq, Debug, Clone)]
pub enum DeclTy {
    Known(ty::Ref<ty::Poly>),
    Free,
}

impl From<Ty<ty::Poly>> for DeclTy {
    fn from(ty: Ty<ty::Poly>) -> Self {
        DeclTy::Known(ty::Ref::Fixed(ty))
    }
}

impl From<ty::Ref<ty::Poly>> for DeclTy {
    fn from(poly: ty::Ref<ty::Poly>) -> Self {
        DeclTy::Known(poly)
    }
}

/// Decl is a purity declared by a user
///
/// The `Known` variant indicates the purity is specified while `Free` indicates it must be
/// inferred.
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum DeclPurity {
    Known(purity::Ref),
    Free,
}

impl From<purity::Ref> for DeclPurity {
    fn from(poly: purity::Ref) -> Self {
        DeclPurity::Known(poly)
    }
}

#[cfg(test)]
impl From<purity::Purity> for DeclPurity {
    fn from(purity: purity::Purity) -> Self {
        DeclPurity::Known(purity::Ref::Fixed(purity))
    }
}

pub trait Phase: Clone + std::cmp::PartialEq + std::fmt::Debug {
    type Purity: Clone + std::cmp::PartialEq + std::fmt::Debug;
    type DeclType: Clone + std::cmp::PartialEq + std::fmt::Debug;
    type ResultType: Clone + std::cmp::PartialEq + std::fmt::Debug;
    type TyArgs: Clone + std::cmp::PartialEq + std::fmt::Debug;
}

#[derive(Clone, PartialEq, Debug)]
pub struct Inferred {}
impl Phase for Inferred {
    type Purity = purity::Ref;
    type DeclType = ty::Ref<ty::Poly>;
    type ResultType = ty::Ref<ty::Poly>;
    type TyArgs = ty::ty_args::TyArgs<ty::Poly>;
}

#[derive(Clone, PartialEq, Debug)]
pub struct Lowered {}
impl Phase for Lowered {
    type Purity = DeclPurity;
    type DeclType = DeclTy;
    type ResultType = ();
    type TyArgs = ();
}

#[derive(PartialEq, Debug, Clone)]
pub struct Fun<P: Phase> {
    pub span: Span,

    pub pvars: purity::PVars,
    pub tvars: ty::TVars,

    pub purity: P::Purity,
    pub params: destruc::List<P>,

    pub ret_ty: P::DeclType,
    pub ret_ty_span: Option<Span>,

    pub body_expr: Expr<P>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct Cond<P: Phase> {
    pub span: Span,
    pub test_expr: Expr<P>,
    pub true_expr: Expr<P>,
    pub false_expr: Expr<P>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct Let<P: Phase> {
    pub span: Span,
    pub destruc: destruc::Destruc<P>,
    pub value_expr: Expr<P>,
    pub body_expr: Expr<P>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct App<P: Phase> {
    pub span: Span,
    pub fun_expr: Expr<P>,
    pub ty_args: P::TyArgs,
    pub fixed_arg_exprs: Vec<Expr<P>>,
    pub rest_arg_expr: Option<Expr<P>>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct Recur<P: Phase> {
    pub span: Span,
    pub fixed_arg_exprs: Vec<Expr<P>>,
    pub rest_arg_expr: Option<Expr<P>>,
}

#[derive(PartialEq, Debug, Clone)]
pub struct FieldAccessor {
    pub span: Span,
    pub record_cons: record::ConsId,
    pub field_index: usize,
}

#[derive(PartialEq, Debug, Clone)]
pub struct Expr<P: Phase> {
    pub result_ty: P::ResultType,
    pub kind: ExprKind<P>,
}

impl From<Datum> for Expr<Lowered> {
    fn from(datum: Datum) -> Expr<Lowered> {
        ExprKind::Lit(datum).into()
    }
}

#[derive(PartialEq, Debug, Clone)]
pub enum ExprKind<P: Phase> {
    Lit(Datum),
    App(Box<App<P>>),
    Recur(Box<Recur<P>>),
    Fun(Box<Fun<P>>),
    RustFun(Arc<rfi::Fun>),
    Let(Box<Let<P>>),
    Cond(Box<Cond<P>>),
    ExportRef(Span, ExportId),
    LocalRef(Span, LocalId),

    TyPred(Span, ty::pred::TestTy),
    EqPred(Span),
    RecordCons(Span, record::ConsId),
    FieldAccessor(Box<FieldAccessor>),

    Do(Vec<Expr<P>>),

    /// Used for tracing macro expansion for error report and debug information
    ///
    /// Other than the above this should be treated identically to the inner expression.
    MacroExpand(Span, Box<Expr<P>>),
}

impl From<ExprKind<Lowered>> for Expr<Lowered> {
    fn from(kind: ExprKind<Lowered>) -> Expr<Lowered> {
        Expr {
            result_ty: (),
            kind,
        }
    }
}

#[derive(PartialEq, Debug)]
pub struct Def<P: Phase> {
    pub span: Span,
    pub macro_invocation_span: Option<Span>,
    pub destruc: destruc::Destruc<P>,
    pub value_expr: Expr<P>,
}

pub use self::loader::PackagePaths;
pub use self::types::lower_poly;
pub use self::types::str_for_purity;
pub use self::types::str_for_ty_ref;

#[cfg(test)]
pub use self::types::{lower_polymorphic_var_set, poly_for_str, try_lower_purity, tvar_bounded_by};

#[cfg(test)]
pub use self::lowering::expr_for_str;