Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,583 @@
|
||||
#! @file types.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the type table, which gives every type the checker meets an ID of
|
||||
#! its own. A type is interned by its kind, two numbers, and its parts, so two
|
||||
#! types are the same exactly where their IDs are, and the parts of each are a
|
||||
#! run of one vector, as the runs of a syntax are.
|
||||
|
||||
module sakura::analyzer;
|
||||
|
||||
|
||||
#! The kinds of type.
|
||||
pub enum ty : u8 {
|
||||
error, #! What a mistake gives, which fits anywhere, so a mistake is reported once.
|
||||
void_type, #! `void`, which a function giving nothing gives.
|
||||
null_type, #! The type of `null`, which becomes any pointer.
|
||||
boolean, #! `bool`.
|
||||
signed, #! `first` is its bits.
|
||||
unsigned, #! `first` is its bits.
|
||||
floating, #! `first` is its bits.
|
||||
untyped_int, #! An integer constant, an `i64` unless another number type is expected.
|
||||
untyped_uint, #! An integer written with `u`, a `uint` unless another unsigned type is expected.
|
||||
untyped_char, #! A character, a `u32` unless another integer type is expected.
|
||||
untyped_float, #! A float constant, an `f64` unless another float type is expected.
|
||||
untyped_float32, #! A float constant written with `f`, an `f32` unless an `f64` is expected.
|
||||
untyped_bool, #! A constant `true` or `false`.
|
||||
untyped_text, #! Text, a `span<u8>` or a `string`, whichever is expected.
|
||||
empty, #! The element of `[]`, which becomes any element.
|
||||
reference, #! `first` is what it refers to, and `second` is 1 where it writes.
|
||||
pointer, #! `first` is what it points at, and `second` is 1 where it writes.
|
||||
fixed, #! `first` is its element, and `second` how many it holds, or within a template its one part, a constant only each instance knows.
|
||||
function, #! `first` is its result, `parts` its parameters, and `second` 1 where the last is variadic.
|
||||
named, #! `first` is its declaration, and `parts` its type arguments.
|
||||
param, #! `first` is the declaration of the type parameter it is.
|
||||
pack, #! `parts` are its types, or `first` is the declaration of the pack it is, while unknown.
|
||||
value, #! A constant type argument: `first` is the type of its value, `second` its ID among the checker's known values, and `parts` the constant parameters it is worked out from, within a template.
|
||||
matched, #! `match const T { ... }` as a type, while `T` holds a type parameter: `parts` are `T`, then a pattern and the type it gives for each of the arms' patterns, a pattern of 0 being `else`, and `first` and `second` the file and node it was written at.
|
||||
}
|
||||
|
||||
#! What a type's `flags` hold: what is so of it, which interning it works out,
|
||||
#! and what the checker worked out of it and keeps, which `forget` lets go of.
|
||||
pub enum tf : u8 {
|
||||
open = 0x01, #! It holds a type parameter anywhere within.
|
||||
holds = 0x02, #! It holds a constant, or a fixed array whose length is one, anywhere within.
|
||||
asked = 0x04, #! Whether it is an alias was worked out, which `alias` says.
|
||||
alias = 0x08, #! It is an alias.
|
||||
weighed = 0x10, #! Whether values of it close was worked out, which `closes` says.
|
||||
closes = 0x20, #! Values of it close, or hold what does.
|
||||
reported = 0x40, #! A `match const` type no arm of which fits, or that is not chosen within as many steps as aliases are followed, was reported.
|
||||
}
|
||||
|
||||
#! Lets go of what the checker worked out of each type and kept in its flags,
|
||||
#! which a change may have changed.
|
||||
#! @param this The type table.
|
||||
pub def type_table::forget(&mut this): void {
|
||||
val kept := tf::open as u8 | tf::holds as u8;
|
||||
for id in 0..this.flags.length {
|
||||
this.flags.p[id] &= kept;
|
||||
}
|
||||
}
|
||||
|
||||
#! The IDs of the types every program has, which the type table interns first
|
||||
#! and in this order.
|
||||
pub enum bt : u32 {
|
||||
#! The error type.
|
||||
error,
|
||||
#! `void`.
|
||||
void_type,
|
||||
#! The type of `null`.
|
||||
null_type,
|
||||
#! `bool`.
|
||||
boolean,
|
||||
#! `i8`.
|
||||
int8,
|
||||
#! `i16`.
|
||||
int16,
|
||||
#! `i32`.
|
||||
int32,
|
||||
#! `i64`.
|
||||
int64,
|
||||
#! `u8`.
|
||||
uint8,
|
||||
#! `u16`.
|
||||
uint16,
|
||||
#! `u32`.
|
||||
uint32,
|
||||
#! `u64`.
|
||||
uint64,
|
||||
#! `f32`.
|
||||
float32,
|
||||
#! `f64`.
|
||||
float64,
|
||||
#! An integer constant.
|
||||
untyped_int,
|
||||
#! An integer constant written with `u`.
|
||||
untyped_uint,
|
||||
#! A character.
|
||||
untyped_char,
|
||||
#! A float constant.
|
||||
untyped_float,
|
||||
#! A float constant written with `f`.
|
||||
untyped_float32,
|
||||
#! A constant `true` or `false`.
|
||||
untyped_bool,
|
||||
#! Text written as a literal.
|
||||
untyped_text,
|
||||
#! The element of `[]`.
|
||||
empty,
|
||||
#! How many built-in types there are, which is the ID of the first other type.
|
||||
count,
|
||||
}
|
||||
|
||||
#! Every type, by its ID, a field in a column of its own.
|
||||
pub struct type_table {
|
||||
var kinds: column<ty>; #! The kind of each type.
|
||||
var firsts: column<u32>; #! The first number of each type, which its kind says the meaning of.
|
||||
var seconds: column<u32>; #! The second number of each type.
|
||||
var parts: column<u32>; #! The run of each type's parts in `runs`, 0 where it has none.
|
||||
pub var flags: column<u8>; #! What is so of each type, as `tf` says: what it holds, and what was worked out of it.
|
||||
var runs: column<u32>; #! The runs of parts, each a count and then the parts.
|
||||
var index: id_index; #! The types of each hash, which interning finds them by.
|
||||
var scratch: column<u32>; #! The parts of the types substituting is making, those of each inner one past those of the ones around it.
|
||||
}
|
||||
|
||||
#! Makes a type table holding the types every program has.
|
||||
#! @returns The table, in which the IDs of `bt` are those types.
|
||||
pub def type_table::new(): type_table {
|
||||
var made := type_table {
|
||||
kinds = column::<ty>::new(),
|
||||
firsts = column::<u32>::new(),
|
||||
seconds = column::<u32>::new(),
|
||||
parts = column::<u32>::new(),
|
||||
flags = column::<u8>::new(),
|
||||
runs = column::<u32>::new(),
|
||||
index = id_index::new(),
|
||||
scratch = column::<u32>::new()
|
||||
};
|
||||
|
||||
made.runs.append(0);
|
||||
val none := span::<u32>::blank();
|
||||
made.intern(ty::error, 0, 0, none);
|
||||
made.intern(ty::void_type, 0, 0, none);
|
||||
made.intern(ty::null_type, 0, 0, none);
|
||||
made.intern(ty::boolean, 8, 0, none);
|
||||
made.intern(ty::signed, 8, 0, none);
|
||||
made.intern(ty::signed, 16, 0, none);
|
||||
made.intern(ty::signed, 32, 0, none);
|
||||
made.intern(ty::signed, 64, 0, none);
|
||||
made.intern(ty::unsigned, 8, 0, none);
|
||||
made.intern(ty::unsigned, 16, 0, none);
|
||||
made.intern(ty::unsigned, 32, 0, none);
|
||||
made.intern(ty::unsigned, 64, 0, none);
|
||||
made.intern(ty::floating, 32, 0, none);
|
||||
made.intern(ty::floating, 64, 0, none);
|
||||
made.intern(ty::untyped_int, 0, 0, none);
|
||||
made.intern(ty::untyped_uint, 0, 0, none);
|
||||
made.intern(ty::untyped_char, 0, 0, none);
|
||||
made.intern(ty::untyped_float, 0, 0, none);
|
||||
made.intern(ty::untyped_float32, 0, 0, none);
|
||||
made.intern(ty::untyped_bool, 0, 0, none);
|
||||
made.intern(ty::untyped_text, 0, 0, none);
|
||||
made.intern(ty::empty, 0, 0, none);
|
||||
expects(made.kinds.length == bt::count as u64);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! How many types there are.
|
||||
#! @param this The type table.
|
||||
#! @returns The number of types, which is one more than the newest's ID.
|
||||
pub val type_table::count: u64 { get => this.kinds.length; }
|
||||
|
||||
#! The ID of the type of the kind, numbers, and parts, made where it is new.
|
||||
#! @param this The type table.
|
||||
#! @param kind The type's kind.
|
||||
#! @param first Its first number, which its kind says the meaning of.
|
||||
#! @param second Its second number.
|
||||
#! @param items Its parts, in order.
|
||||
#! @returns The type's ID.
|
||||
pub def type_table::intern(&mut this, kind: ty, first: u32, second: u32, items: span<u32>): u32 {
|
||||
var hash := mix(mix(mix(items.length, kind as u64), first as u64), second as u64);
|
||||
for &item in items {
|
||||
hash = mix(hash, item as u64);
|
||||
}
|
||||
|
||||
var walk := this.index.walk(hash);
|
||||
var at := this.index.next(&mut walk);
|
||||
while at != 0 {
|
||||
val id := at - 1;
|
||||
if this.kinds.p[id] == kind && this.firsts.p[id] == first && this.seconds.p[id] == second
|
||||
&& this.same_parts(id, items) {
|
||||
return id;
|
||||
}
|
||||
at = this.index.next(&mut walk);
|
||||
}
|
||||
|
||||
# A type is open where it is a type parameter, or holds one.
|
||||
var open := kind == ty::param || (kind == ty::pack && items.length == 0 && first != 0);
|
||||
if kind == ty::reference || kind == ty::pointer || kind == ty::fixed || kind == ty::function
|
||||
|| kind == ty::value {
|
||||
open = open || this.is_open(first);
|
||||
}
|
||||
# A type holds a constant where it is one, is a fixed array whose length is one,
|
||||
# or holds either, which only substituting within it may work out.
|
||||
var held := kind == ty::value || (kind == ty::fixed && items.length != 0);
|
||||
if kind == ty::reference || kind == ty::pointer || kind == ty::fixed || kind == ty::function {
|
||||
held = held || this.holds_value(first);
|
||||
}
|
||||
for &item in items {
|
||||
open = open || this.is_open(item as u32);
|
||||
held = held || this.holds_value(item as u32);
|
||||
}
|
||||
|
||||
var run: u32 = 0;
|
||||
if items.length != 0 {
|
||||
run = this.runs.length as u32;
|
||||
this.runs.append(items.length as u32);
|
||||
this.runs.extend(items);
|
||||
}
|
||||
|
||||
val id := this.kinds.length as u32;
|
||||
this.kinds.append(kind);
|
||||
this.firsts.append(first);
|
||||
this.seconds.append(second);
|
||||
this.parts.append(run);
|
||||
this.flags.append((open ? tf::open as u8 : 0) | (held ? tf::holds as u8 : 0));
|
||||
this.index.add(hash, id);
|
||||
return id;
|
||||
}
|
||||
|
||||
def type_table::same_parts(&this, id: u32, items: span<u32>): bool {
|
||||
val run: u32 = this.parts.p[id];
|
||||
val count: u32 = run == 0 ? 0 : this.runs.p[run];
|
||||
return false if count as u64 != items.length;
|
||||
for i in 0..items.length {
|
||||
return false if this.runs.p[run as u64 + 1 + i] != items[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The kind of a type.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Its kind.
|
||||
pub def type_table::kind_of(&this, id: u32): ty { return this.kinds.p[id]; }
|
||||
#! The first number of a type, which its kind says the meaning of.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The number.
|
||||
pub def type_table::first_of(&this, id: u32): u32 { return this.firsts.p[id]; }
|
||||
#! The second number of a type, which its kind says the meaning of.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The number.
|
||||
pub def type_table::second_of(&this, id: u32): u32 { return this.seconds.p[id]; }
|
||||
#! Whether a type holds a type parameter anywhere within, so it stands for other
|
||||
#! types once the parameter is known.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it holds one.
|
||||
pub def type_table::is_open(&this, id: u32): bool { return (this.flags.p[id] & tf::open as u8) != 0; }
|
||||
#! Whether a type holds a constant, or a fixed array whose length is one,
|
||||
#! anywhere within.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it holds one.
|
||||
pub def type_table::holds_value(&this, id: u32): bool { return (this.flags.p[id] & tf::holds as u8) != 0; }
|
||||
|
||||
#! How many parts the type has: the parameters of a function, the type
|
||||
#! arguments of a named type, or the types of a pack.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns How many parts it has.
|
||||
pub def type_table::part_count(&this, id: u32): u32 {
|
||||
val run: u32 = this.parts.p[id];
|
||||
return run == 0 ? 0 : this.runs.p[run];
|
||||
}
|
||||
|
||||
#! A part of a type, by its position.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @param index The part's position, below `part_count`.
|
||||
#! @returns The part's ID.
|
||||
pub def type_table::part_at(&this, id: u32, index: u32): u32 {
|
||||
return this.runs.p[this.parts.p[id] as u64 + 1 + index as u64];
|
||||
}
|
||||
|
||||
#! The parts of the type, where they are.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The IDs of its parts, in order, a view only good until a type with
|
||||
#! parts is interned, so a loop that may intern one walks them by
|
||||
#! `part_at`, or copies them by `parts_of`.
|
||||
pub def type_table::parts_view(&this, id: u32): span<u32> {
|
||||
val run: u32 = this.parts.p[id];
|
||||
return span::<u32>::over(this.runs.p + run + 1, run == 0 ? 0 : this.runs.p[run] as u64);
|
||||
}
|
||||
|
||||
#! The parts of the type, copied, since interning may move where they are.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The IDs of its parts, in order.
|
||||
pub def type_table::parts_of(&this, id: u32): vector<u32> {
|
||||
var made := vector::<u32>::new();
|
||||
made.extend(this.parts_view(id));
|
||||
return made;
|
||||
}
|
||||
|
||||
#! The type of a reference.
|
||||
#! @param this The type table.
|
||||
#! @param referent The type it refers to.
|
||||
#! @param mutable Whether it writes, as `&mut` does.
|
||||
#! @returns The reference's ID.
|
||||
pub def type_table::reference(&mut this, referent: u32, mutable: bool): u32 {
|
||||
return this.intern(ty::reference, referent, mutable ? 1 : 0, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The type of a pointer.
|
||||
#! @param this The type table.
|
||||
#! @param pointee The type it points at.
|
||||
#! @param mutable Whether it writes, as `*mut` does.
|
||||
#! @returns The pointer's ID.
|
||||
pub def type_table::pointer(&mut this, pointee: u32, mutable: bool): u32 {
|
||||
return this.intern(ty::pointer, pointee, mutable ? 1 : 0, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The type of a fixed array.
|
||||
#! @param this The type table.
|
||||
#! @param element The type of its elements.
|
||||
#! @param count How many it holds.
|
||||
#! @returns The array's ID.
|
||||
pub def type_table::fixed(&mut this, element: u32, count: u32): u32 {
|
||||
return this.intern(ty::fixed, element, count, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The type of a fixed array whose length only each instance of a template knows.
|
||||
#! @param this The type table.
|
||||
#! @param element The type of its elements.
|
||||
#! @param length The constant parameter or value it holds as many as.
|
||||
#! @returns The array's ID.
|
||||
pub def type_table::fixed_of(&mut this, element: u32, length: u32): u32 {
|
||||
var parts := vector::<u32>::new();
|
||||
parts.append(length);
|
||||
return this.intern(ty::fixed, element, 0, parts.span);
|
||||
}
|
||||
|
||||
#! The type of a function.
|
||||
#! @param this The type table.
|
||||
#! @param params The types of its parameters, in order.
|
||||
#! @param result The type it gives.
|
||||
#! @param variadic Whether its last parameter takes any number of values.
|
||||
#! @returns The function's ID.
|
||||
pub def type_table::function(&mut this, params: span<u32>, result: u32, variadic: bool): u32 {
|
||||
return this.intern(ty::function, result, variadic ? 1 : 0, params);
|
||||
}
|
||||
|
||||
#! The type a declaration names, given its type arguments.
|
||||
#! @param this The type table.
|
||||
#! @param decl The declaration of the struct, union, enum, trait, or alias.
|
||||
#! @param args Its type arguments, or none where it is not generic.
|
||||
#! @returns The type's ID.
|
||||
pub def type_table::named(&mut this, decl: u32, args: span<u32>): u32 {
|
||||
return this.intern(ty::named, decl, 0, args);
|
||||
}
|
||||
|
||||
#! The type a type parameter is, while what it stands for is not known.
|
||||
#! @param this The type table.
|
||||
#! @param decl The type parameter's declaration.
|
||||
#! @returns The type's ID.
|
||||
pub def type_table::param(&mut this, decl: u32): u32 {
|
||||
return this.intern(ty::param, decl, 0, span::<u32>::blank());
|
||||
}
|
||||
|
||||
#! The pack of the types, or while they are unknown, the pack of the parameter.
|
||||
#! @param this The type table.
|
||||
#! @param items The types the pack holds, or none while they are unknown.
|
||||
#! @param decl The declaration of the pack parameter, while its types are
|
||||
#! unknown.
|
||||
#! @returns The pack's ID.
|
||||
pub def type_table::pack(&mut this, items: span<u32>, decl: u32): u32 {
|
||||
return this.intern(ty::pack, items.length == 0 ? decl : 0, 0, items);
|
||||
}
|
||||
|
||||
#! The pack of the parameters of a variadic function, whose last takes any number
|
||||
#! of values of its type, as `(str, i64...)` of `def(str, i64...): void`. Spread
|
||||
#! into a function's parameters, it makes the function variadic.
|
||||
#! @param this The type table.
|
||||
#! @param items The types of the parameters, the last that of the values it
|
||||
#! takes.
|
||||
#! @returns The pack's ID.
|
||||
pub def type_table::variadic_pack(&mut this, items: span<u32>): u32 {
|
||||
return this.intern(ty::pack, 0, 1, items);
|
||||
}
|
||||
|
||||
#! The pack of a function type's parameters, variadic where it is.
|
||||
#! @param this The type table.
|
||||
#! @param id The function type's ID.
|
||||
#! @param from The first parameter the pack holds.
|
||||
#! @returns The pack's ID.
|
||||
pub def type_table::params_pack(&mut this, id: u32, from: u32): u32 {
|
||||
var items := vector::<u32>::new();
|
||||
for i in from..this.part_count(id) {
|
||||
items.append(this.part_at(id, i as u32));
|
||||
}
|
||||
return this.seconds.p[id] != 0 && items.length != 0 ? this.variadic_pack(items.span) : this.pack(items.span, 0);
|
||||
}
|
||||
|
||||
#! Whether the type is an integer of a known size, or an integer constant.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_integer(&this, id: u32): bool {
|
||||
return match this.kinds.p[id] {
|
||||
ty::signed, ty::unsigned, ty::untyped_int, ty::untyped_uint, ty::untyped_char => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether a type is a float of a known size, or a float constant.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_float(&this, id: u32): bool {
|
||||
return match this.kinds.p[id] {
|
||||
ty::floating, ty::untyped_float, ty::untyped_float32 => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether a type is a number: an integer or a float, of a known size or a
|
||||
#! constant.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_numeric(&this, id: u32): bool {
|
||||
return this.is_integer(id) || this.is_float(id);
|
||||
}
|
||||
|
||||
#! Whether a type is `bool`, or a constant `true` or `false`.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_boolean(&this, id: u32): bool {
|
||||
val kind: ty = this.kinds.p[id];
|
||||
return kind == ty::boolean || kind == ty::untyped_bool;
|
||||
}
|
||||
|
||||
#! Whether the type is a constant's, which takes the type expected of it.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_untyped(&this, id: u32): bool {
|
||||
return match this.kinds.p[id] {
|
||||
ty::untyped_int, ty::untyped_uint, ty::untyped_char, ty::untyped_float,
|
||||
ty::untyped_float32, ty::untyped_bool, ty::untyped_text, ty::empty, ty::null_type => true,
|
||||
else => false
|
||||
};
|
||||
}
|
||||
|
||||
#! Whether the type is a signed integer, of a known size or not.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_signed(&this, id: u32): bool {
|
||||
val kind: ty = this.kinds.p[id];
|
||||
return kind == ty::signed || kind == ty::untyped_int;
|
||||
}
|
||||
|
||||
#! The numeric type an untyped one becomes where nothing else is expected.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns The type it becomes, or the type itself where it is not untyped.
|
||||
pub def type_table::settled(&this, id: u32): u32 {
|
||||
return match this.kinds.p[id] {
|
||||
ty::untyped_int => bt::int64,
|
||||
ty::untyped_uint => bt::uint64,
|
||||
ty::untyped_char => bt::uint32,
|
||||
ty::untyped_float => bt::float64,
|
||||
ty::untyped_float32 => bt::float32,
|
||||
ty::untyped_bool => bt::boolean,
|
||||
else => id
|
||||
};
|
||||
}
|
||||
|
||||
#! The type with each type parameter `from` names replaced by the type `to`
|
||||
#! gives at the same index. A type holding no parameter is itself.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @param from The type parameters to replace.
|
||||
#! @param to The types to replace each with.
|
||||
#! @returns The type made.
|
||||
pub def type_table::substitute(&mut this, id: u32, from: span<u32>, to: span<u32>): u32 {
|
||||
return id if !this.is_open(id) || from.length == 0;
|
||||
|
||||
val kind: ty = this.kinds.p[id];
|
||||
val first: u32 = this.firsts.p[id];
|
||||
match kind {
|
||||
ty::param => {
|
||||
for i in 0..from.length {
|
||||
return to[i] if from[i] == first && i < to.length;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
ty::reference, ty::pointer => {
|
||||
val inner := this.substitute(first, from, to);
|
||||
return this.intern(kind, inner, this.seconds.p[id], span::<u32>::blank());
|
||||
}
|
||||
# A length only each instance knows is given its parameter's argument, which the
|
||||
# checker then works out.
|
||||
ty::fixed => {
|
||||
val inner := this.substitute(first, from, to);
|
||||
val mark := this.scratch.length;
|
||||
for i in 0..this.part_count(id) {
|
||||
val part := this.substitute(this.part_at(id, i as u32), from, to);
|
||||
this.scratch.append(part);
|
||||
}
|
||||
return this.intern_scratch(kind, inner, this.seconds.p[id], mark);
|
||||
}
|
||||
else => {}
|
||||
}
|
||||
|
||||
# A pack parameter stands for the pack it is given, whose types it spreads into
|
||||
# the parts around it, as `Ts` in `def(Ts...): void`.
|
||||
if kind == ty::pack && this.part_count(id) == 0 {
|
||||
for i in 0..from.length {
|
||||
return to[i] if from[i] == first && i < to.length;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
# A pack given for a pack parameter among a function's parameters becomes a
|
||||
# parameter for each of its types, so the function is no longer variadic,
|
||||
# unless the pack is a variadic function's parameters. A named type holds the
|
||||
# pack as the one argument it is, as `Ts` of `box<Ts...>`. The parts are put
|
||||
# on the scratch column past those of the substitutions around this one, each
|
||||
# part's own substitution taking what it put there off again, so they lie
|
||||
# together where interning them reads them.
|
||||
val mark := this.scratch.length;
|
||||
var spread := false;
|
||||
var rest: u32 = 0;
|
||||
for i in 0..this.part_count(id) {
|
||||
val written: u32 = this.part_at(id, i as u32);
|
||||
val part := this.substitute(written, from, to);
|
||||
val known_pack := this.kinds.p[part] == ty::pack && this.firsts.p[part] == 0;
|
||||
if known_pack && this.kinds.p[written] == ty::pack && kind == ty::function {
|
||||
this.scratch.extend(this.parts_view(part));
|
||||
spread = true;
|
||||
rest = this.seconds.p[part];
|
||||
continue;
|
||||
}
|
||||
this.scratch.append(part);
|
||||
}
|
||||
|
||||
val result := kind == ty::function ? this.substitute(first, from, to) : first;
|
||||
val second: u32 = kind == ty::function && spread ? rest : this.seconds.p[id];
|
||||
return this.intern_scratch(kind, result, second, mark);
|
||||
}
|
||||
|
||||
# Interns a type whose parts are those on the scratch column from `mark`, then
|
||||
# takes them off it.
|
||||
def type_table::intern_scratch(&mut this, kind: ty, first: u32, second: u32, mark: u64): u32 {
|
||||
val made := this.intern(kind, first, second, span::<u32>::over(this.scratch.p + mark, this.scratch.length - mark));
|
||||
this.scratch.resize(mark);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether the type is a pack whose types are not yet known: a pack parameter.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_open_pack(&this, id: u32): bool {
|
||||
return this.kinds.p[id] == ty::pack && this.part_count(id) == 0 && this.firsts.p[id] != 0;
|
||||
}
|
||||
|
||||
#! Whether the type is a pack whose types are known: one a pack parameter was
|
||||
#! given, which is written as the types it gives.
|
||||
#! @param this The type table.
|
||||
#! @param id The type's ID.
|
||||
#! @returns Whether it is one.
|
||||
pub def type_table::is_known_pack(&this, id: u32): bool {
|
||||
return this.kinds.p[id] == ty::pack && !this.is_open_pack(id);
|
||||
}
|
||||
Reference in new issue
Block a user