Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Big API Module Migration #268

Merged
merged 12 commits into from
Oct 4, 2023
Prev Previous commit
Next Next commit
API: Move semantic generics into marker_api::sem::generic
  • Loading branch information
xFrednet committed Oct 4, 2023
commit dbbe7fda18b7f2572a81c53719f4613469920d5c
146 changes: 141 additions & 5 deletions marker_api/src/ast/generic.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::fmt::Debug;
use std::marker::PhantomData;

mod syn;
pub use syn::*;
mod sem;
pub use sem::*;
mod args;
pub use args::*;
mod param;
pub use param::*;

use crate::{
ast::{GenericId, SpanId, SymbolId},
context::with_cx,
ffi::FfiOption,
ffi::{FfiOption, FfiSlice},
span::Span,
};

Expand Down Expand Up @@ -97,3 +97,139 @@ impl<'ast> Lifetime<'ast> {
}
}
}

/// The syntactic representation of generic arguments for an item or path.
///
/// ```
/// # use std::fmt::Debug;
/// // vv This is a generic argument
/// generic_item::<u8>(32);
///
/// pub fn generic_item<T: Copy>(t: T)
/// // ^^^^^^^ This is a generic parameter
/// where
/// T: Debug,
/// // ^^^^^^^^ This is a bound for a generic parameter
/// {
/// println!("{:#?}", t);
/// }
/// ```
///
/// See:
/// * [`SynGenericParams`]
#[repr(C)]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(Clone))]
pub struct SynGenericArgs<'ast> {
args: FfiSlice<'ast, SynGenericArgKind<'ast>>,
}

impl<'ast> SynGenericArgs<'ast> {
pub fn args(&self) -> &'ast [SynGenericArgKind<'ast>] {
self.args.get()
}

pub fn is_empty(&self) -> bool {
self.args.is_empty()
}
}

#[cfg(feature = "driver-api")]
impl<'ast> SynGenericArgs<'ast> {
pub fn new(args: &'ast [SynGenericArgKind<'ast>]) -> Self {
Self { args: args.into() }
}
}

/// A singular generic argument.
///
/// See: <https://doc.rust-lang.org/stable/reference/paths.html>
#[repr(C)]
#[non_exhaustive]
#[derive(Debug)]
#[cfg_attr(feature = "driver-api", derive(Clone))]
pub enum SynGenericArgKind<'ast> {
/// A lifetime as a generic argument, like this:
///
/// ```
/// # use std::marker::PhantomData;
/// # #[derive(Default)]
/// # pub struct HasLifetime<'a> {
/// # _data: PhantomData<&'a ()>,
/// # }
/// let _foo: HasLifetime<'static> = HasLifetime::default();
/// // ^^^^^^^
/// ```
Lifetime(&'ast SynLifetimeArg<'ast>),
/// A type as a generic argument, like this:
///
/// ```
/// let _bar: Vec<String> = vec![];
/// // ^^^^^^
/// ```
Ty(&'ast SynTyArg<'ast>),
/// A type binding as a generic argument, like this:
///
/// ```ignore
/// let _baz: &dyn Iterator<Item=String> = todo!();
/// // ^^^^^^^^^^^
/// ```
Binding(&'ast SynBindingArg<'ast>),
/// A constant expression as a generic argument, like this:
///
/// ```ignore
/// # struct Vec<const N: usize> {
/// # data: [f32; N],
/// # }
/// #
/// let _bat: Vec<3> = todo!();
/// // ^
/// ```
Const(&'ast SynConstArg<'ast>),
}

/// This represents the generic parameters of a generic item. The bounds applied
/// to the parameters in the declaration are stored as clauses in this struct.
///
/// ```
/// # use std::fmt::Debug;
/// pub fn generic_item<T: Copy>(t: T)
/// // ^^^^^^^ This is a generic parameter
/// where
/// T: Debug,
/// // ^^^^^^^^ This is a bound for a generic parameter
/// {
/// println!("{:#?}", t);
/// }
///
/// // vv This is a generic argument
/// generic_item::<u8>(32);
/// ```
/// See
/// * [`SynGenericArgs`]
#[repr(C)]
#[derive(Debug)]
pub struct SynGenericParams<'ast> {
params: FfiSlice<'ast, SynGenericParamKind<'ast>>,
clauses: FfiSlice<'ast, SynWhereClauseKind<'ast>>,
}

impl<'ast> SynGenericParams<'ast> {
pub fn params(&self) -> &'ast [SynGenericParamKind<'ast>] {
self.params.get()
}

