From 8fbf478058f54a8fc81384f36a12bb890c3c81ae Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 25 Jun 2018 10:41:33 -0700 Subject: [PATCH] Move some utility functions from the webidl crate into the backend crate --- crates/backend/src/ast.rs | 17 ++-------- crates/backend/src/lib.rs | 1 + crates/backend/src/util.rs | 69 ++++++++++++++++++++++++++++++++++++++ crates/webidl/src/lib.rs | 10 +++--- crates/webidl/src/util.rs | 67 ++---------------------------------- 5 files changed, 80 insertions(+), 84 deletions(-) create mode 100644 crates/backend/src/util.rs diff --git a/crates/backend/src/ast.rs b/crates/backend/src/ast.rs index aa15a486..f62a3ebd 100644 --- a/crates/backend/src/ast.rs +++ b/crates/backend/src/ast.rs @@ -1,8 +1,8 @@ use proc_macro2::{Ident, Span, TokenStream, TokenTree}; use quote::ToTokens; use shared; -use std::iter::FromIterator; use syn; +use util; #[cfg_attr(feature = "extra-traits", derive(Debug, PartialEq, Eq))] #[derive(Default)] @@ -416,20 +416,7 @@ impl Program { } else if let Some(cls) = wasm.opts.static_method_of() { let class = cls.to_string(); let kind = MethodKind::Static; - - let segments = syn::punctuated::Punctuated::from_iter(Some(syn::PathSegment { - ident: cls.clone(), - arguments: syn::PathArguments::None, - })); - - let ty = syn::Type::Path(syn::TypePath { - qself: None, - path: syn::Path { - leading_colon: None, - segments, - }, - }); - + let ty = util::ident_ty(cls.clone()); ImportFunctionKind::Method { class, ty, kind } } else if wasm.opts.constructor() { let class = match wasm.ret { diff --git a/crates/backend/src/lib.rs b/crates/backend/src/lib.rs index 19ad1957..60a513da 100755 --- a/crates/backend/src/lib.rs +++ b/crates/backend/src/lib.rs @@ -12,3 +12,4 @@ extern crate wasm_bindgen_shared as shared; pub mod ast; mod codegen; +pub mod util; diff --git a/crates/backend/src/util.rs b/crates/backend/src/util.rs new file mode 100644 index 00000000..8773c4ab --- /dev/null +++ b/crates/backend/src/util.rs @@ -0,0 +1,69 @@ +use std::iter::FromIterator; + +use ast; +use proc_macro2::{self, Ident}; +use syn; + +fn is_rust_keyword(name: &str) -> bool { + match name { + "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" + | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if" + | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" + | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return" + | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true" + | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while" + | "yield" | "bool" | "_" => true, + _ => false, + } +} + +// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword. +pub fn rust_ident(name: &str) -> Ident { + if is_rust_keyword(name) { + Ident::new(&format!("{}_", name), proc_macro2::Span::call_site()) + } else { + raw_ident(name) + } +} + +// Create an `Ident` without checking to see if it conflicts with a Rust +// keyword. +pub fn raw_ident(name: &str) -> Ident { + Ident::new(name, proc_macro2::Span::call_site()) +} + +/// Create a path type from the given segments. For example an iterator yielding +/// the idents `[foo, bar, baz]` will result in the path type `foo::bar::baz`. +pub fn simple_path_ty(segments: I) -> syn::Type +where + I: IntoIterator, +{ + let segments: Vec<_> = segments + .into_iter() + .map(|i| syn::PathSegment { + ident: i, + arguments: syn::PathArguments::None, + }) + .collect(); + + syn::TypePath { + qself: None, + path: syn::Path { + leading_colon: None, + segments: syn::punctuated::Punctuated::from_iter(segments), + }, + }.into() +} + +pub fn ident_ty(ident: Ident) -> syn::Type { + simple_path_ty(Some(ident)) +} + +pub fn wrap_import_function(function: ast::ImportFunction) -> ast::Import { + ast::Import { + module: None, + version: None, + js_namespace: None, + kind: ast::ImportKind::Function(function), + } +} diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index c024cd93..950207eb 100755 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -18,19 +18,19 @@ extern crate syn; extern crate wasm_bindgen_backend as backend; extern crate webidl; +mod util; + use std::fs; use std::io::{self, Read}; -use std::iter; use std::path::Path; +use backend::util::{ident_ty, rust_ident, wrap_import_function}; use failure::ResultExt; use quote::ToTokens; -mod util; - use util::{ - create_basic_method, create_function, create_getter, create_setter, ident_ty, rust_ident, - webidl_ty_to_syn_ty, wrap_import_function, TypePosition, + create_basic_method, create_function, create_getter, create_setter, webidl_ty_to_syn_ty, + TypePosition, }; /// Either `Ok(t)` or `Err(failure::Error)`. diff --git a/crates/webidl/src/util.rs b/crates/webidl/src/util.rs index c8d2eb5a..5b197dde 100644 --- a/crates/webidl/src/util.rs +++ b/crates/webidl/src/util.rs @@ -1,64 +1,12 @@ -use std::iter::{self, FromIterator}; +use std::iter; use backend; +use backend::util::{ident_ty, raw_ident, rust_ident, simple_path_ty}; use heck::SnakeCase; -use proc_macro2::{self, Ident}; +use proc_macro2::Ident; use syn; use webidl; -fn is_rust_keyword(name: &str) -> bool { - match name { - "abstract" | "alignof" | "as" | "become" | "box" | "break" | "const" | "continue" - | "crate" | "do" | "else" | "enum" | "extern" | "false" | "final" | "fn" | "for" | "if" - | "impl" | "in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" - | "offsetof" | "override" | "priv" | "proc" | "pub" | "pure" | "ref" | "return" - | "Self" | "self" | "sizeof" | "static" | "struct" | "super" | "trait" | "true" - | "type" | "typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" | "while" - | "yield" | "bool" | "_" => true, - _ => false, - } -} - -// Create an `Ident`, possibly mangling it if it conflicts with a Rust keyword. -pub fn rust_ident(name: &str) -> Ident { - if is_rust_keyword(name) { - Ident::new(&format!("{}_", name), proc_macro2::Span::call_site()) - } else { - raw_ident(name) - } -} - -// Create an `Ident` without checking to see if it conflicts with a Rust -// keyword. -fn raw_ident(name: &str) -> Ident { - Ident::new(name, proc_macro2::Span::call_site()) -} - -fn simple_path_ty(segments: I) -> syn::Type -where - I: IntoIterator, -{ - let segments: Vec<_> = segments - .into_iter() - .map(|i| syn::PathSegment { - ident: i, - arguments: syn::PathArguments::None, - }) - .collect(); - - syn::TypePath { - qself: None, - path: syn::Path { - leading_colon: None, - segments: syn::punctuated::Punctuated::from_iter(segments), - }, - }.into() -} - -pub fn ident_ty(ident: Ident) -> syn::Type { - simple_path_ty(Some(ident)) -} - fn shared_ref(ty: syn::Type) -> syn::Type { syn::TypeReference { and_token: Default::default(), @@ -337,12 +285,3 @@ pub fn create_setter( vec![backend::ast::BindgenAttr::Setter(Some(raw_ident(name)))], ) } - -pub fn wrap_import_function(function: backend::ast::ImportFunction) -> backend::ast::Import { - backend::ast::Import { - module: None, - version: None, - js_namespace: None, - kind: backend::ast::ImportKind::Function(function), - } -}