mod metadata;
pub(crate) mod wire_access;
pub(crate) use std::{
collections::{BTreeMap, BTreeSet, HashMap},
path::{Path, PathBuf},
sync::Arc,
};
pub(crate) use kotlin_codegen as kt;
use kotlin_codegen::WriteKotlinError;
pub(crate) use metadata::{FoldStrategy, KotlinMeta, NullableKind, Projection, ProjectionKind};
pub use prebindgen_jni_runtime::{
box_jboolean, box_jbyte, box_jchar, box_jdouble, box_jfloat, box_jint, box_jlong, box_jshort,
decode_byte_array, decode_string, encode_byte_array, encode_string, null_byte_array,
null_string, CachedIfaceMethod, JniBindingError,
};
pub(crate) use prebindgen_registry::{
flat::Origin,
types_util::{option_inner_type, vec_inner_type},
ConverterImpl, Direction, NicheSlot, Niches, Prebindgen, Registry, ScalarValue, Stage, TypeKey,
};
pub(crate) use proc_macro2::{Span, TokenStream};
pub(crate) use quote::{format_ident, quote, ToTokens};
pub(crate) use crate::{
jni::wire_access::{box_descriptor_for_primitive, box_helper_for_wire, jni_field_access},
util::snake_to_camel,
};
#[derive(Clone, Default)]
pub(crate) struct OpaqueConfig {
pub gc_managed: bool,
}
#[derive(Clone, Default)]
pub(crate) struct EnumConfig {}
#[derive(Clone, Default)]
pub(crate) struct SumConfig {
pub variant_names: HashMap<String, String>,
}
#[derive(Clone, Debug)]
pub struct FunctionEntry {
pub rust_ident: syn::Ident,
pub kotlin_name_override: Option<String>,
}
impl FunctionEntry {
pub fn new(rust_ident: syn::Ident) -> Self {
Self {
rust_ident,
kotlin_name_override: None,
}
}
}
#[derive(Clone)]
pub(crate) enum DeclaredKind {
Ptr(OpaqueConfig),
Enum(EnumConfig),
Sealed(SumConfig),
Data,
}
#[derive(Clone)]
pub(crate) struct TypeConfig {
pub kind: DeclaredKind,
pub rust_type: Origin<syn::Type>,
pub name_spec: Option<NameSpec>,
pub jobject_input: bool,
pub interface_enabled: bool,
pub interface_name_override: Option<String>,
pub interfaces: Vec<String>,
}
impl TypeConfig {
pub(crate) fn new(
kind: DeclaredKind,
name_spec: NameSpec,
rust_type: Origin<syn::Type>,
) -> Self {
Self {
kind,
rust_type,
name_spec: Some(name_spec),
jobject_input: false,
interface_enabled: false,
interface_name_override: None,
interfaces: Vec::new(),
}
}
pub(crate) fn opaque(&self) -> Option<&OpaqueConfig> {
match &self.kind {
DeclaredKind::Ptr(o) => Some(o),
_ => None,
}
}
pub(crate) fn is_opaque(&self) -> bool {
matches!(self.kind, DeclaredKind::Ptr(_))
}
pub(crate) fn is_enum_class(&self) -> bool {
matches!(self.kind, DeclaredKind::Enum(_))
}
pub(crate) fn sum(&self) -> Option<&SumConfig> {
match &self.kind {
DeclaredKind::Sealed(s) => Some(s),
_ => None,
}
}
}
#[derive(Clone, Default)]
pub(crate) struct PackageConfig {
pub functions: Vec<FunctionEntry>,
pub constants: Vec<FunctionEntry>,
pub constant_functions: Vec<FunctionEntry>,
pub constant_exprs: Vec<super::jni::decl::ConstExprDecl>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum MemberKind {
Method,
Constructor,
}
#[derive(Clone, Debug)]
pub(crate) struct ClassMember {
pub rust_ident: syn::Ident,
pub kotlin_name_override: Option<String>,
pub kind: MemberKind,
}
pub(crate) type NameMangle = Arc<dyn Fn(&str, &str) -> String + Send + Sync>;
pub(crate) type HarnessNameMangle = Arc<dyn Fn(&str) -> String + Send + Sync>;
pub(crate) type MethodNameMangle = Arc<dyn Fn(&str, &str, &str) -> String + Send + Sync>;
pub struct JniGen {
decls: Declarations,
registry: prebindgen_registry::Registry<KotlinMeta>,
}
impl std::fmt::Debug for JniGen {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("JniGen(..)")
}
}
impl JniGen {
pub fn builder() -> JniGenBuilder {
JniGenBuilder::new()
}
pub fn write_rust(
&self,
out_path: impl AsRef<std::path::Path>,
) -> Result<std::path::PathBuf, prebindgen_registry::WriteRustError> {
Ok(prebindgen_registry::write::write_rust(
&self.registry,
&self.decls,
out_path,
)?)
}
pub fn registry(&self) -> &prebindgen_registry::Registry<KotlinMeta> {
&self.registry
}
pub fn declarations(&self) -> &Declarations {
&self.decls
}
}
#[derive(Clone)]
pub struct Declarations {
pub(crate) package: String,
pub(crate) fun_name_mangle: Option<NameMangle>,
pub(crate) ptr_class_name_mangle: Option<NameMangle>,
pub(crate) data_class_name_mangle: Option<NameMangle>,
pub(crate) enum_name_mangle: Option<NameMangle>,
pub(crate) method_name_mangle: Option<MethodNameMangle>,
pub(crate) harness_name_mangle: Option<HarnessNameMangle>,
pub(crate) interface_name_mangle: Option<NameMangle>,
pub(crate) types: HashMap<TypeKey, TypeConfig>,
pub(crate) packages: BTreeMap<String, PackageConfig>,
pub(crate) convert_decls: Vec<ConvertDecl>,
pub(crate) emit_handle_locks: bool,
pub(crate) jni_native_init: Option<String>,
pub(crate) param_expand_decls: Vec<ExpandParamDecl>,
pub(crate) return_expand_decls: Vec<ExpandReturnDecl>,
pub(crate) fn_param_expands: Vec<(syn::Ident, String, ExpandParamDecl)>,
pub(crate) fn_return_expands: Vec<(syn::Ident, ExpandReturnDecl)>,
pub(crate) fn_split_params: Vec<(syn::Ident, String)>,
pub(crate) class_members: HashMap<TypeKey, Vec<ClassMember>>,
pub(crate) ignored_fns: std::collections::HashSet<syn::Ident>,
pub(crate) ignored_name_predicates: Vec<prebindgen_registry::NamePredicate>,
pub(crate) ignored_class_types: std::collections::HashSet<TypeKey>,
pub(crate) ignored_const_idents: std::collections::HashSet<syn::Ident>,
pub(crate) local_fns: Vec<(syn::Ident, syn::Path, syn::Signature)>,
pub(crate) iface_specs:
std::cell::RefCell<std::collections::BTreeMap<SpecKey, std::sync::Arc<IfaceSpec>>>,
pub(crate) fn_plans: std::cell::RefCell<HashMap<syn::Ident, std::rc::Rc<JniFunctionPlan>>>,
}
#[derive(Clone, Default)]
pub struct JniGenBuilder {
pub(crate) decls: Declarations,
pub(crate) sources: prebindgen_registry::flat::FlatBuilder,
}
mod builder;
mod classify;
mod config;
mod decl;
mod emit;
mod equality;
mod iface;
mod prim;
mod prim_array;
mod selector;
#[cfg(test)]
mod tests;
pub(crate) mod trait_impl;
mod fn_plan;
mod fold;
mod kotlin_emit;
mod overloads;
mod render;
mod report;
mod struct_plan;
mod symbol;
mod symbols;
pub(crate) use builder::*;
pub(crate) use classify::*;
pub(crate) use config::*;
pub use decl::*;
pub(crate) use emit::*;
pub(crate) use fn_plan::*;
pub(crate) use fold::*;
pub(crate) use iface::*;
pub(crate) use overloads::*;
pub(crate) use prim::*;
pub(crate) use render::*;
pub(crate) use struct_plan::*;
pub(crate) use symbols::*;