pub fn clauses(&self) -> &'ast [SynWhereClauseKind<'ast>] {
self.clauses.get()
}
}

#[cfg(feature = "driver-api")]
impl<'ast> SynGenericParams<'ast> {
pub fn new(params: &'ast [SynGenericParamKind<'ast>], clauses: &'ast [SynWhereClauseKind<'ast>]) -> Self {
Self {
params: params.into(),
clauses: clauses.into(),
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
span::Span,
};

use super::super::Lifetime;
use super::Lifetime;

/// The syntactic representation of a generic argument, like this:
///
Expand Down
142 changes: 0 additions & 142 deletions marker_api/src/ast/generic/syn.rs

This file was deleted.

1 change: 1 addition & 0 deletions marker_api/src/sem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
mod common;
pub use common::*;

pub mod generic;
pub mod ty;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod args;
pub use args::*;
mod param;
pub use args::*;
pub use param::*;

use crate::{ffi::FfiSlice, sem::ty::SemTyKind};
Expand All @@ -23,7 +23,7 @@ use crate::{ffi::FfiSlice, sem::ty::SemTyKind};
/// ```
///
/// See:
/// * [`SynGenericParams`][super::SynGenericParams]
/// * [`SynGenericParams`][crate::ast::generic::SynGenericParams]
#[repr(C)]
#[derive(Debug)]
pub struct SemGenericArgs<'ast> {
Expand Down
2 changes: 1 addition & 1 deletion marker_api/src/sem/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub enum SemTyKind<'ast> {
// ================================
// User defined types
// ================================
/// A user defined data type, identified by an [`TyDefId`](marker_api::ast::TyDefId)
/// A user defined data type, identified by an [`TyDefId`](crate::ast::TyDefId)
Adt(&'ast SemAdtTy<'ast>),
/// A generic type defined by a generic parameter
Generic(&'ast SemGenericTy<'ast>),
Expand Down
5 changes: 4 additions & 1 deletion marker_api/src/sem/ty/fn_ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::ast::{generic::SemGenericArgs, ItemId, TyDefId};
use crate::{
ast::{ItemId, TyDefId},
sem::generic::SemGenericArgs,
};

/// A [function item type](https://doc.rust-lang.org/reference/types/function-item.html)
/// identifying a specific function and potentualy additional generics.
Expand Down
2 changes: 1 addition & 1 deletion marker_api/src/sem/ty/trait_ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ast::generic::SemTraitBound, ffi::FfiSlice};
use crate::{ffi::FfiSlice, sem::generic::SemTraitBound};

/// The semantic representation of a [trait object].
///
Expand Down
5 changes: 4 additions & 1 deletion marker_api/src/sem/ty/user_ty.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::marker::PhantomData;

use crate::ast::{generic::SemGenericArgs, GenericId, ItemId, TyDefId};
use crate::{
ast::{GenericId, ItemId, TyDefId},
sem::generic::SemGenericArgs,
};

/// The semantic representation of an abstract data type. This can be an
/// [`Enum`], [`Struct`], or [`Union`].
Expand Down
12 changes: 7 additions & 5 deletions marker_rustc_driver/src/conversion/marker/generics.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use marker_api::{
ast::{
generic::{
Lifetime, LifetimeKind, SemBindingArg, SemConstArg, SemGenericArgKind, SemGenericArgs, SemTraitBound,
SynBindingArg, SynConstArg, SynConstParam, SynGenericArgKind, SynGenericArgs, SynGenericParamKind,
SynGenericParams, SynLifetimeArg, SynLifetimeClause, SynLifetimeParam, SynTraitBound, SynTyArg,
SynTyClause, SynTyParam, SynTyParamBound, SynWhereClauseKind,
Lifetime, LifetimeKind, SynBindingArg, SynConstArg, SynConstParam, SynGenericArgKind, SynGenericArgs,
SynGenericParamKind, SynGenericParams, SynLifetimeArg, SynLifetimeClause, SynLifetimeParam, SynTraitBound,
SynTyArg, SynTyClause, SynTyParam, SynTyParamBound, SynWhereClauseKind,
},
TraitRef,
},
sem::ConstValue,
sem::{
generic::{SemBindingArg, SemConstArg, SemGenericArgKind, SemGenericArgs, SemTraitBound},
ConstValue,
},
};
use rustc_hir as hir;
use rustc_middle as mid;
Expand Down