Skip to content

Commit

Permalink
Merge pull request #2012 from fzyzcjy/feat/12118
Browse files Browse the repository at this point in the history
Automatically mark methods of non-pub structs as ignored
  • Loading branch information
fzyzcjy authored Jun 2, 2024
2 parents 517b75e + f2c24bd commit 7fcccba
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 17 deletions.
5 changes: 5 additions & 0 deletions frb_codegen/src/library/codegen/ir/mir/ty/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct MirEnum {
pub comments: Vec<MirComment>,
pub variants: Vec<MirVariant>,
pub mode: MirEnumMode,
pub ignore: bool,
}

#[derive(Copy)]
Expand Down Expand Up @@ -77,6 +78,10 @@ impl MirTypeTrait for MirTypeEnumRef {
fn self_namespace(&self) -> Option<Namespace> {
Some(self.ident.0.namespace.clone())
}

fn should_ignore(&self, mir_context: &impl MirContext) -> bool {
self.get(mir_context).ignore
}
}

impl MirEnum {
Expand Down
4 changes: 4 additions & 0 deletions frb_codegen/src/library/codegen/ir/mir/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ pub trait MirTypeTrait {
None
}

fn should_ignore(&self, _mir_context: &impl MirContext) -> bool {
false
}

// TODO move
fn cloned_getter_semantics_reasonable(&self) -> bool {
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct MirTypeRustAutoOpaqueImplicit {
pub ownership_mode: OwnershipMode,
pub inner: MirTypeRustOpaque,
pub raw: MirRustAutoOpaqueRaw,
pub ignore: bool,
}

/// Original type without any transformation
Expand Down Expand Up @@ -47,6 +48,10 @@ impl MirTypeTrait for MirTypeRustAutoOpaqueImplicit {
fn as_primitive(&self) -> Option<&MirTypePrimitive> {
Some(&RUST_OPAQUE_AS_PRIMITIVE)
}

fn should_ignore(&self, _mir_context: &impl MirContext) -> bool {
self.ignore
}
}

impl MirTypeRustAutoOpaqueImplicit {
Expand Down
4 changes: 4 additions & 0 deletions frb_codegen/src/library/codegen/ir/mir/ty/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl MirTypeTrait for MirTypeStructRef {
fn self_namespace(&self) -> Option<Namespace> {
Some(self.ident.0.namespace.clone())
}

fn should_ignore(&self, mir_context: &impl MirContext) -> bool {
self.get(mir_context).ignore
}
}

impl MirStruct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::codegen::parser::mir::type_parser::misc::parse_comments;
use crate::codegen::parser::mir::type_parser::rust_auto_opaque_implicit::split_ownership_from_ty;
use crate::codegen::parser::mir::type_parser::{TypeParser, TypeParserParsingContext};
use crate::if_then_some;
use crate::library::codegen::ir::mir::ty::MirTypeTrait;
use anyhow::Context;
use syn::*;

Expand Down Expand Up @@ -48,13 +49,11 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
ownership_mode_split,
)?;

if let MirType::StructRef(s) = &ty {
if s.get(self.type_parser).ignore {
return Ok(FunctionPartialInfo {
ignore_func: true,
..Default::default()
});
}
if ty.should_ignore(self.type_parser) {
return Ok(FunctionPartialInfo {
ignore_func: true,
..Default::default()
});
}

let attrs = parse_attrs_from_fn_arg(sig_input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
return Ok(None);
};

if owner_ty.should_ignore(self.type_parser) {
return Ok(None);
}

let actual_method_name = impl_item_fn.sig.ident.to_string();

MirFuncOwnerInfo::Method(MirFuncOwnerInfoMethod {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::codegen::ir::hir::hierarchical::module::HirVisibility;
use crate::codegen::ir::hir::hierarchical::struct_or_enum::HirStructOrEnum;
use crate::codegen::ir::hir::hierarchical::syn_item_struct_or_enum::SynItemStructOrEnum;
use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::parser::mir::attribute_parser::FrbAttributes;
use crate::codegen::parser::mir::type_parser::external_impl;
use crate::codegen::parser::mir::type_parser::misc::parse_type_should_ignore_simple;
use crate::codegen::parser::mir::type_parser::unencodable::SplayedSegment;
use crate::library::codegen::ir::mir::ty::MirTypeTrait;
use crate::utils::namespace::{Namespace, NamespacedName};
Expand Down Expand Up @@ -36,6 +38,7 @@ where

if let Some(src_object) = self.src_objects().get(&name) {
let src_object = (*src_object).clone();
let vis = src_object.visibility;

let namespace = &src_object.namespaced_name.namespace;
let namespaced_name = NamespacedName::new(namespace.clone(), name.clone());
Expand All @@ -44,7 +47,10 @@ where
let attrs_opaque = override_opaque.or(attrs.opaque());
if attrs_opaque == Some(true) {
debug!("Treat {name} as opaque since attribute says so");
return Ok(Some((self.parse_opaque(&namespaced_name)?, attrs)));
return Ok(Some((
self.parse_opaque(&namespaced_name, &attrs, vis)?,
attrs,
)));
}

let ident: Id = namespaced_name.clone().into();
Expand All @@ -64,7 +70,10 @@ where
.map_or(false, |obj| Self::compute_default_opaque(obj))
{
debug!("Treat {name} as opaque by compute_default_opaque");
return Ok(Some((self.parse_opaque(&namespaced_name)?, attrs)));
return Ok(Some((
self.parse_opaque(&namespaced_name, &attrs, vis)?,
attrs,
)));
}

return Ok(Some((self.construct_output(ident)?, attrs)));
Expand All @@ -91,10 +100,20 @@ where
}
}

fn parse_opaque(&mut self, namespaced_name: &NamespacedName) -> anyhow::Result<MirType> {
fn parse_opaque(
&mut self,
namespaced_name: &NamespacedName,
attrs: &FrbAttributes,
vis: HirVisibility,
) -> anyhow::Result<MirType> {
self.parse_type_rust_auto_opaque_implicit(
Some(namespaced_name.namespace.clone()),
&syn::parse_str(&namespaced_name.name)?,
Some(parse_type_should_ignore_simple(
attrs,
vis,
&namespaced_name.namespace.crate_name(),
)),
)
}

Expand All @@ -117,6 +136,7 @@ where
&mut self,
namespace: Option<Namespace>,
ty: &Type,
override_ignore: Option<bool>,
) -> anyhow::Result<MirType>;

fn compute_default_opaque(obj: &Obj) -> bool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use crate::codegen::parser::mir::attribute_parser::FrbAttributes;
use crate::codegen::parser::mir::type_parser::enum_or_struct::{
EnumOrStructParser, EnumOrStructParserInfo,
};
use crate::codegen::parser::mir::type_parser::misc::parse_comments;
use crate::codegen::parser::mir::type_parser::misc::{
parse_comments, parse_type_should_ignore_simple,
};
use crate::codegen::parser::mir::type_parser::structure::structure_compute_default_opaque;
use crate::codegen::parser::mir::type_parser::unencodable::SplayedSegment;
use crate::codegen::parser::mir::type_parser::TypeParserWithContext;
Expand All @@ -37,6 +39,8 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
name: NamespacedName,
wrapper_name: Option<String>,
) -> anyhow::Result<MirEnum> {
let attributes = FrbAttributes::parse(&src_enum.src.attrs)?;

let comments = parse_comments(&src_enum.src.attrs);
let raw_variants = src_enum
.src
Expand All @@ -47,13 +51,19 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {

let mode = compute_enum_mode(&raw_variants);
let variants = maybe_field_wrap_box(raw_variants, mode);
let ignore = parse_type_should_ignore_simple(
&attributes,
src_enum.visibility,
&name.namespace.crate_name(),
);

Ok(MirEnum {
name,
wrapper_name,
comments,
variants,
mode,
ignore,
})
}

Expand Down Expand Up @@ -177,8 +187,10 @@ impl EnumOrStructParser<MirEnumIdent, MirEnum, ItemEnum>
&mut self,
namespace: Option<Namespace>,
ty: &Type,
override_ignore: Option<bool>,
) -> anyhow::Result<MirType> {
self.0.parse_type_rust_auto_opaque_implicit(namespace, ty)
self.0
.parse_type_rust_auto_opaque_implicit(namespace, ty, override_ignore)
}

fn compute_default_opaque(obj: &MirEnum) -> bool {
Expand Down
14 changes: 14 additions & 0 deletions frb_codegen/src/library/codegen/parser/mir/type_parser/misc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::codegen::ir::hir::hierarchical::module::HirVisibility;
use crate::codegen::ir::mir::comment::MirComment;
use crate::codegen::parser::mir::attribute_parser::FrbAttributes;
use crate::utils::crate_name::CrateName;
use itertools::Itertools;
use syn::*;

Expand Down Expand Up @@ -45,3 +48,14 @@ fn parse_comment(input: &str) -> MirComment {
format!("///{input}")
})
}

pub(crate) fn parse_type_should_ignore_simple(
attrs: &FrbAttributes,
vis: HirVisibility,
crate_name: &CrateName,
) -> bool {
attrs.ignore()
// For third party crates, if a struct is not public, then it is impossible to utilize it,
// thus we ignore it.
|| (crate_name != &CrateName::self_crate() && vis != HirVisibility::Public)
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
}
// frb-coverage:ignore-end

self.parse_type_rust_auto_opaque_implicit(None, &syn::Type::Path(type_path.to_owned()))
self.parse_type_rust_auto_opaque_implicit(
None,
&syn::Type::Path(type_path.to_owned()),
None,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
&mut self,
namespace: Option<Namespace>,
ty: &Type,
override_ignore: Option<bool>,
) -> Result<MirType> {
let (inner, ownership_mode) = split_ownership_from_ty(ty);
let (ans_raw, ans_inner) =
Expand All @@ -31,6 +32,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
ownership_mode,
raw: ans_raw,
inner: ans_inner,
ignore: override_ignore.unwrap_or(false),
}))
}

