summaryrefslogtreecommitdiff
path: root/src/codegen/helpers.rs
diff options
context:
space:
mode:
authorBastien Orivel <eijebong@bananium.fr>2018-05-29 12:33:02 +0200
committerEmilio Cobos Álvarez <emilio@crisal.io>2018-11-30 06:25:53 +0100
commit631871c553e16e020ea4161530b76d03b0c9bc3f (patch)
treebc724aba974a6abf0befb5f36643d649a2bc0330 /src/codegen/helpers.rs
parent33912cfcdc34bda21599de29cd7b4241eb7c341a (diff)
Update quote and proc-macro.
I give up on the doc comments. This is a rebase of #1334 keeping the formatting of the comments and using TokenStream::from_str instead because one can hope. Fixes #1407.
Diffstat (limited to 'src/codegen/helpers.rs')
-rw-r--r--src/codegen/helpers.rs67
1 files changed, 31 insertions, 36 deletions
diff --git a/src/codegen/helpers.rs b/src/codegen/helpers.rs
index 02909d57..aea4b1fb 100644
--- a/src/codegen/helpers.rs
+++ b/src/codegen/helpers.rs
@@ -2,58 +2,54 @@
use ir::context::BindgenContext;
use ir::layout::Layout;
-use quote;
-use proc_macro2::{Term, Span};
+use proc_macro2::{self, Ident, Span};
+use quote::TokenStreamExt;
pub mod attributes {
- use quote;
- use proc_macro2::{Term, Span};
+ use proc_macro2::{self, Ident, Span};
- pub fn repr(which: &str) -> quote::Tokens {
- let which = Term::new(which, Span::call_site());
+ pub fn repr(which: &str) -> proc_macro2::TokenStream {
+ let which = Ident::new(which, Span::call_site());
quote! {
#[repr( #which )]
}
}
- pub fn repr_list(which_ones: &[&str]) -> quote::Tokens {
- let which_ones = which_ones.iter().cloned().map(|one| Term::new(one, Span::call_site()));
+ pub fn repr_list(which_ones: &[&str]) -> proc_macro2::TokenStream {
+ let which_ones = which_ones.iter().cloned().map(|one| Ident::new(one, Span::call_site()));
quote! {
#[repr( #( #which_ones ),* )]
}
}
- pub fn derives(which_ones: &[&str]) -> quote::Tokens {
- let which_ones = which_ones.iter().cloned().map(|one| Term::new(one, Span::call_site()));
+ pub fn derives(which_ones: &[&str]) -> proc_macro2::TokenStream {
+ let which_ones = which_ones.iter().cloned().map(|one| Ident::new(one, Span::call_site()));
quote! {
#[derive( #( #which_ones ),* )]
}
}
- pub fn inline() -> quote::Tokens {
+ pub fn inline() -> proc_macro2::TokenStream {
quote! {
#[inline]
}
}
- pub fn must_use() -> quote::Tokens {
+ pub fn must_use() -> proc_macro2::TokenStream {
quote! {
#[must_use]
}
}
- pub fn doc(comment: String) -> quote::Tokens {
- // Doc comments are already preprocessed into nice `///` formats by the
- // time they get here. Just make sure that we have newlines around it so
- // that nothing else gets wrapped into the comment.
- let mut tokens = quote! {};
- tokens.append(Term::new("\n", Span::call_site()));
- tokens.append(Term::new(&comment, Span::call_site()));
- tokens.append(Term::new("\n", Span::call_site()));
- tokens
+ pub fn doc(comment: String) -> proc_macro2::TokenStream {
+ use std::str::FromStr;
+
+ // NOTE(emilio): By this point comments are already preprocessed and in
+ // `///` form. Quote turns them into `#[doc]` comments, but oh well.
+ proc_macro2::TokenStream::from_str(&comment).unwrap()
}
- pub fn link_name(name: &str) -> quote::Tokens {
+ pub fn link_name(name: &str) -> proc_macro2::TokenStream {
// LLVM mangles the name by default but it's already mangled.
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
let name = format!("\u{1}{}", name);
@@ -65,7 +61,7 @@ pub mod attributes {
/// Generates a proper type for a field or type with a given `Layout`, that is,
/// a type with the correct size and alignment restrictions.
-pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
+pub fn blob(ctx: &BindgenContext, layout: Layout) -> proc_macro2::TokenStream {
let opaque = layout.opaque();
// FIXME(emilio, #412): We fall back to byte alignment, but there are
@@ -80,7 +76,7 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
}
};
- let ty_name = Term::new(ty_name, Span::call_site());
+ let ty_name = Ident::new(ty_name, Span::call_site());
let data_len = opaque.array_size(ctx).unwrap_or(layout.size);
@@ -96,14 +92,14 @@ pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
}
/// Integer type of the same size as the given `Layout`.
-pub fn integer_type(ctx: &BindgenContext, layout: Layout) -> Option<quote::Tokens> {
+pub fn integer_type(ctx: &BindgenContext, layout: Layout) -> Option<proc_macro2::TokenStream> {
let name = Layout::known_type_for_size(ctx, layout.size)?;
- let name = Term::new(name, Span::call_site());
+ let name = Ident::new(name, Span::call_site());
Some(quote! { #name })
}
/// Generates a bitfield allocation unit type for a type with the given `Layout`.
-pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
+pub fn bitfield_unit(ctx: &BindgenContext, layout: Layout) -> proc_macro2::TokenStream {
let mut tokens = quote! {};
if ctx.options().enable_cxx_namespaces {
@@ -130,10 +126,9 @@ pub mod ast_ty {
use ir::function::FunctionSig;
use ir::layout::Layout;
use ir::ty::FloatKind;
- use quote;
use proc_macro2;
- pub fn raw_type(ctx: &BindgenContext, name: &str) -> quote::Tokens {
+ pub fn raw_type(ctx: &BindgenContext, name: &str) -> proc_macro2::TokenStream {
let ident = ctx.rust_ident_raw(name);
match ctx.options().ctypes_prefix {
Some(ref prefix) => {
@@ -152,7 +147,7 @@ pub mod ast_ty {
ctx: &BindgenContext,
fk: FloatKind,
layout: Option<Layout>,
- ) -> quote::Tokens {
+ ) -> proc_macro2::TokenStream {
// TODO: we probably should take the type layout into account more
// often?
//
@@ -192,25 +187,25 @@ pub mod ast_ty {
}
}
- pub fn int_expr(val: i64) -> quote::Tokens {
+ pub fn int_expr(val: i64) -> proc_macro2::TokenStream {
// Don't use quote! { #val } because that adds the type suffix.
let val = proc_macro2::Literal::i64_unsuffixed(val);
quote!(#val)
}
- pub fn uint_expr(val: u64) -> quote::Tokens {
+ pub fn uint_expr(val: u64) -> proc_macro2::TokenStream {
// Don't use quote! { #val } because that adds the type suffix.
let val = proc_macro2::Literal::u64_unsuffixed(val);
quote!(#val)
}
- pub fn byte_array_expr(bytes: &[u8]) -> quote::Tokens {
+ pub fn byte_array_expr(bytes: &[u8]) -> proc_macro2::TokenStream {
let mut bytes: Vec<_> = bytes.iter().cloned().collect();
bytes.push(0);
quote! { [ #(#bytes),* ] }
}
- pub fn cstr_expr(mut string: String) -> quote::Tokens {
+ pub fn cstr_expr(mut string: String) -> proc_macro2::TokenStream {
string.push('\0');
let b = proc_macro2::Literal::byte_string(&string.as_bytes());
quote! {
@@ -221,7 +216,7 @@ pub mod ast_ty {
pub fn float_expr(
ctx: &BindgenContext,
f: f64,
- ) -> Result<quote::Tokens, ()> {
+ ) -> Result<proc_macro2::TokenStream, ()> {
if f.is_finite() {
let val = proc_macro2::Literal::f64_unsuffixed(f);
@@ -255,7 +250,7 @@ pub mod ast_ty {
pub fn arguments_from_signature(
signature: &FunctionSig,
ctx: &BindgenContext,
- ) -> Vec<quote::Tokens> {
+ ) -> Vec<proc_macro2::TokenStream> {
let mut unnamed_arguments = 0;
signature
.argument_types()