forked from libre-chip/fayalite
133 lines
4 KiB
Rust
133 lines
4 KiB
Rust
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
// See Notices.txt for copyright information
|
|
use crate::{
|
|
hdl_type_common::{
|
|
get_target, ItemOptions, MakeHdlTypeExpr, MaybeParsed, ParsedGenerics, ParsedType,
|
|
TypesParser,
|
|
},
|
|
kw, Errors, HdlAttr,
|
|
};
|
|
use proc_macro2::TokenStream;
|
|
use quote::ToTokens;
|
|
use syn::{parse_quote_spanned, Attribute, Generics, Ident, ItemType, Token, Type, Visibility};
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub(crate) struct ParsedTypeAlias {
|
|
pub(crate) attrs: Vec<Attribute>,
|
|
pub(crate) options: HdlAttr<ItemOptions, kw::hdl>,
|
|
pub(crate) vis: Visibility,
|
|
pub(crate) type_token: Token![type],
|
|
pub(crate) ident: Ident,
|
|
pub(crate) generics: MaybeParsed<ParsedGenerics, Generics>,
|
|
pub(crate) eq_token: Token![=],
|
|
pub(crate) ty: MaybeParsed<ParsedType, Type>,
|
|
pub(crate) semi_token: Token![;],
|
|
}
|
|
|
|
impl ParsedTypeAlias {
|
|
fn parse(item: ItemType) -> syn::Result<Self> {
|
|
let ItemType {
|
|
mut attrs,
|
|
vis,
|
|
type_token,
|
|
ident,
|
|
mut generics,
|
|
eq_token,
|
|
ty,
|
|
semi_token,
|
|
} = item;
|
|
let mut errors = Errors::new();
|
|
let mut options = errors
|
|
.unwrap_or_default(HdlAttr::<ItemOptions, kw::hdl>::parse_and_take_attr(
|
|
&mut attrs,
|
|
))
|
|
.unwrap_or_default();
|
|
errors.ok(options.body.validate());
|
|
let ItemOptions {
|
|
outline_generated: _,
|
|
target: _,
|
|
custom_bounds,
|
|
no_static,
|
|
no_runtime_generics: _,
|
|
} = options.body;
|
|
if let Some((no_static,)) = no_static {
|
|
errors.error(no_static, "no_static is not valid on type aliases");
|
|
}
|
|
let generics = if custom_bounds.is_some() {
|
|
MaybeParsed::Unrecognized(generics)
|
|
} else if let Some(generics) = errors.ok(ParsedGenerics::parse(&mut generics)) {
|
|
MaybeParsed::Parsed(generics)
|
|
} else {
|
|
MaybeParsed::Unrecognized(generics)
|
|
};
|
|
let ty = TypesParser::maybe_run(generics.as_ref(), *ty, &mut errors);
|
|
errors.finish()?;
|
|
Ok(Self {
|
|
attrs,
|
|
options,
|
|
vis,
|
|
type_token,
|
|
ident,
|
|
generics,
|
|
eq_token,
|
|
ty,
|
|
semi_token,
|
|
})
|
|
}
|
|
}
|
|
|
|
impl ToTokens for ParsedTypeAlias {
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
|
let Self {
|
|
attrs,
|
|
options,
|
|
vis,
|
|
type_token,
|
|
ident,
|
|
generics,
|
|
eq_token,
|
|
ty,
|
|
semi_token,
|
|
} = self;
|
|
let ItemOptions {
|
|
outline_generated: _,
|
|
target,
|
|
custom_bounds: _,
|
|
no_static: _,
|
|
no_runtime_generics,
|
|
} = &options.body;
|
|
let target = get_target(target, ident);
|
|
let mut type_attrs = attrs.clone();
|
|
type_attrs.push(parse_quote_spanned! {ident.span()=>
|
|
#[allow(type_alias_bounds)]
|
|
});
|
|
ItemType {
|
|
attrs: type_attrs,
|
|
vis: vis.clone(),
|
|
type_token: *type_token,
|
|
ident: ident.clone(),
|
|
generics: generics.into(),
|
|
eq_token: *eq_token,
|
|
ty: Box::new(ty.clone().into()),
|
|
semi_token: *semi_token,
|
|
}
|
|
.to_tokens(tokens);
|
|
if let (MaybeParsed::Parsed(generics), MaybeParsed::Parsed(ty), None) =
|
|
(generics, ty, no_runtime_generics)
|
|
{
|
|
generics.make_runtime_generics(tokens, vis, ident, &target, |context| {
|
|
ty.make_hdl_type_expr(context)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
pub(crate) fn hdl_type_alias_impl(item: ItemType) -> syn::Result<TokenStream> {
|
|
let item = ParsedTypeAlias::parse(item)?;
|
|
let outline_generated = item.options.body.outline_generated;
|
|
let mut contents = item.to_token_stream();
|
|
if outline_generated.is_some() {
|
|
contents = crate::outline_generated(contents, "hdl-type-alias-");
|
|
}
|
|
Ok(contents)
|
|
}
|