Skip to content

Commit

Permalink
API: Add impl From<*> for sem::TyKind and correct some names of s…
Browse files Browse the repository at this point in the history
…emantic types (#322)

* API: Add `sem::CommonTyData`

* API: Rename `sem::TyKind::FnTy` -> `sem::TyKind::Fn`

* Doc: Track changes

* PR Review 1

* PR Review 2
  • Loading branch information
xFrednet authored Nov 25, 2023
1 parent 3560c23 commit e1dfde8
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 237 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[Unreleased]: https://github.com/rust-marker/marker/compare/v0.4.0...HEAD
[Unreleased]: https://github.com/rust-marker/marker/compare/v0.4.2...HEAD
[0.4.2]: https://github.com/rust-marker/marker/releases/tag/v0.4.2
[0.4.1]: https://github.com/rust-marker/marker/releases/tag/v0.4.1
[0.4.0]: https://github.com/rust-marker/marker/releases/tag/v0.4.0
[0.3.0]: https://github.com/rust-marker/marker/releases/tag/v0.3.0
[0.2.1]: https://github.com/rust-marker/marker/releases/tag/v0.2.1
Expand All @@ -24,10 +26,24 @@ The following components are considered to be internal and they are excluded fro

## [Unreleased]

[#322]: https://github.com/rust-marker/marker/pull/322

### Added

- [#322]: `sem::TyKind` now implements `From<*>` for all semantic types.

### Breaking Changes

- [#322]: Renamed `sem::TyKind::FnTy` -> `sem::TyKind::Fn`
- [#322]: Renamed `sem::TyKind::ClosureTy` -> `sem::TyKind::Closure`
- [#322]: Renamed `sem::ClosureTy::closure_ty_id` -> `sem::ClosureTy::def_id`

## [0.4.2] - 2023-11-25

[#320]: https://github.com/rust-marker/marker/pull/320

### Fixed

- [#320]: Disable LTO on release builds to fix the crash on Windows with `exit code: 0xc0000005, STATUS_ACCESS_VIOLATION`

## [0.4.1] - 2023-11-24
Expand Down
9 changes: 9 additions & 0 deletions marker_api/src/common/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ new_id! {
pub(crate) SymbolId: u32
}

new_id! {
/// **Unstable**
///
/// This id is used by the driver to lint the semantic type representation, back to the
/// driver type representation, if needed.
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
pub(crate) DriverTyId: u64
}

new_id! {
/// This ID uniquely identifies a statement during linting.
pub StmtId: u64
Expand Down
48 changes: 46 additions & 2 deletions marker_api/src/sem/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod ptr_ty;
mod sequence_ty;
mod trait_ty;
mod user_ty;

pub use fn_ty::*;
pub use other_ty::*;
pub use prim_ty::*;
Expand All @@ -13,6 +14,9 @@ pub use sequence_ty::*;
pub use trait_ty::*;
pub use user_ty::*;

use crate::common::DriverTyId;
use std::{fmt::Debug, marker::PhantomData};

/// The semantic representation of a type.
#[repr(C)]
#[non_exhaustive]
Expand Down Expand Up @@ -43,10 +47,10 @@ pub enum TyKind<'ast> {
// ================================
/// A [function item type](https://doc.rust-lang.org/reference/types/function-item.html)
/// identifying a specific function and potentualy additional generics.
FnTy(&'ast FnTy<'ast>),
Fn(&'ast FnTy<'ast>),
/// The semantic representation of a
/// [closure type](https://doc.rust-lang.org/reference/types/closure.html).
ClosureTy(&'ast ClosureTy<'ast>),
Closure(&'ast ClosureTy<'ast>),
// ================================
// Pointer types
// ================================
Expand Down Expand Up @@ -110,3 +114,43 @@ impl<'ast> TyKind<'ast> {
ty
}
}

#[repr(C)]
#[cfg_attr(feature = "driver-api", visibility::make(pub))]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub(crate) struct CommonTyData<'ast> {
#[cfg_attr(feature = "driver-api", builder(default))]
_lifetime: PhantomData<&'ast ()>,
driver_id: DriverTyId,
}

impl<'ast> Debug for CommonTyData<'ast> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommonTyData {...}").finish()
}
}

#[cfg(feature = "driver-api")]
impl<'ast> CommonTyData<'ast> {
pub fn driver_id(&self) -> DriverTyId {
self.driver_id
}
}

macro_rules! impl_ty_data {
($self_ty:ty, $enum_name:ident) => {
#[cfg(feature = "driver_api")]
impl<'ast> $self_ty {
pub fn data(&self) -> &$crate::sem::ty::CommonTyData<'ast> {
&self.data
}
}

impl<'ast> From<&'ast $self_ty> for $crate::sem::ty::TyKind<'ast> {
fn from(from: &'ast $self_ty) -> Self {
$crate::sem::ty::TyKind::$enum_name(from)
}
}
};
}
use impl_ty_data;
33 changes: 13 additions & 20 deletions marker_api/src/sem/ty/fn_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use crate::{
sem::generic::GenericArgs,
};

use super::CommonTyData;

/// A [function item type](https://doc.rust-lang.org/reference/types/function-item.html)
/// identifying a specific function and potentualy additional generics.
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct FnTy<'ast> {
data: CommonTyData<'ast>,
fn_id: ItemId,
generics: GenericArgs<'ast>,
}
Expand All @@ -24,12 +28,7 @@ impl<'ast> FnTy<'ast> {
}
}

