Skip to content

Commit

Permalink
Update quote and proc-macro.
Browse files Browse the repository at this point in the history
I give up on the doc comments. This is a rebase of rust-lang#1334 keeping the formatting
of the comments and using TokenStream::from_str instead because one can hope.

Fixes rust-lang#1407.
  • Loading branch information
Eijebong authored and emilio committed Oct 4, 2018
1 parent dc85e44 commit a71f3ec
Show file tree
Hide file tree
Showing 100 changed files with 1,305 additions and 1,614 deletions.
22 changes: 2 additions & 20 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 @@ -51,12 +51,12 @@ clap = "2"
clang-sys = { version = "0.24", 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 = "2.0"
# 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
65 changes: 30 additions & 35 deletions src/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,48 @@
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 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);
Expand All @@ -59,7 +55,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
Expand All @@ -74,7 +70,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);

Expand All @@ -90,14 +86,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 {
Expand All @@ -124,10 +120,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) => {
Expand All @@ -146,7 +141,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?
//
Expand Down Expand Up @@ -186,25 +181,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 @@ -215,7 +210,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 @@ -249,7 +244,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 @@ -63,7 +63,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 @@ -73,7 +73,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 @@ -89,7 +89,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 @@ -118,7 +118,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 @@ -136,8 +136,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

0 comments on commit a71f3ec

Please sign in to comment.