Move some utility functions from the webidl crate into the backend crate

This commit is contained in:
Nick Fitzgerald
2018-06-25 10:41:33 -07:00
parent eb04d15a65
commit 8fbf478058
5 changed files with 80 additions and 84 deletions

View File

@ -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 {

View File

@ -12,3 +12,4 @@ extern crate wasm_bindgen_shared as shared;
pub mod ast;
mod codegen;
pub mod util;

View File

@ -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<I>(segments: I) -> syn::Type
where
I: IntoIterator<Item = Ident>,
{
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),
}
}