Backend refactor (#411)

* remove BindgenAttrs from other backend::ast structs

This is primarily a tool for use with the macro crate. Most of
these attributes were ignored in the actual codegen, but a few
were still being used. This is confusing when trying to add
other sources for codegen (such as webidl and typescript).

* move parsing logic to macro crate

This makes the backend crate solely concerned with having an ast
for which we can generate code.
This commit is contained in:
R. Andrew Ohana
2018-07-07 10:20:31 -07:00
committed by Alex Crichton
parent 056b45aeed
commit 2d50d5209b
10 changed files with 1095 additions and 1053 deletions

View File

@ -129,7 +129,7 @@ impl WebidlParse<()> for webidl::ast::Interface {
impl WebidlParse<()> for webidl::ast::Typedef {
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
if util::is_chrome_only(&self.extended_attributes) {
return Ok(())
return Ok(());
}
let dest = rust_ident(&self.name);
@ -159,7 +159,7 @@ impl WebidlParse<()> for webidl::ast::Typedef {
impl WebidlParse<()> for webidl::ast::NonPartialInterface {
fn webidl_parse(&self, program: &mut backend::ast::Program, _: ()) -> Result<()> {
if util::is_chrome_only(&self.extended_attributes) {
return Ok(())
return Ok(());
}
program.imports.push(backend::ast::Import {
@ -205,9 +205,8 @@ impl<'a> WebidlParse<&'a webidl::ast::NonPartialInterface> for webidl::ast::Exte
arguments
.iter()
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
kind,
Some(self_ty),
vec![backend::ast::BindgenAttr::Constructor],
kind,
).map(|function| {
program.imports.push(backend::ast::Import {
module: None,
@ -303,24 +302,16 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::Operation {
impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
if util::is_chrome_only(&self.extended_attributes) {
return Ok(())
return Ok(());
}
create_getter(
&self.name,
&self.type_,
self_name,
backend::ast::MethodKind::Normal,
).map(wrap_import_function)
create_getter(&self.name, &self.type_, self_name, false)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
if !self.read_only {
create_setter(
&self.name,
&self.type_,
self_name,
backend::ast::MethodKind::Normal,
).map(wrap_import_function)
create_setter(&self.name, &self.type_, self_name, false)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
}
@ -331,24 +322,16 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularAttribute {
impl<'a> WebidlParse<&'a str> for webidl::ast::StaticAttribute {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
if util::is_chrome_only(&self.extended_attributes) {
return Ok(())
return Ok(());
}
create_getter(
&self.name,
&self.type_,
self_name,
backend::ast::MethodKind::Static,
).map(wrap_import_function)
create_getter(&self.name, &self.type_, self_name, true)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
if !self.read_only {
create_setter(
&self.name,
&self.type_,
self_name,
backend::ast::MethodKind::Static,
).map(wrap_import_function)
create_setter(&self.name, &self.type_, self_name, true)
.map(wrap_import_function)
.map(|import| program.imports.push(import));
}
@ -359,7 +342,7 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::StaticAttribute {
impl<'a> WebidlParse<&'a str> for webidl::ast::RegularOperation {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
if util::is_chrome_only(&self.extended_attributes) {
return Ok(())
return Ok(());
}
create_basic_method(
@ -367,7 +350,7 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularOperation {
self.name.as_ref(),
&self.return_type,
self_name,
backend::ast::MethodKind::Normal,
false,
).map(wrap_import_function)
.map(|import| program.imports.push(import));
@ -378,7 +361,7 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::RegularOperation {
impl<'a> WebidlParse<&'a str> for webidl::ast::StaticOperation {
fn webidl_parse(&self, program: &mut backend::ast::Program, self_name: &'a str) -> Result<()> {
if util::is_chrome_only(&self.extended_attributes) {
return Ok(())
return Ok(());
}
create_basic_method(
@ -386,7 +369,7 @@ impl<'a> WebidlParse<&'a str> for webidl::ast::StaticOperation {
self.name.as_ref(),
&self.return_type,
self_name,
backend::ast::MethodKind::Static,
true,
).map(wrap_import_function)
.map(|import| program.imports.push(import));

View File

@ -114,7 +114,10 @@ where
let len = estimate.1.unwrap_or(estimate.0);
let mut res = if let backend::ast::ImportFunctionKind::Method {
ty,
kind: backend::ast::MethodKind::Normal,
kind:
backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static: false, ..
}),
..
} = kind
{
@ -146,9 +149,8 @@ where
pub fn create_function<'a, I>(
name: &str,
arguments: I,
kind: backend::ast::ImportFunctionKind,
ret: Option<syn::Type>,
mut attrs: Vec<backend::ast::BindgenAttr>,
kind: backend::ast::ImportFunctionKind,
) -> Option<backend::ast::ImportFunction>
where
I: Iterator<Item = (&'a str, &'a webidl::ast::Type, bool)>,
@ -160,12 +162,6 @@ where
let js_ret = ret.clone();
if let backend::ast::ImportFunctionKind::Method { .. } = kind {
attrs.push(backend::ast::BindgenAttr::Method);
}
let opts = backend::ast::BindgenAttrs { attrs };
let shim = {
let ns = match kind {
backend::ast::ImportFunctionKind::Normal => "",
@ -180,7 +176,6 @@ where
name,
arguments,
ret,
opts,
rust_attrs: vec![],
rust_vis: syn::Visibility::Public(syn::VisPublic {
pub_token: Default::default(),
@ -188,6 +183,8 @@ where
},
rust_name,
js_ret,
catch: false,
structural: false,
kind,
shim,
})
@ -198,7 +195,7 @@ pub fn create_basic_method(
name: Option<&String>,
return_type: &webidl::ast::ReturnType,
self_name: &str,
kind: backend::ast::MethodKind,
is_static: bool,
) -> Option<backend::ast::ImportFunction> {
let name = match name {
None => {
@ -211,7 +208,10 @@ pub fn create_basic_method(
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
kind,
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static,
kind: backend::ast::OperationKind::Regular,
}),
};
let ret = match return_type {
@ -231,9 +231,8 @@ pub fn create_basic_method(
arguments
.iter()
.map(|arg| (&*arg.name, &*arg.type_, arg.variadic)),
kind,
ret,
Vec::new(),
kind,
)
}
@ -241,7 +240,7 @@ pub fn create_getter(
name: &str,
ty: &webidl::ast::Type,
self_name: &str,
kind: backend::ast::MethodKind,
is_static: bool,
) -> Option<backend::ast::ImportFunction> {
let ret = match webidl_ty_to_syn_ty(ty, TypePosition::Return) {
None => {
@ -254,36 +253,35 @@ pub fn create_getter(
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
kind,
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static,
kind: backend::ast::OperationKind::Getter(Some(raw_ident(name))),
}),
};
create_function(
name,
iter::empty(),
kind,
ret,
vec![backend::ast::BindgenAttr::Getter(Some(raw_ident(name)))],
)
create_function(name, iter::empty(), ret, kind)
}
pub fn create_setter(
name: &str,
ty: &webidl::ast::Type,
self_name: &str,
kind: backend::ast::MethodKind,
is_static: bool,
) -> Option<backend::ast::ImportFunction> {
let kind = backend::ast::ImportFunctionKind::Method {
class: self_name.to_string(),
ty: ident_ty(rust_ident(self_name)),
kind,
kind: backend::ast::MethodKind::Operation(backend::ast::Operation {
is_static,
kind: backend::ast::OperationKind::Setter(Some(raw_ident(name))),
}),
};
create_function(
&format!("set_{}", name),
iter::once((name, ty, false)),
kind,
None,
vec![backend::ast::BindgenAttr::Setter(Some(raw_ident(name)))],
kind,
)
}
@ -294,24 +292,26 @@ pub fn is_chrome_only(ext_attrs: &[Box<ExtendedAttribute>]) -> bool {
ExtendedAttribute::ArgumentList(al) => {
println!("ArgumentList");
al.name == "ChromeOnly"
},
}
ExtendedAttribute::Identifier(i) => {
println!("Identifier");
i.lhs == "ChromeOnly"
},
}
ExtendedAttribute::IdentifierList(il) => {
println!("IdentifierList");
il.lhs == "ChromeOnly"
},
}
ExtendedAttribute::NamedArgumentList(nal) => {
println!("NamedArgumentList");
nal.lhs_name == "ChromeOnly"
},
ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name)) => name == "ChromeOnly",
}
ExtendedAttribute::NoArguments(webidl::ast::Other::Identifier(name)) => {
name == "ChromeOnly"
}
ExtendedAttribute::NoArguments(_na) => {
println!("NoArguments");
false
}
};
})
}
}