use crate::conversion::error_reporter::report_any_error;
use crate::conversion::{
api::{FuncToConvert, UnanalyzedApi},
convert_error::ConvertErrorWithContext,
convert_error::ErrorContext,
parse::parse_bindgen::get_bindgen_original_name_annotation,
};
use crate::{
conversion::api::ApiDetail,
conversion::ConvertError,
types::{Namespace, QualifiedName},
};
use std::collections::{HashMap, HashSet};
use syn::{Block, Expr, ExprCall, ForeignItem, Ident, ImplItem, ItemImpl, Stmt, Type};
pub(crate) struct ParseForeignMod {
ns: Namespace,
funcs_to_convert: Vec<FuncToConvert>,
method_receivers: HashMap<Ident, QualifiedName>,
ignored_apis: Vec<UnanalyzedApi>,
}
impl ParseForeignMod {
pub(crate) fn new(ns: Namespace) -> Self {
Self {
ns,
funcs_to_convert: Vec::new(),
method_receivers: HashMap::new(),
ignored_apis: Vec::new(),
}
}
pub(crate) fn convert_foreign_mod_items(
&mut self,
foreign_mod_items: Vec<ForeignItem>,
virtual_this_type: Option<QualifiedName>,
) {
let mut extra_apis = Vec::new();
for i in foreign_mod_items {
report_any_error(&self.ns.clone(), &mut extra_apis, || {
self.parse_foreign_item(i, &virtual_this_type)
});
}
self.ignored_apis.append(&mut extra_apis);
}
fn parse_foreign_item(
&mut self,
i: ForeignItem,
virtual_this_type: &Option<QualifiedName>,
) -> Result<(), ConvertErrorWithContext> {
match i {
ForeignItem::Fn(item) => {
self.funcs_to_convert.push(FuncToConvert {
item,
virtual_this_type: virtual_this_type.clone(),
self_ty: None,
});
Ok(())
}
ForeignItem::Static(item) => Err(ConvertErrorWithContext(
ConvertError::StaticData(item.ident.to_string()),
Some(ErrorContext::Item(item.ident)),
)),
_ => Err(ConvertErrorWithContext(
ConvertError::UnexpectedForeignItem,
None,
)),
}
}
pub(crate) fn convert_impl_items(&mut self, imp: ItemImpl) {
let ty_id = match *imp.self_ty {
Type::Path(typ) => typ.path.segments.last().unwrap().ident.clone(),
_ => return,
};
for i in imp.items {
if let ImplItem::Method(itm) = i {
let effective_fun_name = if itm.sig.ident == "new" {
ty_id.clone()
} else {
match get_called_function(&itm.block) {
Some(id) => id.clone(),
None => itm.sig.ident,
}
};
self.method_receivers.insert(
effective_fun_name,
QualifiedName::new(&self.ns, ty_id.clone()),
);
}
}
}
pub(crate) fn finished(mut self, apis: &mut Vec<UnanalyzedApi>) {
apis.append(&mut self.ignored_apis);
while !self.funcs_to_convert.is_empty() {
let mut fun = self.funcs_to_convert.remove(0);
fun.self_ty = self.method_receivers.get(&fun.item.sig.ident).cloned();
apis.push(UnanalyzedApi {
name: QualifiedName::new(&self.ns, fun.item.sig.ident.clone()),
cpp_name: get_bindgen_original_name_annotation(&fun.item.attrs),
deps: HashSet::new(), detail: ApiDetail::Function {
fun: Box::new(fun),
analysis: (),
},
})
}
}
}
fn get_called_function(block: &Block) -> Option<&Ident> {
match block.stmts.first() {
Some(Stmt::Expr(Expr::Call(ExprCall { func, .. }))) => match **func {
Expr::Path(ref exp) => exp.path.segments.first().map(|ps| &ps.ident),
_ => None,
},
_ => None,
}
}
#[cfg(test)]
mod test {
use super::get_called_function;
use syn::parse_quote;
use syn::Block;
#[test]
fn test_get_called_function() {
let b: Block = parse_quote! {
{
call_foo()
}
};
assert_eq!(get_called_function(&b).unwrap().to_string(), "call_foo");
}
}