Skip to content

Commit

Permalink
Merge branch 'feat/12204' into feat/12204-temp1
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy committed Jun 12, 2024
2 parents 89ff866 + 725d18a commit 8d59e87
Show file tree
Hide file tree
Showing 132 changed files with 2,667 additions and 9,994 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::codegen::ir::mir::default::MirDefaultValue;
use crate::codegen::ir::mir::field::MirField;
use crate::codegen::ir::mir::ty::delegate::MirTypeDelegate;
use crate::codegen::ir::mir::ty::MirType;
use crate::utils::dart_keywords;
use crate::utils::dart_keywords::make_string_keyword_safe;
use convert_case::{Case, Casing};
use std::borrow::Cow;

Expand Down Expand Up @@ -61,7 +61,7 @@ fn default_value_to_dart_style(value: &str) -> String {
format!(
"{}.{}",
enum_name,
dart_keywords::escape(variant_name.to_case(Case::Camel))
make_string_keyword_safe(variant_name.to_string()).to_case(Case::Camel)
)
}
_ => value.to_string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {

fn generate_implements_exception(&self, variant: &MirEnumVariant) -> &str {
let has_backtrace = matches!(&variant.kind,
MirVariantKind::Struct(MirStruct {is_fields_named: true, fields, ..}) if fields.iter().any(|field| field.name.rust_style() == BACKTRACE_IDENT));
MirVariantKind::Struct(MirStruct {is_fields_named: true, fields, ..}) if fields.iter().any(|field| field.name.raw == BACKTRACE_IDENT));
if self.mir.is_exception && has_backtrace {
"@Implements<FrbBacktracedException>()"
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::codegen::generator::api_dart::spec_generator::misc::generate_dart_com
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirEnumVariant};
use crate::library::codegen::generator::api_dart::spec_generator::base::*;
use crate::utils::basic_code::dart_header_code::DartHeaderCode;
use crate::utils::dart_keywords;
use crate::utils::dart_keywords::make_string_keyword_safe;
use itertools::Itertools;

impl<'a> EnumRefApiDartGenerator<'a> {
Expand Down Expand Up @@ -41,7 +41,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {

fn generate_mode_simple_variant(&self, variant: &MirEnumVariant) -> String {
let variant_name = if self.context.config.dart_enums_style {
dart_keywords::escape(variant.name.dart_style())
make_string_keyword_safe(variant.name.dart_style())
} else {
variant.name.rust_style().to_string()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ fn compute_skips(mir_pack: &MirPack, namespace: &Namespace) -> Vec<MirSkip> {
.filter(|t| &t.namespace == namespace)
.map(|name| MirSkip {
name: name.clone(),
reason: MirSkipReason::IgnoreBecauseTypeNotUsedByPub,
reason: MirSkipReason::IgnoredTypeNotUsedByPub,
})
.collect_vec();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::codegen::ir::mir::ty::primitive::MirTypePrimitive;
impl<'a> WireDartCodecDcoGeneratorDecoderTrait for PrimitiveListWireDartCodecDcoGenerator<'a> {
fn generate_impl_decode_body(&self) -> String {
match &self.mir.primitive {
MirTypePrimitive::I64 => "return Int64List.from(raw);".into(),
MirTypePrimitive::U64 => "return Uint64List.from(raw);".into(),
MirTypePrimitive::I64 => "return dcoDecodeInt64List(raw);".into(),
MirTypePrimitive::U64 => "return dcoDecodeUint64List(raw);".into(),
_ => gen_decode_simple_type_cast(self.mir.clone().into(), self.context),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn generate_decode_statement(
)
}

fn get_variable_name(field: &MirFuncInput) -> String {
fn get_variable_name(field: &MirFuncInput) -> &str {
field.inner.name.rust_style()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a> WireRustGeneratorMiscTrait for StructRefWireRustGenerator<'a> {
.enumerate()
.map(|(i, field)| {
let field_access = if src.is_fields_named {
field.name.rust_style().to_owned()
field.name.raw.clone()
} else {
i.to_string()
};
Expand Down
47 changes: 15 additions & 32 deletions frb_codegen/src/library/codegen/ir/mir/ident.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
use crate::codegen::generator::codec::sse::lang::Lang;
use crate::utils::{cbindgen_keywords, dart_keywords};
use crate::utils::cbindgen_keywords;
use convert_case::{Case, Casing};

crate::mir! {
#[serde(transparent)]
pub struct MirIdent {
rust_style: String,
dart_style: Option<String>,
pub raw: String,
}
}

impl MirIdent {
pub fn new(rust_style: String, dart_style: Option<String>) -> MirIdent {
MirIdent {
rust_style,
dart_style,
}
pub fn new(raw: String) -> MirIdent {
MirIdent { raw }
}

pub fn rust_style(&self) -> String {
self.rust_style.clone()
pub fn rust_style(&self) -> &str {
&self.raw
}

pub fn c_style(&self) -> String {
convert_rust_to_c_style(&self.rust_style)
convert_rust_to_c_style(&self.raw)
}

pub fn dart_style(&self) -> String {
(self.dart_style.clone()).unwrap_or_else(|| convert_rust_to_dart_style(&self.rust_style))
(self.raw.strip_prefix("r#").unwrap_or(self.raw.as_str())).to_case(Case::Camel)
}

pub fn style(&self, lang: &Lang) -> String {
Expand All @@ -39,37 +35,24 @@ impl MirIdent {

impl std::fmt::Display for MirIdent {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
fmt.write_str(&self.rust_style)?;
if let Some(dart_style) = &self.dart_style {
write!(fmt, "(dart_style={})", dart_style)?;
}
Ok(())
fmt.write_str(&self.raw)
}
}

fn convert_rust_to_c_style(raw: &str) -> String {
let mut ans = strip_prefix_rhash(raw).to_owned();
let mut ans = raw.to_owned();

if let Some(stripped) = ans.strip_prefix("r#") {
ans = stripped.to_owned();
}

// match behavior of ffigen
if &ans == "async" {
ans = "async1".to_owned();
}
if &ans == "interface" {
ans = "interface1".to_owned();
}

// match behavior of cbindgen
cbindgen_keywords::escape(&mut ans);

ans
}

fn convert_rust_to_dart_style(raw: &str) -> String {
let ans = strip_prefix_rhash(raw).to_case(Case::Camel);

dart_keywords::escape(ans)
}

fn strip_prefix_rhash(raw: &str) -> &str {
raw.strip_prefix("r#").unwrap_or(raw)
}
35 changes: 15 additions & 20 deletions frb_codegen/src/library/codegen/ir/mir/skip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,32 @@ pub struct MirSkip {

#[derive(Copy, PartialOrd, Ord)]
pub(crate) enum MirSkipReason {
IgnoreBecauseFunctionNotPub,
IgnoreBecauseFunctionGeneric,
IgnoreBecauseTypeNotUsedByPub,
IgnoreBecauseExplicitAttribute,
IgnoreBecauseType,
IgnoreBecauseParseMethodOwnerTy,
IgnoreBecauseParseOwnerCannotFindTrait,
IgnoreBecauseNotAllowedOwner,
IgnoreBecauseOwnerTyShouldIgnore,
IgnoreSilently,
IgnoredFunctionNotPub,
IgnoredFunctionGeneric,
IgnoredTypeNotUsedByPub,
IgnoredMisc,
IgnoredSilently,
Err,
}
}

impl MirSkipReason {
pub(crate) fn explanation_prefix(&self) -> Option<String> {
pub(crate) fn explanation_prefix(&self) -> Option<&'static str> {
Some(match self {
Self::IgnoreBecauseFunctionNotPub => {
"These functions are ignored because they are not marked as `pub`".to_owned()
Self::IgnoredFunctionNotPub => {
"These functions are ignored because they are not marked as `pub`"
}
Self::IgnoreBecauseFunctionGeneric => {
"These functions are ignored because they have generic arguments".to_owned()
Self::IgnoredFunctionGeneric => {
"These functions are ignored because they have generic arguments"
}
Self::IgnoreBecauseTypeNotUsedByPub => {
"These types are ignored because they are not used by any `pub` functions".to_owned()
Self::IgnoredTypeNotUsedByPub => {
"These types are ignored because they are not used by any `pub` functions"
}
Self::IgnoreSilently => return None,
Self::IgnoredMisc => "These functions are ignored",
Self::IgnoredSilently => return None,
Self::Err => {
"These functions have error during generation (see debug logs or enable `stop_on_error: true` for more details)".to_owned()
"These functions have error during generation (see debug logs or enable `stop_on_error: true` for more details)"
}
_ => format!("These functions are ignored (category: {:?})", self)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::codegen::ir::hir::misc::item_fn::GeneralizedItemFn;
use crate::codegen::ir::hir::naive_flat::item::HirNaiveFlatItemMeta;
use crate::if_then_some;
use itertools::Itertools;
use syn::{Attribute, ImplItem, ImplItemFn, ItemImpl};
use syn::{ImplItem, ItemImpl};

pub(crate) fn parse_syn_item_impl(
target: &mut HirFlatPack,
Expand All @@ -29,8 +29,6 @@ fn parse_functions(
meta: &HirNaiveFlatItemMeta,
trait_def_name: &Option<String>,
) -> Vec<HirFlatFunction> {
let attrs_item_impl = item_impl.attrs;

(item_impl.items.into_iter())
.filter_map(|item| if_then_some!(let ImplItem::Fn(impl_item_fn) = item, impl_item_fn))
.map(|impl_item_fn| HirFlatFunction {
Expand All @@ -39,17 +37,12 @@ fn parse_functions(
impl_ty: *item_impl.self_ty.clone(),
trait_def_name: trait_def_name.clone(),
},
item_fn: GeneralizedItemFn::ImplItemFn(add_attrs(impl_item_fn, &attrs_item_impl)),
item_fn: GeneralizedItemFn::ImplItemFn(impl_item_fn),
sources: meta.sources.clone(),
})
.collect_vec()
}

fn add_attrs(mut item: ImplItemFn, attrs: &[Attribute]) -> ImplItemFn {
item.attrs.extend(attrs.to_owned());
item
}

fn parse_trait_impl(item_impl: &ItemImpl, trait_name: &str) -> HirFlatTraitImpl {
HirFlatTraitImpl {
trait_name: trait_name.to_owned(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ pub(super) fn parse_auto_accessor_of_field(
type_parser: &mut TypeParser,
context: &TypeParserParsingContext,
) -> anyhow::Result<MirFuncAndSanityCheckInfo> {
let rust_method_name = format!("{}_{}", accessor_mode.verb_str(), field.name.rust_style());
let rust_method_name = format!("{}_{}", accessor_mode.verb_str(), field.name.raw);

let owner = MirFuncOwnerInfoMethod {
owner_ty: ty_direct_parse.to_owned(),
actual_method_name: rust_method_name,
actual_method_dart_name: Some(field.name.rust_style().to_owned()),
actual_method_dart_name: Some(field.name.raw.clone()),
mode: MirFuncOwnerInfoMethodMode::Instance,
trait_def: None,
};
Expand All @@ -47,7 +47,7 @@ pub(super) fn parse_auto_accessor_of_field(
if accessor_mode == MirFuncAccessorMode::Setter {
inputs.push(MirFuncInput {
ownership_mode: None,
inner: create_mir_field(field.ty.clone(), &field.name.rust_style()),
inner: create_mir_field(field.ty.clone(), &field.name.raw),
});
}

Expand Down Expand Up @@ -124,15 +124,15 @@ fn compute_self_arg(
fn compute_src_lineno_pseudo(struct_name: &NamespacedName, field: &MirField) -> usize {
let mut hasher = Sha1::new();
hasher.update(struct_name.rust_style().as_bytes());
hasher.update(field.name.rust_style().as_bytes());
hasher.update(field.name.raw.as_bytes());
let digest = hasher.finalize();
usize::from_le_bytes(digest[..8].try_into().unwrap())
}

fn create_mir_field(ty: MirType, name: &str) -> MirField {
MirField {
ty,
name: MirIdent::new(name.to_owned(), None),
name: MirIdent::new(name.to_owned()),
is_final: true,
is_rust_public: None,
comments: vec![],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::codegen::ir::mir::field::{MirField, MirFieldSettings};
use crate::codegen::ir::mir::func::{MirFuncInput, MirFuncOwnerInfo};
use crate::codegen::ir::mir::func::{MirFuncOwnerInfoMethod, OwnershipMode};
use crate::codegen::ir::mir::ident::MirIdent;
use crate::codegen::ir::mir::skip::MirSkipReason;
use crate::codegen::ir::mir::ty::boxed::MirTypeBoxed;
use crate::codegen::ir::mir::ty::delegate::{MirTypeDelegate, MirTypeDelegateProxyEnum};
use crate::codegen::ir::mir::ty::MirType;
Expand Down Expand Up @@ -57,7 +56,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {

if ty.should_ignore(self.type_parser) {
return Ok(FunctionPartialInfo {
ignore_func: Some(MirSkipReason::IgnoreBecauseType),
ignore_func: true,
..Default::default()
});
}
Expand All @@ -70,7 +69,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
Ok(FunctionPartialInfo {
inputs: vec![MirFuncInput {
inner: MirField {
name: MirIdent::new(name, None),
name: MirIdent::new(name),
ty,
is_final: true,
is_rust_public: None,
Expand Down
Loading

0 comments on commit 8d59e87

Please sign in to comment.