WIP adding const generics
Some checks failed
/ test (push) Failing after 1m3s

This commit is contained in:
Jacob Lifshay 2024-09-16 16:47:10 -07:00
parent 5835b995a9
commit 76ea7f82c3
Signed by: programmerjake
SSH key fingerprint: SHA256:B1iRVvUJkvd7upMIiMqn6OyxvD2SgJkAH3ZnUOj6z+c

View file

@ -5,7 +5,7 @@ use std::{collections::HashMap, fmt, mem};
use syn::{ use syn::{
parse::{Parse, ParseStream}, parse::{Parse, ParseStream},
parse_quote, parse_quote_spanned, parse_quote, parse_quote_spanned,
punctuated::Punctuated, punctuated::{Pair, Punctuated},
spanned::Spanned, spanned::Spanned,
token::{Brace, Bracket, Paren}, token::{Brace, Bracket, Paren},
AngleBracketedGenericArguments, Attribute, ConstParam, Expr, ExprIndex, ExprPath, ExprTuple, AngleBracketedGenericArguments, Attribute, ConstParam, Expr, ExprIndex, ExprPath, ExprTuple,
@ -239,7 +239,34 @@ impl ParseTypes<GenericArgument> for ParsedGenericArgument {
parser: &mut TypesParser<'_>, parser: &mut TypesParser<'_>,
) -> Result<Self, ParseFailed> { ) -> Result<Self, ParseFailed> {
match arg { match arg {
GenericArgument::Type(ty) => Ok(Self::Type(parser.parse(ty)?)), GenericArgument::Type(ty) => {
{
let mut ty = &*ty;
while let Type::Group(TypeGroup { elem, .. }) = ty {
ty = &**elem;
}
if let Type::Path(TypePath { qself: None, path }) = ty {
if let Some(ident) = path.get_ident() {
if let Some(&param_index) =
parser.generics.param_name_to_index_map.get(ident)
{
match parser.generics.params[param_index] {
ParsedGenericParam::Type(_)
| ParsedGenericParam::SizeType(_) => {}
ParsedGenericParam::Const(_) => {
return Ok(Self::Const(Expr::Path(ExprPath {
attrs: vec![],
qself: None,
path: path.clone(),
})));
}
}
}
}
}
}
Ok(Self::Type(parser.parse(ty)?))
}
GenericArgument::Const(expr) => Ok(Self::Const(expr.clone())), GenericArgument::Const(expr) => Ok(Self::Const(expr.clone())),
_ => parse_failed!(parser, arg, "expected type or const generic argument"), _ => parse_failed!(parser, arg, "expected type or const generic argument"),
} }
@ -1205,6 +1232,13 @@ impl ParseTypes<Path> for ParsedType {
param_index, param_index,
}) })
} }
ParsedGenericParam::Const(ParsedConstParam { ref ident, .. }) => {
parser
.errors
.error(ident, "constant provided when a type was expected");
todo!();
return Err(ParseFailed);
}
}, },
)); ));
} }
@ -1291,6 +1325,120 @@ impl ParseTypes<Type> for ParsedType {
} }
} }
#[derive(Debug, Clone)]
pub(crate) enum ParsedConstGenericType {
Usize(known_items::usize),
}
impl_fold! {
enum ParsedConstGenericType<> {
Usize(known_items::usize),
}
}
impl From<ParsedConstGenericType> for Type {
fn from(value: ParsedConstGenericType) -> Self {
match value {
ParsedConstGenericType::Usize(v) => parse_quote! { #v },
}
}
}
impl From<MaybeParsed<ParsedConstGenericType, Type>> for Type {
fn from(value: MaybeParsed<ParsedConstGenericType, Type>) -> Self {
match value {
MaybeParsed::Unrecognized(value) => value,
MaybeParsed::Parsed(value) => value.into(),
}
}
}
impl From<Box<ParsedConstGenericType>> for Type {
fn from(value: Box<ParsedConstGenericType>) -> Self {
(*value).into()
}
}
impl ToTokens for ParsedConstGenericType {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
ParsedConstGenericType::Usize(ty) => ty.to_tokens(tokens),
}
}
}
impl ParseTypes<Path> for ParsedConstGenericType {
fn parse_types(path: &mut Path, parser: &mut TypesParser<'_>) -> Result<Self, ParseFailed> {
let Path {
leading_colon,
ref mut segments,
} = *path;
if segments.is_empty() {
parse_failed!(parser, path, "path must not be empty");
}
let mut args = None;
let segments = Punctuated::from_iter(segments.pairs_mut().map_pair_value_mut(|segment| {
let PathSegment { ident, arguments } = segment;
if let Some(_) = args {
parser
.errors()
.error(&ident, "associated types/consts are not yet implemented");
}
args = match arguments {
PathArguments::None => None,
PathArguments::AngleBracketed(args) => parser.parse(args).ok(),
PathArguments::Parenthesized(_) => {
parser
.errors()
.error(&segment, "function traits are not allowed");
None
}
};
PathSegment::from(segment.ident.clone())
}));
let named = ParsedTypeNamed {
path: Path {
leading_colon,
segments,
},
args,
};
let named = match known_items::usize::try_from_named(named, parser)? {
Ok(v) => return Ok(Self::Usize(v)),
Err(named) => named,
};
parser.errors.error(
named,
"const parameter types other than `usize` are not yet implemented",
);
Err(ParseFailed)
}
}
impl ParseTypes<TypePath> for ParsedConstGenericType {
fn parse_types(ty: &mut TypePath, parser: &mut TypesParser<'_>) -> Result<Self, ParseFailed> {
let TypePath { qself, path } = ty;
if let Some(_qself) = qself {
parse_failed!(
parser,
ty,
"associated types/consts are not yet implemented"
);
} else {
parser.parse(path)
}
}
}
impl ParseTypes<Type> for ParsedConstGenericType {
fn parse_types(ty: &mut Type, parser: &mut TypesParser<'_>) -> Result<Self, ParseFailed> {
Ok(match ty {
Type::Path(ty) => parser.parse(ty)?,
_ => parse_failed!(parser, ty, "unsupported const generic type"),
})
}
}
pub(crate) struct ParseFailed; pub(crate) struct ParseFailed;
impl fmt::Display for ParseFailed { impl fmt::Display for ParseFailed {
@ -1377,6 +1525,14 @@ pub(crate) enum UnparsedGenericParam {
colon_token: Token![:], colon_token: Token![:],
bounds: ParsedBounds, bounds: ParsedBounds,
}, },
Const {
attrs: Vec<Attribute>,
options: HdlAttr<ConstParamOptions>,
const_token: Token![const],
ident: Ident,
colon_token: Token![:],
ty: ParsedConstGenericType,
},
} }
pub(crate) mod known_items { pub(crate) mod known_items {
@ -1390,7 +1546,7 @@ pub(crate) mod known_items {
macro_rules! impl_known_item_body { macro_rules! impl_known_item_body {
($known_item:ident) => { ($known_item:ident) => {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
#[allow(dead_code)] #[allow(dead_code, non_camel_case_types)]
pub(crate) struct $known_item { pub(crate) struct $known_item {
pub(crate) path: Path, pub(crate) path: Path,
pub(crate) span: Span, pub(crate) span: Span,
@ -1509,6 +1665,7 @@ pub(crate) mod known_items {
impl_known_item!(::fayalite::ty::StaticType); impl_known_item!(::fayalite::ty::StaticType);
impl_known_item!(::fayalite::ty::Type); impl_known_item!(::fayalite::ty::Type);
impl_known_item!(::fayalite::util::ConstUsize); impl_known_item!(::fayalite::util::ConstUsize);
impl_known_item!(::fayalite::__std::primitive::usize);
} }
macro_rules! impl_bounds { macro_rules! impl_bounds {
@ -1865,10 +2022,54 @@ impl ToTokens for ParsedSizeTypeParam {
} }
} }
#[derive(Debug, Clone)]
pub(crate) struct ParsedConstParamWhereBounds {
pub(crate) const_usize: known_items::ConstUsize,
pub(crate) lt_token: Token![<],
pub(crate) ident: Ident,
pub(crate) gt_token: Token![>],
pub(crate) colon_token: Token![:],
pub(crate) bounds: ParsedSizeTypeBounds,
}
#[derive(Debug, Clone)]
pub(crate) struct ParsedConstParam {
pub(crate) attrs: Vec<Attribute>,
pub(crate) options: HdlAttr<ConstParamOptions>,
pub(crate) const_token: Token![const],
pub(crate) ident: Ident,
pub(crate) colon_token: Token![:],
pub(crate) ty: ParsedConstGenericType,
pub(crate) bounds: ParsedConstParamWhereBounds,
}
impl ToTokens for ParsedConstParam {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self {
attrs,
options,
const_token,
ident,
colon_token,
ty,
bounds: _,
} = self;
let ConstParamOptions {} = options.body;
for attr in attrs {
attr.to_tokens(tokens);
}
const_token.to_tokens(tokens);
ident.to_tokens(tokens);
colon_token.to_tokens(tokens);
ty.to_tokens(tokens);
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) enum ParsedGenericParam { pub(crate) enum ParsedGenericParam {
Type(ParsedTypeParam), Type(ParsedTypeParam),
SizeType(ParsedSizeTypeParam), SizeType(ParsedSizeTypeParam),
Const(ParsedConstParam),
} }
impl ToTokens for ParsedGenericParam { impl ToTokens for ParsedGenericParam {
@ -1876,6 +2077,7 @@ impl ToTokens for ParsedGenericParam {
match self { match self {
ParsedGenericParam::Type(v) => v.to_tokens(tokens), ParsedGenericParam::Type(v) => v.to_tokens(tokens),
ParsedGenericParam::SizeType(v) => v.to_tokens(tokens), ParsedGenericParam::SizeType(v) => v.to_tokens(tokens),
ParsedGenericParam::Const(v) => v.to_tokens(tokens),
} }
} }
} }
@ -1885,6 +2087,7 @@ impl ParsedGenericParam {
match self { match self {
ParsedGenericParam::Type(v) => &v.ident, ParsedGenericParam::Type(v) => &v.ident,
ParsedGenericParam::SizeType(v) => &v.ident, ParsedGenericParam::SizeType(v) => &v.ident,
ParsedGenericParam::Const(v) => &v.ident,
} }
} }
} }
@ -1911,6 +2114,11 @@ impl ParsedGenerics {
.KnownSize .KnownSize
.get_or_insert_with(|| known_items::KnownSize(ident.span())); .get_or_insert_with(|| known_items::KnownSize(ident.span()));
} }
ParsedGenericParam::Const(ParsedConstParam { ident, bounds, .. }) => {
bounds
.KnownSize
.get_or_insert_with(|| known_items::KnownSize(ident.span()));
}
} }
} }
self self
@ -1982,6 +2190,13 @@ impl ParsedGenerics {
<#ident as ::fayalite::int::Size>::SizeType <#ident as ::fayalite::int::Size>::SizeType
} }
} }
ParsedGenericParam::Const(param) => {
let ident = &param.ident;
parse_quote_spanned! {span=>
<::fayalite::util::ConstUsize<#ident>
as ::fayalite::int::Size>::SizeType
}
}
}) })
.collect(), .collect(),
} }
@ -2254,98 +2469,117 @@ impl ParsedGenerics {
todo!(); todo!();
} }
} }
ParsedGenericParam::Const(_) => todo!(),
}; };
} }
} }
pub(crate) fn parse(generics: &mut Generics) -> syn::Result<Self> { pub(crate) fn parse<'a>(generics: &'a mut Generics) -> syn::Result<Self> {
let Generics { let Generics {
lt_token, lt_token,
params, params: input_params,
gt_token, gt_token,
where_clause, where_clause,
} = generics; } = generics;
let mut errors = Errors::new(); let mut errors = Errors::new();
let mut predicates: Vec<WherePredicate> = Vec::with_capacity(params.len()); let mut predicates: Vec<WherePredicate> = Vec::with_capacity(input_params.len());
let mut defaults = Vec::with_capacity(params.len()); struct LateParsedParam<'a> {
let mut params = Punctuated::<UnparsedGenericParam, _>::from_iter( default: Option<(Token![=], &'a mut Type)>,
params.pairs_mut().filter_map_pair_value_mut(|param| { const_param_type: Option<&'a mut Type>,
let (param, default) = match param { }
GenericParam::Lifetime(param) => { let mut late_parsed_params: Vec<LateParsedParam<'a>> =
errors.unwrap_or_default( Vec::with_capacity(input_params.len());
HdlAttr::<LifetimeParamOptions>::parse_and_take_attr(&mut param.attrs), let mut unparsed_params: Punctuated<UnparsedGenericParam, _> = Punctuated::new();
); for input_param in input_params.pairs_mut() {
errors.error(param, "lifetime generics are not supported by #[hdl]"); let (input_param, punct) = input_param.into_tuple();
return None; let (unparsed_param, late_parsed_param) = match input_param {
GenericParam::Lifetime(param) => {
errors.unwrap_or_default(HdlAttr::<LifetimeParamOptions>::parse_and_take_attr(
&mut param.attrs,
));
errors.error(param, "lifetime generics are not supported by #[hdl]");
continue;
}
GenericParam::Type(TypeParam {
attrs,
ident,
colon_token,
bounds,
eq_token,
default,
}) => {
let span = ident.span();
let options = errors
.unwrap_or_default(HdlAttr::<TypeParamOptions>::parse_and_take_attr(attrs))
.unwrap_or_default();
let colon_token = colon_token.unwrap_or_else(|| Token![:](span));
if !bounds.is_empty() {
predicates.push(WherePredicate::Type(PredicateType {
lifetimes: None,
bounded_ty: parse_quote! { #ident },
colon_token,
bounds: bounds.clone(),
}));
} }
GenericParam::Type(TypeParam { (
attrs, UnparsedGenericParam::Type {
ident, attrs: attrs.clone(),
colon_token, options,
bounds, ident: ident.clone(),
eq_token, colon_token,
default, bounds: ParsedBounds::default(),
}) => { },
let span = ident.span(); LateParsedParam {
let options = errors default: default
.unwrap_or_default(HdlAttr::<TypeParamOptions>::parse_and_take_attr( .as_mut()
attrs,
))
.unwrap_or_default();
let colon_token = colon_token.unwrap_or_else(|| Token![:](span));
if !bounds.is_empty() {
predicates.push(WherePredicate::Type(PredicateType {
lifetimes: None,
bounded_ty: parse_quote! { #ident },
colon_token,
bounds: bounds.clone(),
}));
}
(
UnparsedGenericParam::Type {
attrs: attrs.clone(),
options,
ident: ident.clone(),
colon_token,
bounds: ParsedBounds::default(),
},
default
.clone()
.map(|v| (eq_token.unwrap_or_else(|| Token![=](span)), v)), .map(|v| (eq_token.unwrap_or_else(|| Token![=](span)), v)),
) const_param_type: None,
} },
GenericParam::Const(ConstParam { )
attrs, }
const_token, GenericParam::Const(ConstParam {
ident, attrs,
colon_token, const_token,
ty, ident,
eq_token, colon_token,
default, ty,
}) => { eq_token,
let options = errors default,
.unwrap_or_default(HdlAttr::<ConstParamOptions>::parse_and_take_attr( }) => {
attrs, let options = errors
)) .unwrap_or_default(HdlAttr::<ConstParamOptions>::parse_and_take_attr(attrs))
.unwrap_or_default(); .unwrap_or_default();
let _ = const_token; if let Some(default) = default {
let _ = ident;
let _ = colon_token;
let _ = ty;
let _ = eq_token; let _ = eq_token;
let _ = default; errors.error(
let _ = options; default,
todo!() "const generics' default values are not yet implemented",
);
} }
}; (
defaults.push(default); UnparsedGenericParam::Const {
Some(param) attrs: attrs.clone(),
}), options,
); const_token: *const_token,
let param_name_to_index_map: HashMap<Ident, usize> = params ident: ident.clone(),
colon_token: *colon_token,
ty: ParsedConstGenericType::Usize(known_items::usize(ident.span())),
},
LateParsedParam {
default: None,
const_param_type: Some(ty),
},
)
}
};
late_parsed_params.push(late_parsed_param);
unparsed_params.extend([Pair::new(unparsed_param, punct.cloned())]);
}
let param_name_to_index_map: HashMap<Ident, usize> = unparsed_params
.iter() .iter()
.enumerate() .enumerate()
.map(|(index, param)| { .map(|(index, param)| {
let UnparsedGenericParam::Type { ident, .. } = param; let (UnparsedGenericParam::Type { ident, .. }
| UnparsedGenericParam::Const { ident, .. }) = param;
(ident.clone(), index) (ident.clone(), index)
}) })
.collect(); .collect();
@ -2367,80 +2601,131 @@ impl ParsedGenerics {
errors.error(predicate, "unsupported where predicate kind"); errors.error(predicate, "unsupported where predicate kind");
continue; continue;
}; };
ParsedTypeNamed {
path: todo!(),
args: todo!(),
};
let Some(&index) = bounded_ty let Some(&index) = bounded_ty
.get_ident() .get_ident()
.and_then(|bounded_ty| param_name_to_index_map.get(bounded_ty)) .and_then(|bounded_ty| param_name_to_index_map.get(bounded_ty))
else { else {
errors.error( errors.error(
bounded_ty, bounded_ty,
"where predicate bounded type must be one of the generic parameters", "where predicate bounded type must be one of the generic type \
parameters or `ConstUsize<A_CONST_PARAM>`",
); );
continue; continue;
}; };
let UnparsedGenericParam::Type { let parsed_bounds = match &mut unparsed_params[index] {
bounds: parsed_bounds, UnparsedGenericParam::Type { bounds, .. } => bounds,
.. UnparsedGenericParam::Const { ident, .. } => {
} = &mut params[index]; errors.error(
bounded_ty,
format_args!("expected type, found const parameter `{ident}`"),
);
continue;
}
};
parsed_bounds.extend(errors.ok(syn::parse2::<ParsedBounds>( parsed_bounds.extend(errors.ok(syn::parse2::<ParsedBounds>(
unparsed_bounds.to_token_stream(), unparsed_bounds.to_token_stream(),
))); )));
} }
let params = let params =
Punctuated::from_iter(params.into_pairs().map_pair_value(|param| match param { Punctuated::from_iter(unparsed_params.into_pairs().map_pair_value(
UnparsedGenericParam::Type { |param| match param {
attrs, UnparsedGenericParam::Type {
options, attrs,
ident, options,
colon_token, ident,
mut bounds, colon_token,
} => { mut bounds,
bounds.add_implied_bounds(); } => {
match bounds.categorize(&mut errors, ident.span()) { bounds.add_implied_bounds();
ParsedBoundsCategory::Type(bounds) => { match bounds.categorize(&mut errors, ident.span()) {
ParsedGenericParam::Type(ParsedTypeParam { ParsedBoundsCategory::Type(bounds) => {
attrs, ParsedGenericParam::Type(ParsedTypeParam {
options, attrs,
ident, options,
colon_token, ident,
bounds, colon_token,
default: None, bounds,
}) default: None,
} })
ParsedBoundsCategory::SizeType(bounds) => { }
ParsedGenericParam::SizeType(ParsedSizeTypeParam { ParsedBoundsCategory::SizeType(bounds) => {
attrs, ParsedGenericParam::SizeType(ParsedSizeTypeParam {
options, attrs,
ident, options,
colon_token, ident,
bounds, colon_token,
default: None, bounds,
}) default: None,
})
}
} }
} }
} UnparsedGenericParam::Const {
})); attrs,
options,
const_token,
ident,
colon_token,
ty,
} => ParsedGenericParam::Const(ParsedConstParam {
bounds: ParsedSizeTypeBounds {
KnownSize: None,
Size: Some(known_items::Size(ident.span())),
},
attrs,
options,
const_token,
ident,
colon_token,
ty,
}),
},
));
let mut retval = Self { let mut retval = Self {
lt_token: *lt_token, lt_token: *lt_token,
params, params,
gt_token: *gt_token, gt_token: *gt_token,
param_name_to_index_map, param_name_to_index_map,
}; };
for (cur_param_index, default) in defaults.into_iter().enumerate() { for (
let Some((eq, mut ty)) = default else { cur_param_index,
continue; LateParsedParam {
}; default,
let Ok(ty) = TypesParser { const_param_type,
},
) in late_parsed_params.into_iter().enumerate()
{
let mut parser = TypesParser {
generics: &retval, generics: &retval,
cur_param_index: Some(cur_param_index), cur_param_index: Some(cur_param_index),
errors: &mut errors, errors: &mut errors,
}
.parse(&mut ty) else {
continue;
}; };
let parsed_default = default.and_then(|(eq, ty)| {
let ty = parser.parse(ty).ok()?;
Some((eq, ty))
});
let parsed_const_param_type = const_param_type.and_then(|ty| parser.parse(ty).ok());
match &mut retval.params[cur_param_index] { match &mut retval.params[cur_param_index] {
ParsedGenericParam::Type(ParsedTypeParam { default, .. }) ParsedGenericParam::Type(ParsedTypeParam { default, .. })
| ParsedGenericParam::SizeType(ParsedSizeTypeParam { default, .. }) => { | ParsedGenericParam::SizeType(ParsedSizeTypeParam { default, .. }) => {
*default = Some((eq, ty)) *default = parsed_default;
}
ParsedGenericParam::Const(ParsedConstParam {
attrs: _,
options: _,
const_token: _,
ident: _,
colon_token: _,
ty,
bounds: _,
}) => {
if let Some(parsed_const_param_type) = parsed_const_param_type {
*ty = parsed_const_param_type;
}
} }
} }
} }
@ -2517,6 +2802,21 @@ impl ToTokens for ParsedGenericsImplGenerics<'_> {
colon_token.to_tokens(tokens); colon_token.to_tokens(tokens);
bounds.to_tokens(tokens); bounds.to_tokens(tokens);
} }
ParsedGenericParam::Const(ParsedConstParam {
attrs: _,
options,
const_token,
ident,
colon_token,
ty,
bounds: _,
}) => {
let ConstParamOptions {} = options.body;
const_token.to_tokens(tokens);
ident.to_tokens(tokens);
colon_token.to_tokens(tokens);
ty.to_tokens(tokens);
}
} }
punct.to_tokens(tokens); punct.to_tokens(tokens);
} }
@ -2609,6 +2909,27 @@ impl ToTokens for ParsedGenericsWhereClause<'_> {
} }
} }
ParsedGenericParam::SizeType(_) => {} ParsedGenericParam::SizeType(_) => {}
ParsedGenericParam::Const(ParsedConstParam {
ident: _,
bounds:
ParsedConstParamWhereBounds {
const_usize,
lt_token,
ident,
gt_token,
colon_token,
bounds,
},
..
}) => {
where_token(ident.span()).to_tokens(tokens);
const_usize.to_tokens(tokens);
lt_token.to_tokens(tokens);
ident.to_tokens(tokens);
gt_token.to_tokens(tokens);
colon_token.to_tokens(tokens);
bounds.to_tokens(tokens);
}
} }
} }
} }