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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#![feature(in_band_lifetimes)]
#![feature(never_type)]
#![feature(specialization)]
#![feature(const_fn)]
#![warn(unused_imports)]

use lark_collections::{IndexVec, Seq};
use lark_debug_derive::DebugWith;
use lark_debug_with::DebugWith;
use lark_entity::Entity;
use lark_error::ErrorReported;
use lark_error::ErrorSentinel;
use lark_string::GlobalIdentifier;
use lark_unify::InferVar;
use std::fmt::{self, Debug};
use std::hash::Hash;
use std::iter::IntoIterator;
use std::sync::Arc;

pub mod base_inferred;
pub mod declaration;
pub mod full_inferred;
pub mod identity;
pub mod map_family;

pub trait TypeFamily: Copy + Clone + Debug + DebugWith + Eq + Hash + 'static {
    type InternTables: AsRef<Self::InternTables>;

    type Repr: Copy + Clone + Debug + DebugWith + Eq + Hash;
    type Perm: Copy + Clone + Debug + DebugWith + Eq + Hash;
    type Base: Copy + Clone + Debug + DebugWith + Eq + Hash;

    type Placeholder: Copy + Clone + Debug + DebugWith + Eq + Hash;

    fn intern_base_data(
        tables: &dyn AsRef<Self::InternTables>,
        base_data: BaseData<Self>,
    ) -> Self::Base;

    fn own_perm(tables: &dyn AsRef<Self::InternTables>) -> Self::Perm;

    fn direct_repr(tables: &dyn AsRef<Self::InternTables>) -> Self::Repr {
        Self::known_repr(tables, ReprKind::Direct)
    }

    fn known_repr(tables: &dyn AsRef<Self::InternTables>, repr_kind: ReprKind) -> Self::Repr;

    fn error_type(tables: &dyn AsRef<Self::InternTables>) -> Ty<Self> {
        Ty {
            repr: Self::direct_repr(tables),
            perm: Self::own_perm(tables),
            base: Self::error_base_data(tables),
        }
    }

    fn error_base_data(tables: &dyn AsRef<Self::InternTables>) -> Self::Base {
        Self::intern_base_data(
            tables,
            BaseData {
                kind: BaseKind::Error,
                generics: Generics::empty(),
            },
        )
    }
}

/// A type is the combination of a *permission* and a *base type*.
#[derive(Copy, Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub struct Ty<F: TypeFamily> {
    pub repr: F::Repr,
    pub perm: F::Perm,
    pub base: F::Base,
}

impl<DB, F> ErrorSentinel<&DB> for Ty<F>
where
    DB: AsRef<F::InternTables>,
    F: TypeFamily,
{
    fn error_sentinel(db: &DB, _report: ErrorReported) -> Self {
        F::error_type(db)
    }
}

/// Indicates something that we've opted not to track statically.
#[derive(Copy, Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub struct Erased;

/// The "base data" for a type.
#[derive(Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub struct BaseData<F: TypeFamily> {
    pub kind: BaseKind<F>,
    pub generics: Generics<F>,
}

impl<F: TypeFamily> BaseData<F> {
    pub fn from_placeholder(p: F::Placeholder) -> Self {
        BaseData {
            kind: BaseKind::Placeholder(p),
            generics: Generics::empty(),
        }
    }
}

/// The *kinds* of base types we have on offer.
#[derive(Copy, Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub enum BaseKind<F: TypeFamily> {
    /// A named type (might be value, might be linear, etc).
    Named(Entity),

    /// Instantiated generic type -- exists only in type-check results
    /// for a function.
    Placeholder(F::Placeholder),

    /// Indicates that a type error was reported.
    Error,
}

/// Used as the value for inferable things during inference -- either
/// a given `Base` (etc) maps to an inference variable or to some
/// known value.
#[derive(Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub enum InferVarOr<T> {
    InferVar(InferVar),
    Known(T),
}

impl<T> InferVarOr<T> {
    pub fn assert_known(self) -> T {
        match self {
            InferVarOr::InferVar(_) => panic!("assert_known invoked on infer var"),
            InferVarOr::Known(v) => v,
        }
    }
}

/// A "placeholder" represents a dummy type (or permission, etc) meant to represent
/// "any type". It is used when you are "inside" a "forall" binder -- so, for example,
/// when we are type-checking a function like `fn foo<T>`, the `T` is represented by
/// a placeholder.
#[derive(Copy, Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub struct Placeholder {
    pub universe: Universe,
    pub bound_var: BoundVar,
}

/// A "universe" is a set of names -- the root universe (U(0)) contains all
/// the "global names"; each time we traverse into a binder, we instantiate a
/// new universe (e.g., U(1)) that can see all things from lower universes
/// as well as some new placeholders.
lark_collections::index_type! {
    pub struct Universe {
        debug_name["U"],
        ..
    }
}

lark_debug_with::debug_fallback_impl!(Universe);

impl Universe {
    pub const ROOT: Universe = Universe::from_u32(0);
}

/// A "bound variable" refers to one of the generic type parameters in scope
/// within a declaration. So, for example, if you have `struct Foo<T> { x: T }`,
/// then the bound var #0 would be `T`.
#[derive(Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub enum BoundVarOr<T> {
    BoundVar(BoundVar),
    Known(T),
}

