Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate methods of Default trait ; Automatically rename function names to avoid keyword conflict ; Improve parsing trait impl in third party crates #2150

Merged
merged 29 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::if_then_some;
use crate::library::codegen::generator::api_dart::spec_generator::base::*;
use crate::library::codegen::generator::api_dart::spec_generator::info::ApiDartGeneratorInfoTrait;
use crate::utils::basic_code::dart_header_code::DartHeaderCode;
use crate::utils::dart_keywords;
use convert_case::{Case, Casing};
use itertools::Itertools;

Expand Down Expand Up @@ -174,7 +175,6 @@ fn generate_api_method(
default_constructor_mode,
&api_dart_func,
&method_name,
config,
dart_class_name,
);

Expand Down Expand Up @@ -226,7 +226,6 @@ fn generate_signature(
default_constructor_mode: Option<MirFuncDefaultConstructorMode>,
api_dart_func: &ApiDartGeneratedFunction,
method_name: &str,
_config: &GenerateApiMethodConfig,
dart_class_name: &str,
) -> String {
let is_static_method = method_info.mode == MirFuncOwnerInfoMethodMode::Static;
Expand Down Expand Up @@ -269,11 +268,13 @@ fn generate_method_name(
if default_constructor_mode.is_some() {
"newInstance".to_owned()
} else {
(method_info
.actual_method_dart_name
.as_ref()
.unwrap_or(&method_info.actual_method_name))
.to_case(Case::Camel)
dart_keywords::escape(
method_info
.actual_method_dart_name
.as_ref()
.unwrap_or(&method_info.actual_method_name)
.to_case(Case::Camel),
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/codegen/ir/hir/flat/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl HirFlatFunction {
| HirFlatFunctionOwner::StructOrEnum {
trait_def_name: None,
..
} => (self.item_fn.vis()).map(|vis| matches!(vis, Visibility::Public(_))),
} => (self.item_fn.vis_raw()).map(|vis| matches!(vis, Visibility::Public(_))),
HirFlatFunctionOwner::TraitDef { .. }
| HirFlatFunctionOwner::StructOrEnum {
trait_def_name: Some(_),
Expand Down
4 changes: 3 additions & 1 deletion frb_codegen/src/library/codegen/ir/hir/misc/item_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ impl GeneralizedItemFn {
}
}

pub(crate) fn vis(&self) -> Option<&Visibility> {
/// NOTE: When it is `ImplItemFn`, but it is in a `impl SomeTrait for SomeType`,
/// it will always be `inherited` even if it should be public
pub(crate) fn vis_raw(&self) -> Option<&Visibility> {
match self {
Self::ItemFn(inner) => Some(&inner.vis),
Self::ImplItemFn(inner) => Some(&inner.vis),
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/codegen/ir/hir/misc/serializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(crate) fn serialize_generalized_item_fn<S: Serializer>(
s.serialize_str(&format!(
"GeneralizedItemFn(name={}, vis={:?}, attrs=[{}])",
x.name(),
x.vis(),
x.vis_raw(),
x.attrs().iter().map(ty_to_string).join(", "),
))
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::codegen::ir::hir::flat::function::HirFlatFunction;
use crate::codegen::ir::hir::flat::pack::HirFlatPack;
use crate::codegen::misc::SELF_CRATE_THIRD_PARTY_NAMESPACE;
use crate::codegen::parser::hir::internal_config::ParserHirInternalConfig;
use crate::utils::namespace::Namespace;
use itertools::Itertools;
use syn::Visibility;

pub(crate) fn transform(
mut pack: HirFlatPack,
Expand All @@ -18,7 +16,7 @@ fn filter_function(pack: &mut HirFlatPack, config: &ParserHirInternalConfig) {
pack.functions = (pack.functions.drain(..))
.filter(|f| {
is_interest_module(&f.namespace, config)
&& (f.namespace.crate_name().is_self_crate() || is_function_public(f))
&& (f.namespace.crate_name().is_self_crate() || f.is_public().unwrap_or(true))
})
.collect_vec();
}
Expand All @@ -27,10 +25,3 @@ fn is_interest_module(namespace: &Namespace, config: &ParserHirInternalConfig) -
(config.rust_input_namespace_pack).is_interest(namespace)
|| SELF_CRATE_THIRD_PARTY_NAMESPACE.is_prefix_of(namespace)
}

fn is_function_public(f: &HirFlatFunction) -> bool {
f.item_fn
.vis()
.map(|v| matches!(v, Visibility::Public(_)))
.unwrap_or(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::HashSet;

pub(crate) fn transform(mut pack: HirFlatPack) -> anyhow::Result<HirFlatPack> {
let good_trait_names: HashSet<String> =
pack.traits.iter().map(|t| t.name.name.clone()).collect();
(pack.traits.iter().map(|t| t.name.name.clone())).collect();

let (funcs, skips) = IrValueOrSkip::split(
(pack.functions.drain(..))
Expand Down Expand Up @@ -36,7 +36,9 @@ fn should_retain(f: &HirFlatFunction, good_trait_names: &HashSet<String>) -> boo
..
} = &f.owner
{
good_trait_names.contains(trait_def_name) || has_frb_attributes(f)
good_trait_names.contains(trait_def_name)
|| WHITELIST_TRAIT_NAMES.contains(&&**trait_def_name)
|| has_frb_attributes(f)
} else {
true
}
Expand All @@ -47,3 +49,5 @@ fn has_frb_attributes(f: &HirFlatFunction) -> bool {
let attrs = FrbAttributes::parse(f.item_fn.attrs()).unwrap();
!attrs.is_empty()
}

pub(crate) const WHITELIST_TRAIT_NAMES: [&str; 1] = ["Default"];
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::codegen::ir::mir::ty::trait_def::MirTypeTraitDef;
use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::ir::misc::skip::IrSkipReason::IgnoreBecauseOwnerTyShouldIgnore;
use crate::codegen::ir::misc::skip::{IrSkipReason, IrValueOrSkip};
use crate::codegen::parser::hir::flat::transformer::remove_not_defined_trait_transformer::WHITELIST_TRAIT_NAMES;
use crate::codegen::parser::mir::parser::attribute::FrbAttributes;
use crate::codegen::parser::mir::parser::function::real::{
is_struct_or_enum_or_opaque_from_them, FunctionParser,
Expand Down Expand Up @@ -44,7 +45,9 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
} else {
// If cannot find the trait and there is no things like `#[frb]` marker,
// we directly skip the function currently
if attributes.is_empty() {
if attributes.is_empty()
&& !WHITELIST_TRAIT_NAMES.contains(&&**trait_def_name)
{
return Ok(IrValueOrSkip::Skip(IgnoreBecauseParseOwnerCannotFindTrait));
} else {
None
Expand Down
Loading
Loading