Expand Down Expand Up @@ -93,6 +95,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
self.parse_type_rust_auto_opaque_implicit(
ty_raw.self_namespace(),
&syn::parse_str(&transform(&ty_raw.raw.string))?,
None,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::codegen::parser::mir::attribute_parser::FrbAttributes;
use crate::codegen::parser::mir::type_parser::enum_or_struct::{
EnumOrStructParser, EnumOrStructParserInfo,
};
use crate::codegen::parser::mir::type_parser::misc::parse_comments;
use crate::codegen::parser::mir::type_parser::misc::{
parse_comments, parse_type_should_ignore_simple,
};
use crate::codegen::parser::mir::type_parser::unencodable::SplayedSegment;
use crate::codegen::parser::mir::type_parser::TypeParserWithContext;
use crate::utils::namespace::{Namespace, NamespacedName};
Expand Down Expand Up @@ -51,13 +53,19 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
let attributes = FrbAttributes::parse(&src_struct.src.attrs)?;
let dart_metadata = attributes.dart_metadata();

let ignore = parse_type_should_ignore_simple(
&attributes,
src_struct.visibility,
&name.namespace.crate_name(),
);

Ok(MirStruct {
name,
wrapper_name,
fields,
is_fields_named,
dart_metadata,
ignore: attributes.ignore(),
ignore,
generate_hash: attributes.generate_hash(),
generate_eq: attributes.generate_eq(),
comments,
Expand Down Expand Up @@ -120,8 +128,10 @@ impl EnumOrStructParser<MirStructIdent, MirStruct, ItemStruct>
&mut self,
namespace: Option<Namespace>,
ty: &Type,
override_ignore: Option<bool>,
) -> anyhow::Result<MirType> {
self.0.parse_type_rust_auto_opaque_implicit(namespace, ty)
self.0
.parse_type_rust_auto_opaque_implicit(namespace, ty, override_ignore)
}

fn compute_default_opaque(obj: &MirStruct) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
Type::ImplTrait(type_impl_trait) => self
.parse_type_impl_trait_dart_fn(&type_impl_trait)
.context("when trying to parse DartFn")?,
_ => self.parse_type_rust_auto_opaque_implicit(None, &resolve_ty)?,
_ => self.parse_type_rust_auto_opaque_implicit(None, &resolve_ty, None)?,
})
}

Expand Down
4 changes: 4 additions & 0 deletions frb_codegen/src/library/utils/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl Namespace {
// Ok(Self::new_self_crate(p))
// }

pub fn crate_name(&self) -> CrateName {
CrateName::new(self.path()[0].to_owned())
}

pub fn path(&self) -> Vec<&str> {
self.joined_path.split(Self::SEP).collect()
}
Expand Down
Loading

0 comments on commit 7fcccba

Please sign in to comment.