Commit e336f80f authored by Ryan Hunt's avatar Ryan Hunt

Update syn to 0.12.6

parent e2982b81
......@@ -24,9 +24,9 @@ version = "0.4.3"
dependencies = [
"clap 2.24.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.12.10 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
......@@ -80,11 +80,27 @@ name = "num-traits"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "proc-macro2"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quote"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "quote"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand"
version = "0.3.15"
......@@ -95,7 +111,7 @@ dependencies = [
[[package]]
name = "serde"
version = "1.0.21"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -128,7 +144,7 @@ dependencies = [
"dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -146,6 +162,16 @@ dependencies = [
"unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.12.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "synom"
version = "0.11.3"
......@@ -177,7 +203,7 @@ name = "toml"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"serde 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......@@ -195,6 +221,11 @@ name = "unicode-xid"
version = "0.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vec_map"
version = "0.8.0"
......@@ -221,14 +252,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum libc 0.2.23 (registry+https://github.com/rust-lang/crates.io-index)" = "e7eb6b826bfc1fdea7935d46556250d1799b7fe2d9f7951071f4291710665e3e"
"checksum log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "5141eca02775a762cc6cd564d8d2c50f67c0ea3a372cbf1c51592b3e029e10ad"
"checksum num-traits 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "e1cbfa3781f3fe73dc05321bed52a06d2d491eaa764c52335cf4399f046ece99"
"checksum proc-macro2 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5f9786e83afb5654ab1b336584548011f252db3c320c0ddba5dc21a1a76f83ca"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1eca14c727ad12702eb4b6bfb5a232287dcf8385cb8ca83a3eeaf6519c44c408"
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum serde 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "6eda663e865517ee783b0891a3f6eb3a253e0b0dabb46418969ee9635beadd9e"
"checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526"
"checksum serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "652bc323d694dc925829725ec6c890156d8e70ae5202919869cb00fe2eff3788"
"checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab"
"checksum serde_json 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e4586746d1974a030c48919731ecffd0ed28d0c40749d0d18d43b3a7d6c9b20e"
"checksum strsim 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d15c810519a91cf877e7e36e63fe068815c678181439f2f29e2562147c3694"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum syn 0.12.10 (registry+https://github.com/rust-lang/crates.io-index)" = "7d12ebcea3f1027a817b98e91cfe30805634ea1f63e36015f765960a7782494d"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6"
"checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209"
......@@ -236,6 +270,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a8083c594e02b8ae1654ae26f0ade5158b119bd88ad0e8227a5d8fcd72407946"
"checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f"
"checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
......@@ -23,9 +23,8 @@ tempdir = "0.3"
toml = "0.4"
[dependencies.syn]
version = "0.11"
default-features = false
features = ["full"]
version = "0.12.6"
features = ["extra-traits", "full"]
[[bin]]
name = "cbindgen"
......
......@@ -4,6 +4,7 @@
use std::fmt;
pub use syn::synom::ParseError;
pub use bindgen::cargo::cargo_metadata::Error as CargoMetadataError;
pub use bindgen::cargo::cargo_toml::Error as CargoTomlError;
pub use bindgen::cargo::cargo_expand::Error as CargoExpandError;
......@@ -13,7 +14,7 @@ pub enum Error {
CargoMetadata(String, CargoMetadataError),
CargoToml(String, CargoTomlError),
CargoExpand(String, CargoExpandError),
ParseSyntaxError{crate_name: String, src_path: String, message: String},
ParseSyntaxError{crate_name: String, src_path: String, error: ParseError},
ParseCannotOpenFile{crate_name: String, src_path: String},
}
......@@ -29,8 +30,8 @@ impl fmt::Display for Error {
&Error::CargoExpand(ref crate_name, ref error) => {
write!(f, "Parsing crate `{}`: couldn't run `cargo rustc --pretty=expanded`: {:?}", crate_name, error)
}
&Error::ParseSyntaxError{ref crate_name, ref src_path, ref message} => {
write!(f, "Parsing crate `{}`:`{}`:\n{}", crate_name, src_path, message)
&Error::ParseSyntaxError{ref crate_name, ref src_path, ref error} => {
write!(f, "Parsing crate `{}`:`{}`:\n{:?}", crate_name, src_path, error)
}
&Error::ParseCannotOpenFile{ref crate_name, ref src_path} => {
write!(f, "Parsing crate `{}`: cannot open file `{}`.", crate_name, src_path)
......
......@@ -45,15 +45,16 @@ impl AnnotationSet {
self.annotations.is_empty()
}
pub fn load(attrs: &Vec<syn::Attribute>) -> Result<AnnotationSet, String> {
pub fn load(attrs: &[syn::Attribute]) -> Result<AnnotationSet, String> {
let mut lines = Vec::new();
for attr in attrs {
if attr.style == syn::AttrStyle::Outer {
if let syn::MetaItem::NameValue(ref name, syn::Lit::Str(ref comment, _)) =
attr.value
if let Some(syn::Meta::NameValue(syn::MetaNameValue { ident, lit: syn::Lit::Str(comment), .. })) =
attr.interpret_meta()
{
if &*name == "doc" {
let comment = comment.value();
if &*ident.to_string() == "doc" {
let line = comment.trim_left_matches("///").trim();
if line.starts_with("cbindgen:") {
lines.push(line.to_owned());
......
......@@ -16,7 +16,7 @@ enum DefineKey<'a> {
impl<'a> DefineKey<'a> {
fn load(key: &str) -> DefineKey {
// TODO - dirty parser
// TODO: dirty parser
if key.contains("=") {
let mut splits = key.trim().split("=");
......@@ -72,7 +72,7 @@ impl Cfg {
}
}
pub fn load(attrs: &Vec<syn::Attribute>) -> Option<Cfg> {
pub fn load(attrs: &[syn::Attribute]) -> Option<Cfg> {
let mut configs = Vec::new();
for attr in attrs {
......@@ -80,18 +80,17 @@ impl Cfg {
continue;
}
match &attr.value {
&syn::MetaItem::Word(..) => {}
&syn::MetaItem::NameValue(..) => {}
&syn::MetaItem::List(ref ident, ref nested) => {
match attr.interpret_meta() {
Some(syn::Meta::List(syn::MetaList { ident, nested, .. })) => {
if ident.as_ref() != "cfg" || nested.len() != 1 {
continue;
}
if let Some(config) = Cfg::load_single(&nested[0]) {
if let Some(config) = Cfg::load_single(nested.first().unwrap().value()) {
configs.push(config);
}
}
_ => {}
}
}
......@@ -102,27 +101,27 @@ impl Cfg {
}
}
fn load_single(item: &syn::NestedMetaItem) -> Option<Cfg> {
fn load_single(item: &syn::NestedMeta) -> Option<Cfg> {
match item {
&syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ref ident)) => {
&syn::NestedMeta::Meta(syn::Meta::Word(ref ident)) => {
Some(Cfg::Boolean(ident.as_ref().to_owned()))
}
&syn::NestedMetaItem::MetaItem(syn::MetaItem::NameValue(ref ident, ref value)) => {
match value {
&syn::Lit::Str(ref value, _) => {
Some(Cfg::Named(ident.as_ref().to_owned(), value.clone()))
&syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. })) => {
match lit {
&syn::Lit::Str(ref value) => {
Some(Cfg::Named(ident.as_ref().to_owned(), value.value()))
}
_ => None,
}
}
&syn::NestedMetaItem::MetaItem(syn::MetaItem::List(ref ident, ref nested)) => {
&syn::NestedMeta::Meta(syn::Meta::List(syn::MetaList { ref ident, ref nested, .. })) => {
match ident.as_ref() {
"any" => if let Some(configs) = Cfg::load_list(nested) {
"any" => if let Some(configs) = Cfg::load_list(nested.iter()) {
Some(Cfg::Any(configs))
} else {
None
},
"all" => if let Some(configs) = Cfg::load_list(nested) {
"all" => if let Some(configs) = Cfg::load_list(nested.iter()) {
Some(Cfg::All(configs))
} else {
None
......@@ -145,11 +144,7 @@ impl Cfg {
}
}
fn load_list(attrs: &Vec<syn::NestedMetaItem>) -> Option<Vec<Cfg>> {
if attrs.len() == 0 {
return None;
}
fn load_list<'a, I: Iterator<Item=&'a syn::NestedMeta>>(attrs: I) -> Option<Vec<Cfg>> {
let mut configs = Vec::new();
for attr in attrs {
......@@ -160,7 +155,11 @@ impl Cfg {
}
}
Some(configs)
if configs.is_empty() {
None
} else {
Some(configs)
}
}
fn has_defines(&self, config: &Config) -> bool {
......
......@@ -16,34 +16,36 @@ pub struct LiteralExpr(String);
impl LiteralExpr {
pub fn load(expr: &syn::Expr) -> Result<LiteralExpr, String> {
match &expr.node {
&syn::ExprKind::Lit(syn::Lit::Str(ref text, ..)) => {
Ok(LiteralExpr(format!("u8\"{}\"", text)))
match expr {
&syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(ref value), .. }) => {
Ok(LiteralExpr(format!("u8\"{}\"", value.value())))
}
&syn::ExprKind::Lit(syn::Lit::Byte(value)) => Ok(LiteralExpr(format!("{}", value))),
&syn::ExprKind::Lit(syn::Lit::Char(value)) => Ok(LiteralExpr(format!("{}", value))),
&syn::ExprKind::Lit(syn::Lit::Int(value, ref ty)) => match ty {
&syn::IntTy::Usize
| &syn::IntTy::U8
| &syn::IntTy::U16
| &syn::IntTy::U32
| &syn::IntTy::U64
| &syn::IntTy::Unsuffixed => Ok(LiteralExpr(format!("{}", value))),
&syn::IntTy::Isize
| &syn::IntTy::I8
| &syn::IntTy::I16
| &syn::IntTy::I32
| &syn::IntTy::I64 => unsafe {
&syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Byte(ref value), .. }) => Ok(LiteralExpr(format!("{}", value.value()))),
&syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Char(ref value), .. }) => Ok(LiteralExpr(format!("{}", value.value()))),
&syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref value), .. }) => match value.suffix() {
syn::IntSuffix::Usize
| syn::IntSuffix::U8
| syn::IntSuffix::U16
| syn::IntSuffix::U32
| syn::IntSuffix::U64
| syn::IntSuffix::U128
| syn::IntSuffix::None => Ok(LiteralExpr(format!("{}", value.value()))),
syn::IntSuffix::Isize
| syn::IntSuffix::I8
| syn::IntSuffix::I16
| syn::IntSuffix::I32
| syn::IntSuffix::I64
| syn::IntSuffix::I128 => unsafe {
Ok(LiteralExpr(format!(
"{}",
mem::transmute::<u64, i64>(value)
mem::transmute::<u64, i64>(value.value())
)))
},
},
&syn::ExprKind::Lit(syn::Lit::Float(ref value, ref _ty)) => {
Ok(LiteralExpr(format!("{}", value)))
&syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Float(ref value), .. }) => {
Ok(LiteralExpr(format!("{}", value.value())))
}
&syn::ExprKind::Lit(syn::Lit::Bool(value)) => Ok(LiteralExpr(format!("{}", value))),
&syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Bool(ref value), .. }) => Ok(LiteralExpr(format!("{}", value.value))),
_ => Err("Unsupported literal expression.".to_owned()),
}
}
......@@ -62,12 +64,10 @@ pub struct Constant {
impl Constant {
pub fn load(
name: String,
ty: &syn::Ty,
expr: &syn::Expr,
attrs: &Vec<syn::Attribute>,
item: &syn::ItemConst,
mod_cfg: &Option<Cfg>,
) -> Result<Constant, String> {
let ty = Type::load(ty)?;
let ty = Type::load(&item.ty)?;
if ty.is_none() {
return Err("Cannot have a zero sized const definition.".to_owned());
......@@ -82,10 +82,10 @@ impl Constant {
Ok(Constant {
name: name,
ty: ty,
value: LiteralExpr::load(expr)?,
cfg: Cfg::append(mod_cfg, Cfg::load(attrs)),
annotations: AnnotationSet::load(attrs)?,
documentation: Documentation::load(attrs),
value: LiteralExpr::load(&item.expr)?,
cfg: Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
annotations: AnnotationSet::load(&item.attrs)?,
documentation: Documentation::load(&item.attrs),
})
}
}
......
......@@ -15,7 +15,7 @@ pub struct Documentation {
}
impl Documentation {
pub fn load(attrs: &Vec<syn::Attribute>) -> Self {
pub fn load(attrs: &[syn::Attribute]) -> Self {
let mut doc = Vec::new();
for attr in attrs {
......@@ -26,9 +26,12 @@ impl Documentation {
// step through rust. In that case they are stored as doc
// attributes and the leading three slashes (and optional space)
// are not included.
if let syn::MetaItem::NameValue(ref name, syn::Lit::Str(ref comment, _)) =
attr.value
if let Some(syn::Meta::NameValue(syn::MetaNameValue{ident, lit: syn::Lit::Str(comment), ..})) =
attr.interpret_meta()
{
let name = ident.to_string();
let comment = comment.value();
if &*name == "doc" {
let line = if attr.is_sugared_doc {
comment
......
......@@ -29,59 +29,75 @@ impl EnumVariant {
variant: &syn::Variant,
mod_cfg: &Option<Cfg>,
) -> Result<Self, String> {
let discriminant = match variant.discriminant {
Some(syn::ConstExpr::Lit(syn::Lit::Int(i, _))) => Some(i),
Some(_) => {
let discriminant = match &variant.discriminant {
&Some((_, syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Int(ref lit), .. }))) => Some(lit.value()),
&Some(_) => {
return Err("Unsupported discriminant.".to_owned());
}
None => None,
&None => None,
};
let body = match variant.data {
syn::VariantData::Unit => None,
syn::VariantData::Struct(ref fields) | syn::VariantData::Tuple(ref fields) => {
fn parse_fields(
is_tagged: bool,
fields: &syn::punctuated::Punctuated<syn::Field, syn::token::Comma>
) -> Result<Vec<(String, Type, Documentation)>, String> {
let mut res = Vec::new();
if is_tagged {
res.push((
"tag".to_string(),
Type::Path(GenericPath {
name: "Tag".to_string(),
generics: vec![],
}),
Documentation::none(),
));
}
for (i, field) in fields.iter().enumerate() {
if let Some(ty) = Type::load(&field.ty)? {
res.push((
match field.ident {
Some(ref ident) => ident.to_string(),
None => i.to_string(),
},
ty,
Documentation::load(&field.attrs),
));
}
}
Ok(res)
}
let body = match variant.fields {
syn::Fields::Unit => None,
syn::Fields::Named(ref fields) => {
Some(Struct {
name: format!("{}_Body", variant.ident),
generic_params: GenericParams::default(),
fields: parse_fields(is_tagged, &fields.named)?,
is_tagged,
tuple_struct: false,
cfg: Cfg::append(mod_cfg, Cfg::load(&variant.attrs)),
annotations: AnnotationSet::load(&variant.attrs)?,
documentation: Documentation::none(),
})
}
syn::Fields::Unnamed(ref fields) => {
Some(Struct {
name: format!("{}_Body", variant.ident),
generic_params: GenericParams::default(),
fields: {
let mut res = Vec::new();
if is_tagged {
res.push((
"tag".to_string(),
Type::Path(GenericPath {
name: "Tag".to_string(),
generics: vec![],
}),
Documentation::none(),
));
}
for (i, field) in fields.iter().enumerate() {
if let Some(ty) = Type::load(&field.ty)? {
res.push((
match field.ident {
Some(ref ident) => ident.to_string(),
None => i.to_string(),
},
ty,
Documentation::load(&field.attrs),
));
}
}
res
},
fields: parse_fields(is_tagged, &fields.unnamed)?,
is_tagged,
tuple_struct: match variant.data {
syn::VariantData::Tuple(_) => true,
_ => false,
},
tuple_struct: true,
cfg: Cfg::append(mod_cfg, Cfg::load(&variant.attrs)),
annotations: AnnotationSet::load(&variant.attrs)?,
documentation: Documentation::none(),
})
}
};
Ok(EnumVariant {
name: variant.ident.to_string(),
discriminant,
......@@ -127,12 +143,10 @@ pub struct Enum {
impl Enum {
pub fn load(
name: String,
values: &Vec<syn::Variant>,
attrs: &Vec<syn::Attribute>,
item: &syn::ItemEnum,
mod_cfg: &Option<Cfg>,
) -> Result<Enum, String> {
let repr = Repr::load(attrs)?;
let repr = Repr::load(&item.attrs)?;
if repr == Repr::RUST {
return Err("Enum not marked with a valid repr(prim) or repr(C).".to_owned());
}
......@@ -140,13 +154,13 @@ impl Enum {
let mut variants = Vec::new();
let mut is_tagged = false;
for variant in values {
for variant in item.variants.iter() {
let variant = EnumVariant::load(repr.style == ReprStyle::Rust, variant, mod_cfg)?;
is_tagged = is_tagged || variant.body.is_some();
variants.push(variant);
}
let annotations = AnnotationSet::load(attrs)?;
let annotations = AnnotationSet::load(&item.attrs)?;
if let Some(names) = annotations.list("enum-trailing-values") {
for name in names {
......@@ -160,7 +174,7 @@ impl Enum {
}
Ok(Enum {
name,
name: item.ident.to_string(),
repr,
variants,
tag: if is_tagged {
......@@ -168,9 +182,9 @@ impl Enum {
} else {
None
},
cfg: Cfg::append(mod_cfg, Cfg::load(attrs)),
cfg: Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
annotations,
documentation: Documentation::load(attrs),
documentation: Documentation::load(&item.attrs),
})
}
}
......
......@@ -9,7 +9,7 @@ use syn;
use bindgen::cdecl;
use bindgen::config::{Config, Layout, Language};
use bindgen::dependencies::Dependencies;
use bindgen::ir::{AnnotationSet, Cfg, CfgWrite, Documentation, SynFnRetTyHelpers, Type};
use bindgen::ir::{AnnotationSet, Cfg, CfgWrite, Documentation, Type, PrimitiveType};
use bindgen::library::Library;
use bindgen::monomorph::Monomorphs;
use bindgen::rename::{IdentifierType, RenameRule};
......@@ -32,11 +32,22 @@ impl Function {
name: String,
decl: &syn::FnDecl,
extern_decl: bool,
attrs: &Vec<syn::Attribute>,
attrs: &[syn::Attribute],
mod_cfg: &Option<Cfg>,
) -> Result<Function, String> {
let args = decl.inputs.iter().try_skip_map(|x| x.as_ident_and_type())?;
let ret = decl.output.as_type()?;
let ret = match decl.output {
syn::ReturnType::Default => {
Type::Primitive(PrimitiveType::Void)
}
syn::ReturnType::Type(_, ref ty) => {
if let Some(x) = Type::load(ty)? {
x
} else {
Type::Primitive(PrimitiveType::Void)
}
}
};
Ok(Function {
name: name,
......@@ -181,7 +192,7 @@ pub trait SynFnArgHelpers {
impl SynFnArgHelpers for syn::FnArg {
fn as_ident_and_type(&self) -> Result<Option<(String, Type)>, String> {
match self {
&syn::FnArg::Captured(syn::Pat::Ident(_, ref ident, _), ref ty) => {
&syn::FnArg::Captured(syn::ArgCaptured { pat: syn::Pat::Ident(syn::PatIdent { ref ident, .. }), ref ty, .. }) => {
if let Some(x) = Type::load(ty)? {
Ok(Some((ident.to_string(), x)))
} else {
......
......@@ -13,9 +13,12 @@ impl GenericParams {
pub fn new(generics: &syn::Generics) -> Self {
GenericParams(
generics
.ty_params
.params
.iter()
.map(|x| x.ident.to_string())
.filter_map(|x| match x {
&syn::GenericParam::Type(syn::TypeParam { ref ident, .. }) => Some(ident.to_string()),
_ => None,
})
.collect::<Vec<_>>(),
)
}
......
......@@ -24,28 +24,22 @@ pub struct Static {
impl Static {
pub fn load(
name: String,
ty: &syn::Ty,
mutable: &syn::Mutability,
attrs: &Vec<syn::Attribute>,
item: &syn::ItemStatic,
mod_cfg: &Option<Cfg>,
) -> Result<Static, String> {
let ty = Type::load(ty)?;
let ty = Type::load(&item.ty)?;
if ty.is_none() {
return Err("Cannot have a zero sized static definition.".to_owned());
}
let ty = ty.unwrap();
let mutable = mutable == &syn::Mutability::Mutable;
Ok(Static {
name: name,
ty: ty,
mutable: mutable,
cfg: Cfg::append(mod_cfg, Cfg::load(attrs)),
annotations: AnnotationSet::load(attrs)?,
documentation: Documentation::load(attrs),
name: item.ident.to_string(),
ty: ty.unwrap(),
mutable: item.mutability.is_some(),
cfg: Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
annotations: AnnotationSet::load(&item.attrs)?,
documentation: Documentation::load(&item.attrs),
})
}
......
......@@ -25,21 +25,27 @@ impl GenericPath {
pub fn load(path: &syn::Path) -> Result<GenericPath, String> {
assert!(path.segments.len() > 0);
let last_segment = path.segments.last().unwrap();
let last_segment_token = path.segments.last().unwrap();
let last_segment = last_segment_token.value();
let name = last_segment.ident.to_string();
if name == "PhantomData" {
return Ok(GenericPath::new(name, Vec::new()));
}
let generics = match &last_segment.parameters {
&syn::PathParameters::AngleBracketed(ref d) => {
d.types.iter().try_skip_map(|x| Type::load(x))?
let generics = match &last_segment.arguments {
&syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { ref args, .. }) => {
args.iter().try_skip_map(|x| {
match *x {
&syn::GenericArgument::Type(ref x) => Type::load(x),
_ => { Err(String::new()) }
}
})?
}
&syn::PathParameters::Parenthesized(_) => {
&syn::PathArguments::Parenthesized(_) => {
return Err("Path contains parentheses.".to_owned());
}
_ => Vec::new()
};
Ok(GenericPath::new(name, generics))
......
......@@ -48,26 +48,27 @@ impl Repr {
pub fn load(attrs: &[syn::Attribute]) -> Result<Repr, String> {
let ids = attrs
.iter()
.filter_map(|attr| match *attr {
syn::Attribute {
style: syn::AttrStyle::Outer,
is_sugared_doc: false,
value: syn::MetaItem::List(ref id, ref nested),