use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::Read;
use std::path::{Path as FilePath, PathBuf as FilePathBuf};
use syn;
use bindgen::bitflags;
use bindgen::cargo::{Cargo, PackageRef};
use bindgen::config::MacroExpansionConfig;
use bindgen::error::Error;
use bindgen::ir::{
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemMap,
OpaqueItem, Path, Static, Struct, Type, Typedef, Union,
};
use bindgen::utilities::{SynAbiHelpers, SynItemHelpers};
const STD_CRATES: &'static [&'static str] = &[
"std",
"std_unicode",
"alloc",
"collections",
"core",
"proc_macro",
];
type ParseResult = Result<Parse, Error>;
pub fn parse_src(
src_file: &FilePath,
macro_expansion_config: &MacroExpansionConfig,
) -> ParseResult {
let mod_name = src_file.file_stem().unwrap().to_str().unwrap();
let mut context = Parser {
binding_crate_name: mod_name.to_owned(),
macro_expansion_config,
lib: None,
parse_deps: true,
include: None,
exclude: Vec::new(),
expand: Vec::new(),
expand_all_features: true,
expand_default_features: true,
expand_features: None,
parsed_crates: HashSet::new(),
cache_src: HashMap::new(),
cache_expanded_crate: HashMap::new(),
cfg_stack: Vec::new(),
out: Parse::new(),
};
let pkg_ref = PackageRef {
name: mod_name.to_owned(),
version: "0.0.0".to_owned(),
};
context.parse_mod(&pkg_ref, src_file)?;
Ok(context.out)
}
pub(crate) fn parse_lib(
lib: Cargo,
macro_expansion_config: &MacroExpansionConfig,
parse_deps: bool,
include: &Option<Vec<String>>,
exclude: &[String],
expand: &[String],
expand_all_features: bool,
expand_default_features: bool,
expand_features: &Option<Vec<String>>,
) -> ParseResult {
let mut context = Parser {
binding_crate_name: lib.binding_crate_name().to_owned(),
macro_expansion_config,
lib: Some(lib),
parse_deps: parse_deps,
include: include.clone(),
exclude: exclude.to_owned(),
expand: expand.to_owned(),
expand_all_features,
expand_default_features,
expand_features: expand_features.clone(),
parsed_crates: HashSet::new(),
cache_src: HashMap::new(),
cache_expanded_crate: HashMap::new(),
cfg_stack: Vec::new(),
out: Parse::new(),
};
let binding_crate = context.lib.as_ref().unwrap().binding_crate_ref();
context.parse_crate(&binding_crate)?;
Ok(context.out)
}
#[derive(Debug, Clone)]
struct Parser<'a> {
binding_crate_name: String,
macro_expansion_config: &'a MacroExpansionConfig,
lib: Option<Cargo>,
parse_deps: bool,
include: Option<Vec<String>>,
exclude: Vec<String>,
expand: Vec<String>,
expand_all_features: bool,
expand_default_features: bool,
expand_features: Option<Vec<String>>,
parsed_crates: HashSet<String>,
cache_src: HashMap<FilePathBuf, Vec<syn::Item>>,
cache_expanded_crate: HashMap<String, Vec<syn::Item>>,
cfg_stack: Vec<Cfg>,
out: Parse,
}
impl<'a> Parser<'a> {
fn should_parse_dependency(&self, pkg_name: &String) -> bool {
if self.parsed_crates.contains(pkg_name) {
return false;
}
if !self.parse_deps {
return false;
}
if self.expand.contains(&pkg_name) {
return true;
}
if let Some(ref include) = self.include {
if !include.contains(&pkg_name) {
return false;
}
}
return !STD_CRATES.contains(&pkg_name.as_ref()) && !self.exclude.contains(&pkg_name);
}
fn parse_crate(&mut self, pkg: &PackageRef) -> Result<(), Error> {
assert!(self.lib.is_some());
self.parsed_crates.insert(pkg.name.clone());
if self.expand.contains(&pkg.name) {
return self.parse_expand_crate(pkg);
}
let crate_src = self.lib.as_ref().unwrap().find_crate_src(pkg);
match crate_src {
Some(crate_src) => self.parse_mod(pkg, crate_src.as_path()),
None => {
warn!(
"Parsing crate `{}`: can't find lib.rs with `cargo metadata`.",
pkg.name
);
Ok(())
}
}
}
fn parse_expand_crate(&mut self, pkg: &PackageRef) -> Result<(), Error> {
assert!(self.lib.is_some());
let mod_parsed = {
if !self.cache_expanded_crate.contains_key(&pkg.name) {
let s = self
.lib
.as_ref()
.unwrap()
.expand_crate(
pkg,
self.expand_all_features,
self.expand_default_features,
&self.expand_features,
)
.map_err(|x| Error::CargoExpand(pkg.name.clone(), x))?;
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
crate_name: pkg.name.clone(),
src_path: "".to_owned(),
error: x,
})?;
self.cache_expanded_crate.insert(pkg.name.clone(), i.items);
}
self.cache_expanded_crate.get(&pkg.name).unwrap().clone()
};
self.process_expanded_mod(pkg, &mod_parsed)
}
fn process_expanded_mod(&mut self, pkg: &PackageRef, items: &[syn::Item]) -> Result<(), Error> {
self.out.load_syn_crate_mod(
&self.macro_expansion_config,
&self.binding_crate_name,
&pkg.name,
Cfg::join(&self.cfg_stack).as_ref(),
items,
);
for item in items {
if item.has_test_attr() {
continue;
}
match *item {
syn::Item::Mod(ref item) => {
let cfg = Cfg::load(&item.attrs);
if let &Some(ref cfg) = &cfg {
self.cfg_stack.push(cfg.clone());
}
if let Some((_, ref inline_items)) = item.content {
self.process_expanded_mod(pkg, inline_items)?;
} else {
unreachable!();
}
if cfg.is_some() {
self.cfg_stack.pop();
}
}
syn::Item::ExternCrate(ref item) => {
let dep_pkg_name = item.ident.to_string();
let cfg = Cfg::load(&item.attrs);
if let &Some(ref cfg) = &cfg {
self.cfg_stack.push(cfg.clone());
}
if self.should_parse_dependency(&dep_pkg_name) {
if self.lib.is_some() {
let dep_pkg_ref =
self.lib.as_ref().unwrap().find_dep_ref(pkg, &dep_pkg_name);
if let Some(dep_pkg_ref) = dep_pkg_ref {
self.parse_crate(&dep_pkg_ref)?;
} else {
error!(
"Parsing crate `{}`: can't find dependency version for `{}`.",
pkg.name, dep_pkg_name
);
}
} else {
error!(
"Parsing crate `{}`: cannot parse external crate `{}` because \
cbindgen is in single source mode. Consider specifying a crate \
directory instead of a source file.",
pkg.name, dep_pkg_name
);
}
}
if cfg.is_some() {
self.cfg_stack.pop();
}
}
_ => {}
}
}
Ok(())
}
fn parse_mod(&mut self, pkg: &PackageRef, mod_path: &FilePath) -> Result<(), Error> {
let mod_parsed = {
let owned_mod_path = mod_path.to_path_buf();
if !self.cache_src.contains_key(&owned_mod_path) {
let mut s = String::new();
let mut f = File::open(mod_path).map_err(|_| Error::ParseCannotOpenFile {
crate_name: pkg.name.clone(),
src_path: mod_path.to_str().unwrap().to_owned(),
})?;
f.read_to_string(&mut s)
.map_err(|_| Error::ParseCannotOpenFile {
crate_name: pkg.name.clone(),
src_path: mod_path.to_str().unwrap().to_owned(),
})?;
let i = syn::parse_file(&s).map_err(|x| Error::ParseSyntaxError {
crate_name: pkg.name.clone(),
src_path: owned_mod_path.to_string_lossy().into(),
error: x,
})?;
self.cache_src.insert(owned_mod_path.clone(), i.items);
}
self.cache_src.get(&owned_mod_path).unwrap().clone()
};
let mod_dir = mod_path.parent().unwrap();
self.process_mod(pkg, mod_dir, &mod_parsed)
}
fn process_mod(
&mut self,
pkg: &PackageRef,
mod_dir: &FilePath,
items: &[syn::Item],
) -> Result<(), Error> {
self.out.load_syn_crate_mod(
&self.macro_expansion_config,
&self.binding_crate_name,
&pkg.name,
Cfg::join(&self.cfg_stack).as_ref(),
items,
);
for item in items {
if item.has_test_attr() {
continue;
}
match *item {
syn::Item::Mod(ref item) => {
let next_mod_name = item.ident.to_string();
let cfg = Cfg::load(&item.attrs);
if let &Some(ref cfg) = &cfg {
self.cfg_stack.push(cfg.clone());
}
if let Some((_, ref inline_items)) = item.content {
self.process_mod(pkg, &mod_dir.join(&next_mod_name), inline_items)?;
} else {
let next_mod_path1 = mod_dir.join(next_mod_name.clone() + ".rs");
let next_mod_path2 = mod_dir.join(next_mod_name.clone()).join("mod.rs");
if next_mod_path1.exists() {
self.parse_mod(pkg, next_mod_path1.as_path())?;
} else if next_mod_path2.exists() {
self.parse_mod(pkg, next_mod_path2.as_path())?;
} else {
let mut path_attr_found = false;
for attr in &item.attrs {
match attr.interpret_meta() {
Some(syn::Meta::NameValue(syn::MetaNameValue {
ident,
lit,
..
})) => match lit {
syn::Lit::Str(ref path) if ident == "path" => {
path_attr_found = true;
self.parse_mod(pkg, &mod_dir.join(path.value()))?;
break;
}
_ => (),
},
_ => (),
}
}
if !path_attr_found {
warn!(
"Parsing crate `{}`: can't find mod {}`.",
pkg.name, next_mod_name
);
}
}
}
if cfg.is_some() {
self.cfg_stack.pop();
}
}
syn::Item::ExternCrate(ref item) => {
let dep_pkg_name = item.ident.to_string();
let cfg = Cfg::load(&item.attrs);
if let &Some(ref cfg) = &cfg {
self.cfg_stack.push(cfg.clone());
}
if self.should_parse_dependency(&dep_pkg_name) {
if self.lib.is_some() {
let dep_pkg_ref =
self.lib.as_ref().unwrap().find_dep_ref(pkg, &dep_pkg_name);
if let Some(dep_pkg_ref) = dep_pkg_ref {
self.parse_crate(&dep_pkg_ref)?;
} else {
error!(
"Parsing crate `{}`: can't find dependency version for `{}`.",
pkg.name, dep_pkg_name
);
}
} else {
error!(
"Parsing crate `{}`: cannot parse external crate `{}` because \
cbindgen is in single source mode. Consider specifying a crate \
directory instead of a source file.",
pkg.name, dep_pkg_name
);
}
}
if cfg.is_some() {
self.cfg_stack.pop();
}
}
_ => {}
}
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct Parse {
pub constants: ItemMap<Constant>,
pub globals: ItemMap<Static>,
pub enums: ItemMap<Enum>,
pub structs: ItemMap<Struct>,
pub unions: ItemMap<Union>,
pub opaque_items: ItemMap<OpaqueItem>,
pub typedefs: ItemMap<Typedef>,
pub functions: Vec<Function>,
}
impl Parse {
pub fn new() -> Parse {
Parse {
constants: ItemMap::new(),
globals: ItemMap::new(),
enums: ItemMap::new(),
structs: ItemMap::new(),
unions: ItemMap::new(),
opaque_items: ItemMap::new(),
typedefs: ItemMap::new(),
functions: Vec::new(),
}
}
pub fn add_std_types(&mut self) {
let mut add_opaque = |path: &str, generic_params: Vec<&str>| {
let path = Path::new(path);
let generic_params: Vec<_> = generic_params.into_iter().map(|s| Path::new(s)).collect();
self.opaque_items.try_insert(OpaqueItem::new(
path,
GenericParams(generic_params),
None,
AnnotationSet::new(),
Documentation::none(),
))
};
add_opaque("String", vec![]);
add_opaque("Box", vec!["T"]);
add_opaque("Rc", vec!["T"]);
add_opaque("Arc", vec!["T"]);
add_opaque("Result", vec!["T", "E"]);
add_opaque("Option", vec!["T"]);
add_opaque("NonNull", vec!["T"]);
add_opaque("Vec", vec!["T"]);
add_opaque("HashMap", vec!["K", "V"]);
add_opaque("BTreeMap", vec!["K", "V"]);
add_opaque("HashSet", vec!["T"]);
add_opaque("BTreeSet", vec!["T"]);
add_opaque("LinkedList", vec!["T"]);
add_opaque("VecDeque", vec!["T"]);
}
pub fn extend_with(&mut self, other: &Parse) {
self.constants.extend_with(&other.constants);
self.globals.extend_with(&other.globals);
self.enums.extend_with(&other.enums);
self.structs.extend_with(&other.structs);
self.unions.extend_with(&other.unions);
self.opaque_items.extend_with(&other.opaque_items);
self.typedefs.extend_with(&other.typedefs);
self.functions.extend_from_slice(&other.functions);
}
pub fn load_syn_crate_mod(
&mut self,
macro_expansion_config: &MacroExpansionConfig,
binding_crate_name: &str,
crate_name: &str,
mod_cfg: Option<&Cfg>,
items: &[syn::Item],
) {
let mut impls_with_assoc_consts = Vec::new();
for item in items {
if item.has_test_attr() {
continue;
}
match item {
syn::Item::ForeignMod(ref item) => {
self.load_syn_foreign_mod(binding_crate_name, crate_name, mod_cfg, item);
}
syn::Item::Fn(ref item) => {
self.load_syn_fn(binding_crate_name, crate_name, mod_cfg, item);
}
syn::Item::Const(ref item) => {
self.load_syn_const(binding_crate_name, crate_name, mod_cfg, item);
}
syn::Item::Static(ref item) => {
self.load_syn_static(binding_crate_name, crate_name, mod_cfg, item);
}
syn::Item::Struct(ref item) => {
self.load_syn_struct(crate_name, mod_cfg, item);
}
syn::Item::Union(ref item) => {
self.load_syn_union(crate_name, mod_cfg, item);
}
syn::Item::Enum(ref item) => {
self.load_syn_enum(crate_name, mod_cfg, item);
}
syn::Item::Type(ref item) => {
self.load_syn_ty(crate_name, mod_cfg, item);
}
syn::Item::Impl(ref item_impl) => {
let has_assoc_const = item_impl.items.iter().any(|item| match item {
syn::ImplItem::Const(_) => true,
_ => false,
});
if has_assoc_const {
impls_with_assoc_consts.push(item_impl);
}
}
syn::Item::Macro(ref item) => {
self.load_builtin_macro(macro_expansion_config, crate_name, mod_cfg, item)
}
_ => {}
}
}
for item_impl in impls_with_assoc_consts {
self.load_syn_assoc_consts_from_impl(crate_name, mod_cfg, item_impl)
}
}
fn load_syn_assoc_consts_from_impl(
&mut self,
crate_name: &str,
mod_cfg: Option<&Cfg>,
item_impl: &syn::ItemImpl,
) {
let associated_constants = item_impl.items.iter().filter_map(|item| match item {
syn::ImplItem::Const(ref associated_constant) => Some(associated_constant),
_ => None,
});
self.load_syn_assoc_consts(
crate_name,
mod_cfg,
&item_impl.self_ty,
associated_constants,
);
}
fn load_syn_foreign_mod(
&mut self,
binding_crate_name: &str,
crate_name: &str,
mod_cfg: Option<&Cfg>,
item: &syn::ItemForeignMod,
) {
if !item.abi.is_c() {
info!("Skip {} - (extern block must be extern C).", crate_name);
return;
}
for foreign_item in &item.items {
match *foreign_item {
syn::ForeignItem::Fn(ref function) => {
if crate_name != binding_crate_name {
info!(
"Skip {}::{} - (fn's outside of the binding crate are not used).",
crate_name, &function.ident
);
return;
}
let path = Path::new(function.ident.to_string());
match Function::load(path, &function.decl, true, &function.attrs, mod_cfg) {
Ok(func) => {
info!("Take {}::{}.", crate_name, &function.ident);
self.functions.push(func);
}
Err(msg) => {
error!(
"Cannot use fn {}::{} ({}).",
crate_name, &function.ident, msg
);
}
}
}
_ => {}
}
}
}
fn load_syn_fn(
&mut self,
binding_crate_name: &str,
crate_name: &str,
mod_cfg: Option<&Cfg>,
item: &syn::ItemFn,
) {
if crate_name != binding_crate_name {
info!(
"Skip {}::{} - (fn's outside of the binding crate are not used).",
crate_name, &item.ident
);
return;
}
if let syn::Visibility::Public(_) = item.vis {
if item.is_no_mangle() && (item.abi.is_omitted() || item.abi.is_c()) {
let path = Path::new(item.ident.to_string());
match Function::load(path, &item.decl, false, &item.attrs, mod_cfg) {
Ok(func) => {
info!("Take {}::{}.", crate_name, &item.ident);
self.functions.push(func);
}
Err(msg) => {
error!("Cannot use fn {}::{} ({}).", crate_name, &item.ident, msg);
}
}
return;
}
}
if let syn::Visibility::Public(_) = item.vis {
} else {
warn!("Skip {}::{} - (not `pub`).", crate_name, &item.ident);
}
if (item.abi.is_omitted() || item.abi.is_c()) && !item.is_no_mangle() {
warn!(
"Skip {}::{} - (`extern` but not `no_mangle`).",
crate_name, &item.ident
);
}
if item.abi.is_some() && !(item.abi.is_omitted() || item.abi.is_c()) {
warn!(
"Skip {}::{} - (non `extern \"C\"`).",
crate_name, &item.ident
);
}
}
fn load_syn_assoc_consts<'a, I>(
&mut self,
crate_name: &str,
mod_cfg: Option<&Cfg>,
impl_ty: &syn::Type,
items: I,
) where
I: IntoIterator<Item = &'a syn::ImplItemConst>,
{
let ty = match Type::load(impl_ty) {
Ok(ty) => ty,
Err(e) => {
warn!("Skipping associated constants for {:?}: {:?}", impl_ty, e);
return;
}
};
if ty.is_none() {
return;
}
let impl_path = ty.unwrap().get_root_path().unwrap();
for item in items.into_iter() {
let path = Path::new(item.ident.to_string());
match Constant::load(
path,
mod_cfg,
&item.ty,
&item.expr,
&item.attrs,
Some(impl_path.clone()),
) {
Ok(constant) => {
info!("Take {}::{}::{}.", crate_name, impl_path, &item.ident);
let mut any = false;
self.structs.for_items_mut(&impl_path, |item| {
any = true;
item.add_associated_constant(constant.clone());
});
if !any && !self.constants.try_insert(constant) {
error!(
"Conflicting name for constant {}::{}::{}.",
crate_name, impl_path, &item.ident,
);
}
}
Err(msg) => {
warn!("Skip {}::{} - ({})", crate_name, &item.ident, msg);
}
}
}
}
fn load_syn_const(
&mut self,
binding_crate_name: &str,
crate_name: &str,
mod_cfg: Option<&Cfg>,
item: &syn::ItemConst,
) {
if crate_name != binding_crate_name {
info!(
"Skip {}::{} - (const's outside of the binding crate are not used).",
crate_name, &item.ident
);
return;
}
let path = Path::new(item.ident.to_string());
match Constant::load(path, mod_cfg, &item.ty, &item.expr, &item.attrs, None) {
Ok(constant) => {
info!("Take {}::{}.", crate_name, &item.ident);
let full_name = constant.path.clone();
if !self.constants.try_insert(constant) {
error!("Conflicting name for constant {}", full_name);
}
}
Err(msg) => {
warn!("Skip {}::{} - ({})", crate_name, &item.ident, msg);
}
}
}
fn load_syn_static(
&mut self,
binding_crate_name: &str,
crate_name: &str,
mod_cfg: Option<&Cfg>,
item: &syn::ItemStatic,
) {
if crate_name != binding_crate_name {
info!(
"Skip {}::{} - (static's outside of the binding crate are not used).",
crate_name, &item.ident
);
return;
}
if let syn::Visibility::Public(_) = item.vis {
if item.is_no_mangle() {
match Static::load(item, mod_cfg) {
Ok(constant) => {
info!("Take {}::{}.", crate_name, &item.ident);
self.globals.try_insert(constant);
}
Err(msg) => {
warn!("Skip {}::{} - ({})", crate_name, &item.ident, msg);
}
}
}
}
if let syn::Visibility::Public(_) = item.vis {
} else {
warn!("Skip {}::{} - (not `pub`).", crate_name, &item.ident);
}
if !item.is_no_mangle() {
warn!("Skip {}::{} - (not `no_mangle`).", crate_name, &item.ident);
}
}
fn load_syn_struct(&mut self, crate_name: &str, mod_cfg: Option<&Cfg>, item: &syn::ItemStruct) {
match Struct::load(item, mod_cfg) {
Ok(st) => {
info!("Take {}::{}.", crate_name, &item.ident);
self.structs.try_insert(st);
}
Err(msg) => {
info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg);
let path = Path::new(item.ident.to_string());
self.opaque_items.try_insert(
OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(),
);
}
}
}
fn load_syn_union(&mut self, crate_name: &str, mod_cfg: Option<&Cfg>, item: &syn::ItemUnion) {
match Union::load(item, mod_cfg) {
Ok(st) => {
info!("Take {}::{}.", crate_name, &item.ident);
self.unions.try_insert(st);
}
Err(msg) => {
info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg);
let path = Path::new(item.ident.to_string());
self.opaque_items.try_insert(
OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(),
);
}
}
}
fn load_syn_enum(&mut self, crate_name: &str, mod_cfg: Option<&Cfg>, item: &syn::ItemEnum) {
if item.generics.lifetimes().count() > 0 {
info!(
"Skip {}::{} - (has generics or lifetimes or where bounds).",
crate_name, &item.ident
);
return;
}
match Enum::load(item, mod_cfg) {
Ok(en) => {
info!("Take {}::{}.", crate_name, &item.ident);
self.enums.try_insert(en);
}
Err(msg) => {
info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg);
let path = Path::new(item.ident.to_string());
self.opaque_items.try_insert(
OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(),
);
}
}
}
fn load_syn_ty(&mut self, crate_name: &str, mod_cfg: Option<&Cfg>, item: &syn::ItemType) {
match Typedef::load(item, mod_cfg) {
Ok(st) => {
info!("Take {}::{}.", crate_name, &item.ident);
self.typedefs.try_insert(st);
}
Err(msg) => {
info!("Take {}::{} - opaque ({}).", crate_name, &item.ident, msg);
let path = Path::new(item.ident.to_string());
self.opaque_items.try_insert(
OpaqueItem::load(path, &item.generics, &item.attrs, mod_cfg).unwrap(),
);
}
}
}
fn load_builtin_macro(
&mut self,
macro_expansion_config: &MacroExpansionConfig,
crate_name: &str,
mod_cfg: Option<&Cfg>,
item: &syn::ItemMacro,
) {
let name = match item.mac.path.segments.last() {
Some(ref n) => n.value().ident.to_string(),
None => return,
};
if name != "bitflags" || !macro_expansion_config.bitflags {
return;
}
let bitflags = match bitflags::parse(item.mac.tts.clone()) {
Ok(b) => b,
Err(e) => {
warn!("Failed to parse bitflags invocation: {:?}", e);
return;
}
};
let (struct_, impl_) = bitflags.expand();
self.load_syn_struct(crate_name, mod_cfg, &struct_);
self.load_syn_assoc_consts_from_impl(crate_name, mod_cfg, &impl_);
}
}