#[cfg(feature = "driver-api")]
impl<'ast> FnTy<'ast> {
pub fn new(fn_id: ItemId, generics: GenericArgs<'ast>) -> Self {
Self { fn_id, generics }
}
}
super::impl_ty_data!(FnTy<'ast>, Fn);

/// The semantic representation of a
/// [closure type](https://doc.rust-lang.org/reference/types/closure.html).
Expand All @@ -38,18 +37,20 @@ impl<'ast> FnTy<'ast> {
/// closure. This type on it's own therefore only identifies the type of the closure.
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct ClosureTy<'ast> {
closure_ty_id: TyDefId,
data: CommonTyData<'ast>,
def_id: TyDefId,
generics: GenericArgs<'ast>,
}

impl<'ast> ClosureTy<'ast> {
/// This returns the [`ItemId`] of the identified function.
pub fn closure_ty_id(&self) -> TyDefId {
self.closure_ty_id
/// This returns the [`ItemId`] of the struct that was generated for this closure.
pub fn def_id(&self) -> TyDefId {
self.def_id
}

/// This returns the [`GenericArgs`] used by identified function
/// This returns the [`GenericArgs`] used by closure.
pub fn generics(&self) -> &GenericArgs<'ast> {
&self.generics
}
Expand All @@ -58,12 +59,4 @@ impl<'ast> ClosureTy<'ast> {
// parameters and return type.
}

#[cfg(feature = "driver-api")]
impl<'ast> ClosureTy<'ast> {
pub fn new(closure_ty_id: TyDefId, generics: GenericArgs<'ast>) -> Self {
Self {
closure_ty_id,
generics,
}
}
}
super::impl_ty_data!(ClosureTy<'ast>, Closure);
12 changes: 4 additions & 8 deletions marker_api/src/sem/ty/other_ty.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use std::marker::PhantomData;
use super::CommonTyData;

/// The placeholder type, signalling that the semantic type is still unstable
/// and therefor not represented as part of the API.
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct UnstableTy<'ast> {
_lt: PhantomData<&'ast ()>,
data: CommonTyData<'ast>,
}

#[cfg(feature = "driver-api")]
impl<'ast> UnstableTy<'ast> {
pub fn new() -> Self {
Self { _lt: PhantomData }
}
}
super::impl_ty_data!(UnstableTy<'ast>, Unstable);
56 changes: 17 additions & 39 deletions marker_api/src/sem/ty/prim_ty.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
use std::marker::PhantomData;

use crate::common::{NumKind, TextKind};

use super::CommonTyData;

/// The semantic representation of the [`bool`] type.
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct BoolTy<'ast> {
_lt: PhantomData<&'ast ()>,
}

#[cfg(feature = "driver-api")]
impl<'ast> BoolTy<'ast> {
pub fn new() -> Self {
Self { _lt: PhantomData }
}
data: CommonTyData<'ast>,
}

impl<'ast> std::fmt::Display for BoolTy<'ast> {
Expand All @@ -22,24 +16,17 @@ impl<'ast> std::fmt::Display for BoolTy<'ast> {
}
}

super::impl_ty_data!(BoolTy<'ast>, Bool);

/// The semantic representation of a numeric type like [`u32`], [`i32`], [`f64`].
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct NumTy<'ast> {
_ast: PhantomData<&'ast ()>,
data: CommonTyData<'ast>,
numeric_kind: NumKind,
}

#[cfg(feature = "driver-api")]
impl<'ast> NumTy<'ast> {
pub fn new(numeric_kind: NumKind) -> Self {
Self {
_ast: PhantomData,
numeric_kind,
}
}
}

impl<'ast> NumTy<'ast> {
pub fn numeric_kind(&self) -> NumKind {
self.numeric_kind
Expand All @@ -62,6 +49,8 @@ impl<'ast> NumTy<'ast> {
}
}

super::impl_ty_data!(NumTy<'ast>, Num);

impl<'ast> std::fmt::Display for NumTy<'ast> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.numeric_kind)
Expand All @@ -70,21 +59,12 @@ impl<'ast> std::fmt::Display for NumTy<'ast> {

/// The semantic representation of a textual type like [`char`] or [`str`].
#[repr(C)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct TextTy<'ast> {
_ast: PhantomData<&'ast ()>,
data: CommonTyData<'ast>,
textual_kind: TextKind,
}

#[cfg(feature = "driver-api")]
impl<'ast> TextTy<'ast> {
pub fn new(textual_kind: TextKind) -> Self {
Self {
_ast: PhantomData,
textual_kind,
}
}
}

impl<'ast> TextTy<'ast> {
pub fn textual_kind(&self) -> TextKind {
self.textual_kind
Expand All @@ -99,6 +79,8 @@ impl<'ast> TextTy<'ast> {
}
}

super::impl_ty_data!(TextTy<'ast>, Text);

impl<'ast> std::fmt::Debug for TextTy<'ast> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.textual_kind)
Expand All @@ -107,16 +89,12 @@ impl<'ast> std::fmt::Debug for TextTy<'ast> {

/// The semantic representation of the never type [`!`](prim@never).
#[repr(C)]
#[cfg_attr(feature = "driver-api", derive(typed_builder::TypedBuilder))]
pub struct NeverTy<'ast> {
_lt: PhantomData<&'ast ()>,
data: CommonTyData<'ast>,
}

#[cfg(feature = "driver-api")]
impl<'ast> NeverTy<'ast> {
pub fn new() -> Self {
Self { _lt: PhantomData }
}
}
super::impl_ty_data!(NeverTy<'ast>, Never);

impl<'ast> std::fmt::Debug for NeverTy<'ast> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
Loading

0 comments on commit e1dfde8

Please sign in to comment.