impl<T> BoundVarOr<T> {
    pub fn assert_known(self) -> T {
        match self {
            BoundVarOr::BoundVar(_) => panic!("`assert_known` invoked on bound var"),
            BoundVarOr::Known(v) => v,
        }
    }
}

lark_collections::index_type! {
    pub struct BoundVar { .. }
}

lark_debug_with::debug_fallback_impl!(BoundVar);

/// A set of generic arguments; e.g., in a type like `Vec<i32>`, this
/// would be `[i32]`.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Generics<F: TypeFamily> {
    elements: Seq<Generic<F>>,
}

impl<F: TypeFamily> Generics<F> {
    pub fn empty() -> Self {
        Generics {
            elements: Seq::default(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn is_not_empty(&self) -> bool {
        self.len() != 0
    }

    pub fn len(&self) -> usize {
        self.elements.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = Generic<F>> + '_ {
        self.into_iter()
    }

    pub fn elements(&self) -> &[Generic<F>] {
        &self.elements[..]
    }

    /// Append an item to this vector; if this set of generics is
    /// shared, this will clone the contents so that we own them
    /// privately. (Effectively generic lists are a copy-on-write data
    /// structure.)
    pub fn push(&mut self, generic: Generic<F>) {
        self.extend(std::iter::once(generic));
    }
}

impl<F: TypeFamily> DebugWith for Generics<F> {
    fn fmt_with<Cx: ?Sized>(&self, cx: &Cx, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_list()
            .entries(self.elements().iter().map(|e| e.debug_with(cx)))
            .finish()
    }
}

/// Append items to this generics, cloning if it is shared with
/// others. (Generics are effectively a simple persistent vector.)
impl<F> std::iter::Extend<Generic<F>> for Generics<F>
where
    F: TypeFamily,
{
    fn extend<T>(&mut self, iter: T)
    where
        T: IntoIterator<Item = Generic<F>>,
    {
        self.elements.extend(iter);
    }
}

impl<F> std::ops::Index<BoundVar> for Generics<F>
where
    F: TypeFamily,
{
    type Output = Generic<F>;

    fn index(&self, index: BoundVar) -> &Self::Output {
        &self.elements()[index.as_usize()]
    }
}

impl<F: TypeFamily> std::iter::FromIterator<Generic<F>> for Generics<F> {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = Generic<F>>,
    {
        Generics {
            elements: Seq::from_iter(iter),
        }
    }
}

impl<F: TypeFamily> IntoIterator for &'iter Generics<F> {
    type IntoIter = std::iter::Cloned<std::slice::Iter<'iter, Generic<F>>>;
    type Item = Generic<F>;

    fn into_iter(self) -> Self::IntoIter {
        self.elements().iter().cloned()
    }
}

/// The value of a single generic argument; e.g., in a type like
/// `Vec<i32>`, this might be the `i32`.
#[allow(type_alias_bounds)]
pub type Generic<F: TypeFamily> = GenericKind<Ty<F>>;

/// An enum that lists out the various "kinds" of generic arguments
/// (currently only types) and a distinct type of value for each kind.
#[derive(Copy, Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub enum GenericKind<T> {
    Ty(T),
}

impl<T> GenericKind<T> {
    pub fn assert_ty(self) -> T {
        match self {
            GenericKind::Ty(ty) => ty,
        }
    }
}

/// Signature from a function or method: `(T1, T2) -> T3`.  `inputs`
/// are the list of the types of the arguments, and `output` is the
/// return type.
///
/// Note: the signature of a method *includes* the `self` type.
#[derive(Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub struct Signature<F: TypeFamily> {
    pub inputs: Seq<Ty<F>>,
    pub output: Ty<F>,
}

impl<F: TypeFamily> Signature<F> {
    pub fn error_sentinel(tables: &dyn AsRef<F::InternTables>, num_inputs: usize) -> Signature<F> {
        Signature {
            inputs: (0..num_inputs).map(|_| F::error_type(tables)).collect(),
            output: F::error_type(tables),
        }
    }
}

/// The "generic declarations" list out the generic parameters for a
/// given item. Since items inherit generic items from one another
/// (e.g., from their parents),
#[derive(Clone, Debug, DebugWith, Default, PartialEq, Eq, Hash)]
pub struct GenericDeclarations {
    pub parent_item: Option<Entity>,
    pub declarations: IndexVec<BoundVar, GenericKind<GenericTyDeclaration>>,
}

impl GenericDeclarations {
    pub fn empty(parent_item: Option<Entity>) -> Arc<Self> {
        Arc::new(GenericDeclarations {
            parent_item,
            declarations: IndexVec::default(),
        })
    }

    pub fn is_empty(&self) -> bool {
        self.declarations.is_empty() && self.parent_item.is_none()
    }
}

/// Declaration of an individual generic type parameter.
#[derive(Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub struct GenericTyDeclaration {
    pub def_id: Entity,
    pub name: GlobalIdentifier,
}

#[derive(Copy, Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub enum PermKind {
    Own,
    Share,
    Borrow,
}

/// Encodes whether we reach the data through pointer indirection or not.
#[derive(Copy, Clone, Debug, DebugWith, PartialEq, Eq, Hash)]
pub enum ReprKind {
    /// Store the fields in place.
    Direct,

    /// Store fields via a pointer.
    Indirect,
}