charon_lib/ast/types/vars.rs
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
//! Type-level variables. There are 4 kinds of variables at the type-level: regions, types, const
//! generics and trait clauses. The relevant definitions are in this module.
use derive_generic_visitor::{Drive, DriveMut};
use serde::{Deserialize, Serialize};
use crate::ast::*;
/// The index of a binder, counting from the innermost. See [`DeBruijnVar`] for details.
#[derive(
Debug,
PartialEq,
Eq,
Copy,
Clone,
Hash,
PartialOrd,
Ord,
Serialize,
Deserialize,
Drive,
DriveMut,
)]
#[serde(transparent)]
#[drive(skip)]
pub struct DeBruijnId {
pub index: usize,
}
/// Type-level variable.
///
/// Variables are bound in groups. Each item has a top-level binding group in its `generic_params`
/// field, and then inner binders are possible using the `RegionBinder<T>` and `Binder<T>` types.
/// Each variable is linked to exactly one binder. The `Id` then identifies the specific variable
/// among all those bound in that group.
///
/// For instance, we have the following:
/// ```text
/// fn f<'a, 'b>(x: for<'c> fn(&'b u8, &'c u16, for<'d> fn(&'b u32, &'c u64, &'d u128)) -> u64) {}
/// ^^^^^^ ^^ ^ ^ ^^ ^ ^ ^
/// | inner binder | | inner binder | | |
/// top-level binder | | | | |
/// Bound(1, b) | Bound(2, b) | Bound(0, d)
/// | |
/// Bound(0, c) Bound(1, c)
/// ```
///
/// To make consumption easier for projects that don't do heavy substitution, a micro-pass at the
/// end changes the variables bound at the top-level (i.e. in the `GenericParams` of items) to be
/// `Free`. This is an optional pass, we may add a flag to deactivate it. The example above
/// becomes:
/// ```text
/// fn f<'a, 'b>(x: for<'c> fn(&'b u8, &'c u16, for<'d> fn(&'b u32, &'c u64, &'d u128)) -> u64) {}
/// ^^^^^^ ^^ ^ ^ ^^ ^ ^ ^
/// | inner binder | | inner binder | | |
/// top-level binder | | | | |
/// Free(b) | Free(b) | Bound(0, d)
/// | |
/// Bound(0, c) Bound(1, c)
/// ```
///
/// At the moment only region variables can be bound in a non-top-level binder.
#[derive(
Debug,
PartialEq,
Eq,
Copy,
Clone,
Hash,
PartialOrd,
Ord,
Serialize,
Deserialize,
Drive,
DriveMut,
)]
pub enum DeBruijnVar<Id> {
/// A variable attached to the nth binder, counting from the innermost.
Bound(DeBruijnId, Id),
/// A variable attached to the outermost binder (the one on the item). As explained above, This
/// is not used in charon internals, only as a micro-pass before exporting the crate data.
Free(Id),
}
// We need to manipulate a lot of indices for the types, variables, definitions, etc. In order not
// to confuse them, we define an index type for every one of them (which is just a struct with a
// unique usize field), together with some utilities like a fresh index generator, using the
// `generate_index_type` macro.
generate_index_type!(RegionId, "Region");
generate_index_type!(TypeVarId, "T");
generate_index_type!(ConstGenericVarId, "Const");
generate_index_type!(TraitClauseId, "TraitClause");
/// A type variable in a signature or binder.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Drive, DriveMut)]
pub struct TypeVar {
/// Index identifying the variable among other variables bound at the same level.
pub index: TypeVarId,
/// Variable name
#[drive(skip)]
pub name: String,
}
/// A region variable in a signature or binder.
#[derive(
Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Drive, DriveMut,
)]
pub struct RegionVar {
/// Index identifying the variable among other variables bound at the same level.
pub index: RegionId,
/// Region name
#[drive(skip)]
pub name: Option<String>,
}
/// A const generic variable in a signature or binder.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Drive, DriveMut)]
pub struct ConstGenericVar {
/// Index identifying the variable among other variables bound at the same level.
pub index: ConstGenericVarId,
/// Const generic name
#[drive(skip)]
pub name: String,
/// Type of the const generic
pub ty: LiteralTy,
}
/// A trait predicate in a signature, of the form `Type: Trait<Args>`. This functions like a
/// variable binder, to which variables of the form `TraitRefKind::Clause` can refer to.
#[derive(Debug, Clone, Hash, Serialize, Deserialize, Drive, DriveMut)]
pub struct TraitClause {
/// Index identifying the clause among other clauses bound at the same level.
pub clause_id: TraitClauseId,
// TODO: does not need to be an option.
pub span: Option<Span>,
/// Where the predicate was written, relative to the item that requires it.
#[charon::opaque]
#[drive(skip)]
pub origin: PredicateOrigin,
/// The trait that is implemented.
#[charon::rename("trait")]
pub trait_: PolyTraitDeclRef,
}
pub type RegionDbVar = DeBruijnVar<RegionId>;
pub type TypeDbVar = DeBruijnVar<TypeVarId>;
pub type ConstGenericDbVar = DeBruijnVar<ConstGenericVarId>;
pub type ClauseDbVar = DeBruijnVar<TraitClauseId>;
impl DeBruijnId {
pub fn zero() -> Self {
DeBruijnId { index: 0 }
}
pub fn one() -> Self {
DeBruijnId { index: 1 }
}
pub fn new(index: usize) -> Self {
DeBruijnId { index }
}
pub fn is_zero(&self) -> bool {
self.index == 0
}
pub fn incr(&self) -> Self {
DeBruijnId {
index: self.index + 1,
}
}
pub fn decr(&self) -> Self {
DeBruijnId {
index: self.index - 1,
}
}
pub fn plus(&self, delta: Self) -> Self {
DeBruijnId {
index: self.index + delta.index,
}
}
pub fn sub(&self, delta: Self) -> Option<Self> {
Some(DeBruijnId {
index: self.index.checked_sub(delta.index)?,
})
}
}
impl<Id> DeBruijnVar<Id>
where
Id: Copy,
{
pub fn new_at_zero(id: Id) -> Self {
DeBruijnVar::Bound(DeBruijnId::new(0), id)
}
pub fn free(id: Id) -> Self {
DeBruijnVar::Free(id)
}
pub fn bound(index: DeBruijnId, id: Id) -> Self {
DeBruijnVar::Bound(index, id)
}
pub fn incr(&self) -> Self {
match *self {
DeBruijnVar::Bound(dbid, varid) => DeBruijnVar::Bound(dbid.incr(), varid),
DeBruijnVar::Free(varid) => DeBruijnVar::Free(varid),
}
}
pub fn decr(&self) -> Self {
match *self {
DeBruijnVar::Bound(dbid, varid) => DeBruijnVar::Bound(dbid.decr(), varid),
DeBruijnVar::Free(varid) => DeBruijnVar::Free(varid),
}
}
/// Returns the variable id if it is bound as the given depth.
pub fn bound_at_depth(&self, depth: DeBruijnId) -> Option<Id> {
match *self {
DeBruijnVar::Bound(dbid, varid) if dbid == depth => Some(varid),
_ => None,
}
}
/// Move the variable out of `depth` binders. Returns `None` if the variable is bound in one of
/// these `depth` binders.
pub fn move_out_from_depth(&self, depth: DeBruijnId) -> Option<Self> {
Some(match *self {
DeBruijnVar::Bound(dbid, varid) => DeBruijnVar::Bound(dbid.sub(depth)?, varid),
DeBruijnVar::Free(_) => *self,
})
}
/// Move under `depth` binders.
pub fn move_under_binders(&self, depth: DeBruijnId) -> Self {
match *self {
DeBruijnVar::Bound(dbid, varid) => DeBruijnVar::Bound(dbid.plus(depth), varid),
DeBruijnVar::Free(_) => *self,
}
}
}
impl TypeVar {
pub fn new(index: TypeVarId, name: String) -> TypeVar {
TypeVar { index, name }
}
}
impl Default for DeBruijnId {
fn default() -> Self {
Self::zero()
}
}