| 1 |
use super::lifetimes::{Lifetimes, LinkedLifetimes}; |
| 2 |
use super::{ |
| 3 |
Borrow, EnumDef, EnumId, Everywhere, OpaqueDef, OpaqueId, OpaqueOwner, OutStructDef, |
| 4 |
OutputOnly, ReturnableStructDef, StructDef, TyPosition, TypeContext, |
| 5 |
}; |
| 6 |
|
| 7 |
/// Path to a struct that may appear as an output. |
| 8 |
#[derive(Debug, Clone)] |
| 9 |
#[non_exhaustive] |
| 10 |
pub enum ReturnableStructPath { |
| 11 |
Struct(StructPath), |
| 12 |
OutStruct(OutStructPath), |
| 13 |
} |
| 14 |
|
| 15 |
/// Path to a struct that can only be used as an output. |
| 16 |
pub type OutStructPath = StructPath<OutputOnly>; |
| 17 |
|
| 18 |
/// Path to a struct that can be used in inputs and outputs. |
| 19 |
#[derive(Debug, Clone)] |
| 20 |
#[non_exhaustive] |
| 21 |
pub struct StructPath<P: TyPosition = Everywhere> { |
| 22 |
pub lifetimes: Lifetimes, |
| 23 |
pub tcx_id: P::StructId, |
| 24 |
} |
| 25 |
|
| 26 |
/// Path to an opaque. |
| 27 |
/// |
| 28 |
/// There are three kinds of opaques that Diplomat uses, so this type has two |
| 29 |
/// generic arguments to differentiate between the three, while still showing |
| 30 |
/// that the three are all paths to opaques. The monomorphized versions that |
| 31 |
/// Diplomat uses are: |
| 32 |
/// |
| 33 |
/// 1. `OpaquePath<Optional, MaybeOwn>`: Opaques in return types, |
| 34 |
/// which can be optional and either owned or borrowed. |
| 35 |
/// 2. `OpaquePath<Optional, Borrow>`: Opaques in method parameters, which can |
| 36 |
/// be optional but must be borrowed, since most languages don't have a way to |
| 37 |
/// entirely give up ownership of a value. |
| 38 |
/// 3. `OpaquePath<NonOptional, Borrow>`: Opaques in the `&self` position, which |
| 39 |
/// cannot be optional and must be borrowed for the same reason as above. |
| 40 |
#[derive(Debug, Clone)] |
| 41 |
#[non_exhaustive] |
| 42 |
pub struct OpaquePath<Opt, Owner> { |
| 43 |
pub lifetimes: Lifetimes, |
| 44 |
pub optional: Opt, |
| 45 |
pub owner: Owner, |
| 46 |
pub tcx_id: OpaqueId, |
| 47 |
} |
| 48 |
|
| 49 |
#[derive(Debug, Copy, Clone)] |
| 50 |
pub struct Optional(pub(super) bool); |
| 51 |
|
| 52 |
#[derive(Debug, Copy, Clone)] |
| 53 |
#[allow(clippy::exhaustive_structs)] // marker type |
| 54 |
pub struct NonOptional; |
| 55 |
|
| 56 |
impl<Owner: OpaqueOwner> OpaquePath<Optional, Owner> { |
| 57 |
pub fn is_optional(&self) -> bool { |
| 58 |
self.optional.0 |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
impl<Owner: OpaqueOwner> OpaquePath<NonOptional, Owner> { |
| 63 |
pub fn wrap_optional(self) -> OpaquePath<Optional, Owner> { |
| 64 |
OpaquePath { |
| 65 |
lifetimes: self.lifetimes, |
| 66 |
optional: Optional(false), |
| 67 |
owner: self.owner, |
| 68 |
tcx_id: self.tcx_id, |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
impl<Opt> OpaquePath<Opt, MaybeOwn> { |
| 74 |
pub fn as_borrowed(&self) -> Option<&Borrow> { |
| 75 |
self.owner.as_borrowed() |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl<Opt> OpaquePath<Opt, Borrow> { |
| 80 |
pub fn borrowed(&self) -> &Borrow { |
| 81 |
&self.owner |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/// Path to an enum. |
| 86 |
#[derive(Debug, Clone)] |
| 87 |
#[non_exhaustive] |
| 88 |
pub struct EnumPath { |
| 89 |
pub tcx_id: EnumId, |
| 90 |
} |
| 91 |
|
| 92 |
/// Determine whether a pointer to an opaque type is owned or borrowed. |
| 93 |
/// |
| 94 |
/// Since owned opaques cannot be used as inputs, this only appears in output types. |
| 95 |
#[derive(Copy, Clone, Debug)] |
| 96 |
#[allow(clippy::exhaustive_enums)] // only two answers to this question |
| 97 |
pub enum MaybeOwn { |
| 98 |
Own, |
| 99 |
Borrow(Borrow), |
| 100 |
} |
| 101 |
|
| 102 |
impl MaybeOwn { |
| 103 |
pub fn as_borrowed(&self) -> Option<&Borrow> { |
| 104 |
match self { |
| 105 |
MaybeOwn::Own => None, |
| 106 |
MaybeOwn::Borrow(borrow) => Some(borrow), |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
impl ReturnableStructPath { |
| 112 |
pub fn resolve<'tcx>(&self, tcx: &'tcx TypeContext) -> ReturnableStructDef<'tcx> { |
| 113 |
match self { |
| 114 |
ReturnableStructPath::Struct(path) => ReturnableStructDef::Struct(path.resolve(tcx)), |
| 115 |
ReturnableStructPath::OutStruct(path) => { |
| 116 |
ReturnableStructDef::OutStruct(path.resolve(tcx)) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
pub(crate) fn lifetimes(&self) -> &Lifetimes { |
| 122 |
match self { |
| 123 |
Self::Struct(p) => &p.lifetimes, |
| 124 |
Self::OutStruct(p) => &p.lifetimes, |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
impl<P: TyPosition> StructPath<P> { |
| 130 |
/// Returns a new [`EnumPath`]. |
| 131 |
pub(super) fn new(lifetimes: Lifetimes, tcx_id: P::StructId) -> Self { |
| 132 |
Self { lifetimes, tcx_id } |
| 133 |
} |
| 134 |
} |
| 135 |
impl StructPath { |
| 136 |
/// Returns the [`StructDef`] that this path references. |
| 137 |
pub fn resolve<'tcx>(&self, tcx: &'tcx TypeContext) -> &'tcx StructDef { |
| 138 |
tcx.resolve_struct(self.tcx_id) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
impl OutStructPath { |
| 143 |
/// Returns the [`OutStructDef`] that this path references. |
| 144 |
pub fn resolve<'tcx>(&self, tcx: &'tcx TypeContext) -> &'tcx OutStructDef { |
| 145 |
tcx.resolve_out_struct(self.tcx_id) |
| 146 |
} |
| 147 |
|
| 148 |
/// Get a map of lifetimes used on this path to lifetimes as named in the def site. See [`LinkedLifetimes`] |
| 149 |
/// for more information. |
| 150 |
pub fn link_lifetimes<'def, 'tcx>( |
| 151 |
&'def self, |
| 152 |
tcx: &'tcx TypeContext, |
| 153 |
) -> LinkedLifetimes<'def, 'tcx> { |
| 154 |
let struc = self.resolve(tcx); |
| 155 |
let env = &struc.lifetimes; |
| 156 |
LinkedLifetimes::new(env, None, &self.lifetimes) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl<Opt, Owner> OpaquePath<Opt, Owner> { |
| 161 |
/// Returns a new [`EnumPath`]. |
| 162 |
pub(super) fn new(lifetimes: Lifetimes, optional: Opt, owner: Owner, tcx_id: OpaqueId) -> Self { |
| 163 |
Self { |
| 164 |
lifetimes, |
| 165 |
optional, |
| 166 |
owner, |
| 167 |
tcx_id, |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/// Returns the [`OpaqueDef`] that this path references. |
| 172 |
pub fn resolve<'tcx>(&self, tcx: &'tcx TypeContext) -> &'tcx OpaqueDef { |
| 173 |
tcx.resolve_opaque(self.tcx_id) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
impl<Opt, Owner: OpaqueOwner> OpaquePath<Opt, Owner> { |
| 178 |
/// Get a map of lifetimes used on this path to lifetimes as named in the def site. See [`LinkedLifetimes`] |
| 179 |
/// for more information. |
| 180 |
pub fn link_lifetimes<'def, 'tcx>( |
| 181 |
&'def self, |
| 182 |
tcx: &'tcx TypeContext, |
| 183 |
) -> LinkedLifetimes<'def, 'tcx> { |
| 184 |
let opaque = self.resolve(tcx); |
| 185 |
let env = &opaque.lifetimes; |
| 186 |
LinkedLifetimes::new(env, self.owner.lifetime(), &self.lifetimes) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
impl EnumPath { |
| 191 |
/// Returns a new [`EnumPath`]. |
| 192 |
pub(super) fn new(tcx_id: EnumId) -> Self { |
| 193 |
Self { tcx_id } |
| 194 |
} |
| 195 |
|
| 196 |
/// Returns the [`EnumDef`] that this path references. |
| 197 |
pub fn resolve<'tcx>(&self, tcx: &'tcx TypeContext) -> &'tcx EnumDef { |
| 198 |
tcx.resolve_enum(self.tcx_id) |
| 199 |
} |
| 200 |
} |