webidl: add support for typedefs

This commit is contained in:
R. Andrew Ohana
2018-06-11 18:35:20 -07:00
parent 3710d87f3b
commit d065f4b05d
5 changed files with 58 additions and 2 deletions

View File

@ -10,6 +10,7 @@ pub struct Program {
pub imports: Vec<Import>,
pub enums: Vec<Enum>,
pub structs: Vec<Struct>,
pub type_aliases: Vec<TypeAlias>,
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
@ -120,6 +121,13 @@ pub enum TypeLocation {
ExportRet,
}
#[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))]
pub struct TypeAlias {
pub vis: syn::Visibility,
pub dest: Ident,
pub src: syn::Type,
}
impl Program {
pub fn push_item(
&mut self,

View File

@ -59,6 +59,9 @@ impl ToTokens for ast::Program {
for e in self.enums.iter() {
e.to_tokens(tokens);
}
for a in self.type_aliases.iter() {
a.to_tokens(tokens);
}
// Generate a static which will eventually be what lives in a custom section
// of the wasm executable. For now it's just a plain old static, but we'll
@ -823,3 +826,15 @@ impl ToTokens for ast::ImportStatic {
}).to_tokens(into);
}
}
impl ToTokens for ast::TypeAlias {
fn to_tokens(&self, into: &mut TokenStream) {
let vis = &self.vis;
let dest = &self.dest;
let src = &self.src;
(quote! {
#[allow(non_camel_case_types)]
#vis type #dest = #src;
}).to_tokens(into);
}
}