Skip to content

WIP: Quote 0.6 #1334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ clap = "2"
clang-sys = { version = "0.22.0", features = ["runtime", "clang_6_0"] }
lazy_static = "1"
peeking_take_while = "0.1.2"
quote = { version = "0.5", default-features = false }
quote = { version = "0.6", default-features = false }
regex = "1.0"
which = "1.0.2"
# New validation in 0.3.6 breaks bindgen-integration:
# https://github.com/alexcrichton/proc-macro2/commit/489c642.
proc-macro2 = { version = "0.3.2, < 0.3.6", default-features = false }
proc-macro2 = { version = "0.4", default-features = false }

[dependencies.env_logger]
optional = true
Expand Down
59 changes: 25 additions & 34 deletions src/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,45 @@

use ir::context::BindgenContext;
use ir::layout::Layout;
use quote;
use std::mem;
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 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 {
quote! {#[doc=#comment]}
}

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);
Expand All @@ -60,7 +52,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(layout: Layout) -> quote::Tokens {
pub fn blob(layout: Layout) -> proc_macro2::TokenStream {
let opaque = layout.opaque();

// FIXME(emilio, #412): We fall back to byte alignment, but there are
Expand All @@ -75,7 +67,7 @@ pub fn blob(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().unwrap_or(layout.size);

Expand All @@ -91,7 +83,7 @@ pub fn blob(layout: Layout) -> quote::Tokens {
}

/// Integer type of the same size as the given `Layout`.
pub fn integer_type(layout: Layout) -> Option<quote::Tokens> {
pub fn integer_type(layout: Layout) -> Option<proc_macro2::TokenStream> {
// This guard can be weakened when Rust implements u128.
if layout.size > mem::size_of::<u64>() {
None
Expand All @@ -101,7 +93,7 @@ pub fn integer_type(layout: Layout) -> Option<quote::Tokens> {
}

/// 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 {
Expand All @@ -127,10 +119,9 @@ pub mod ast_ty {
use ir::context::BindgenContext;
use ir::function::FunctionSig;
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) => {
Expand All @@ -148,7 +139,7 @@ pub mod ast_ty {
pub fn float_kind_rust_type(
ctx: &BindgenContext,
fk: FloatKind,
) -> quote::Tokens {
) -> proc_macro2::TokenStream {
// TODO: we probably should just take the type layout into
// account?
//
Expand All @@ -167,25 +158,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! {
Expand All @@ -196,7 +187,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);

Expand Down Expand Up @@ -230,7 +221,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()
Expand Down
16 changes: 8 additions & 8 deletions src/codegen/impl_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use ir::context::BindgenContext;
use ir::derive::CanTriviallyDeriveDebug;
use ir::item::{HasTypeParamInArray, IsOpaque, Item, ItemCanonicalName};
use ir::ty::{RUST_DERIVE_IN_ARRAY_LIMIT, TypeKind};
use quote;
use proc_macro2;

pub fn gen_debug_impl(
ctx: &BindgenContext,
fields: &[Field],
item: &Item,
kind: CompKind,
) -> quote::Tokens {
) -> proc_macro2::TokenStream {
let struct_name = item.canonical_name(ctx);
let mut format_string = format!("{} {{{{ ", struct_name);
let mut tokens = vec![];
Expand Down Expand Up @@ -61,7 +61,7 @@ pub trait ImplDebug<'a> {
&self,
ctx: &BindgenContext,
extra: Self::Extra,
) -> Option<(String, Vec<quote::Tokens>)>;
) -> Option<(String, Vec<proc_macro2::TokenStream>)>;
}

impl<'a> ImplDebug<'a> for FieldData {
Expand All @@ -71,7 +71,7 @@ impl<'a> ImplDebug<'a> for FieldData {
&self,
ctx: &BindgenContext,
_: Self::Extra,
) -> Option<(String, Vec<quote::Tokens>)> {
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
if let Some(name) = self.name() {
ctx.resolve_item(self.ty()).impl_debug(ctx, name)
} else {
Expand All @@ -87,7 +87,7 @@ impl<'a> ImplDebug<'a> for BitfieldUnit {
&self,
ctx: &BindgenContext,
_: Self::Extra,
) -> Option<(String, Vec<quote::Tokens>)> {
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
let mut format_string = String::new();
let mut tokens = vec![];
for (i, bitfield) in self.bitfields().iter().enumerate() {
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<'a> ImplDebug<'a> for Item {
&self,
ctx: &BindgenContext,
name: &str,
) -> Option<(String, Vec<quote::Tokens>)> {
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
let name_ident = ctx.rust_ident(name);

// We don't know if blacklisted items `impl Debug` or not, so we can't
Expand All @@ -134,8 +134,8 @@ impl<'a> ImplDebug<'a> for Item {

fn debug_print(
name: &str,
name_ident: quote::Tokens,
) -> Option<(String, Vec<quote::Tokens>)> {
name_ident: proc_macro2::TokenStream,
) -> Option<(String, Vec<proc_macro2::TokenStream>)> {
Some((
format!("{}: {{:?}}", name),
vec![quote! {
Expand Down
9 changes: 4 additions & 5 deletions src/codegen/impl_partialeq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use ir::comp::{CompInfo, CompKind, Field, FieldMethods};
use ir::context::BindgenContext;
use ir::item::{IsOpaque, Item};
use ir::ty::{TypeKind, RUST_DERIVE_IN_ARRAY_LIMIT};
use quote;
use proc_macro2;

/// Generate a manual implementation of `PartialEq` trait for the
Expand All @@ -12,8 +11,8 @@ pub fn gen_partialeq_impl(
ctx: &BindgenContext,
comp_info: &CompInfo,
item: &Item,
ty_for_impl: &quote::Tokens,
) -> Option<quote::Tokens> {
ty_for_impl: &proc_macro2::TokenStream,
) -> Option<proc_macro2::TokenStream> {
let mut tokens = vec![];

if item.is_opaque(ctx, &()) {
Expand Down Expand Up @@ -71,8 +70,8 @@ pub fn gen_partialeq_impl(
})
}

fn gen_field(ctx: &BindgenContext, ty_item: &Item, name: &str) -> quote::Tokens {
fn quote_equals(name_ident: proc_macro2::Term) -> quote::Tokens {
fn gen_field(ctx: &BindgenContext, ty_item: &Item, name: &str) -> proc_macro2::TokenStream {
fn quote_equals(name_ident: proc_macro2::Ident) -> proc_macro2::TokenStream {
quote! { self.#name_ident == other.#name_ident }
}

Expand Down
Loading