Skip to content

Commit

Permalink
Merge pull request #2096 from fzyzcjy/feat/12316
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy authored Jun 18, 2024
2 parents 24c02ff + 07cb941 commit a8db0b6
Show file tree
Hide file tree
Showing 60 changed files with 9,262 additions and 7,014 deletions.
4 changes: 4 additions & 0 deletions frb_codegen/src/binary/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ pub(crate) struct GenerateCommandArgsPrimary {
#[arg(long)]
pub enable_lifetime: bool,

/// Let 64 bit types be translated to `int`s instead of types like `BigInt`s
#[arg(long)]
pub type_64bit_int: bool,

/// If having error when, for example, parsing a function, directly stop instead of continue and skip it
#[arg(long)]
pub stop_on_error: bool,
Expand Down
1 change: 1 addition & 0 deletions frb_codegen/src/binary/commands_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn compute_codegen_config_from_naive_command_args(
default_external_library_loader_web_prefix: args.default_external_library_loader_web_prefix,
dart_type_rename: None, // complex type, not supported on command line yet
enable_lifetime: Some(args.enable_lifetime),
type_64bit_int: Some(args.type_64bit_int),
stop_on_error: Some(args.stop_on_error),
dump: args.dump,
dump_all: Some(args.dump_all),
Expand Down
1 change: 1 addition & 0 deletions frb_codegen/src/library/codegen/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Config {
pub default_external_library_loader_web_prefix: Option<String>,
pub dart_type_rename: Option<HashMap<String, String>>,
pub enable_lifetime: Option<bool>,
pub type_64bit_int: Option<bool>,
pub stop_on_error: Option<bool>,
pub dump: Option<Vec<ConfigDumpContent>>,
pub dump_all: Option<bool>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ impl InternalConfig {
default_rust_opaque_codec,
stop_on_error,
enable_lifetime: config.enable_lifetime.unwrap_or_default(),
type_64bit_int: config.type_64bit_int.unwrap_or_default(),
},
},
generator,
Expand Down
2 changes: 1 addition & 1 deletion frb_codegen/src/library/codegen/ir/mir/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl DistinctTypeGatherer {
self.ans
.into_iter()
// make the output change less when input change
.sorted_by_key(|ty| ty.safe_ident())
.sorted_by_cached_key(|ty| ty.safe_ident())
.collect()
}
}
1 change: 1 addition & 0 deletions frb_codegen/src/library/codegen/ir/mir/ty/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ impl MirTypeTrait for MirTypeDelegate {
match self {
MirTypeDelegate::PrimitiveEnum(inner) => inner.mir.self_namespace(),
MirTypeDelegate::Array(inner) => Some(inner.namespace.clone()),
MirTypeDelegate::ProxyEnum(inner) => inner.original.self_namespace(),
MirTypeDelegate::ProxyVariant(inner) => inner.inner.self_namespace(),
_ => None,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) struct ParserMirInternalConfig {
pub default_rust_opaque_codec: RustOpaqueCodecMode,
pub stop_on_error: bool,
pub enable_lifetime: bool,
pub type_64bit_int: bool,
}

// TODO rename - this is no longer an "input-namespace"-only pack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub(crate) struct PartialContext {
pub default_stream_sink_codec: CodecMode,
pub default_rust_opaque_codec: RustOpaqueCodecMode,
pub enable_lifetime: bool,
pub type_64bit_int: bool,
pub parse_mode: ParseMode,
}

Expand Down Expand Up @@ -91,6 +92,7 @@ fn parse_function_inner(
default_stream_sink_codec: partial_context.default_stream_sink_codec,
default_rust_opaque_codec: partial_context.default_rust_opaque_codec,
enable_lifetime: partial_context.enable_lifetime,
type_64bit_int: partial_context.type_64bit_int,
parse_mode: partial_context.parse_mode,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn parse_auto_accessors_of_struct(
config.default_stream_sink_codec,
config.default_rust_opaque_codec,
config.enable_lifetime,
config.type_64bit_int,
parse_mode,
)?;

Expand Down Expand Up @@ -127,6 +128,7 @@ fn create_parsing_context(
default_stream_sink_codec: CodecMode,
default_rust_opaque_codec: RustOpaqueCodecMode,
enable_lifetime: bool,
type_64bit_int: bool,
parse_mode: ParseMode,
) -> anyhow::Result<TypeParserParsingContext> {
Ok(TypeParserParsingContext {
Expand All @@ -138,6 +140,7 @@ fn create_parsing_context(
default_rust_opaque_codec,
owner: None,
enable_lifetime,
type_64bit_int,
parse_mode,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub(crate) fn parse(
config.default_stream_sink_codec,
config.default_rust_opaque_codec,
config.enable_lifetime,
config.type_64bit_int,
parse_mode,
config.stop_on_error,
)
Expand All @@ -82,6 +83,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
default_stream_sink_codec: CodecMode,
default_rust_opaque_codec: RustOpaqueCodecMode,
enable_lifetime: bool,
type_64bit_int: bool,
parse_mode: ParseMode,
stop_on_error: bool,
) -> anyhow::Result<MirFuncOrSkip> {
Expand All @@ -92,6 +94,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
default_stream_sink_codec,
default_rust_opaque_codec,
enable_lifetime,
type_64bit_int,
parse_mode,
) {
Ok(output) => Ok(output),
Expand Down Expand Up @@ -125,6 +128,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
default_stream_sink_codec: CodecMode,
default_rust_opaque_codec: RustOpaqueCodecMode,
enable_lifetime: bool,
type_64bit_int: bool,
parse_mode: ParseMode,
) -> anyhow::Result<MirFuncOrSkip> {
debug!("parse_function function name: {:?}", func.item_fn.name());
Expand Down Expand Up @@ -155,6 +159,7 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
default_rust_opaque_codec,
owner,
enable_lifetime,
type_64bit_int,
parse_mode,
};

Expand Down
2 changes: 2 additions & 0 deletions frb_codegen/src/library/codegen/parser/mir/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub(crate) fn parse(
config.default_stream_sink_codec,
config.default_rust_opaque_codec,
config.enable_lifetime,
config.type_64bit_int,
parse_mode,
)?;

Expand All @@ -50,6 +51,7 @@ pub(crate) fn parse(
default_stream_sink_codec: config.default_stream_sink_codec,
default_rust_opaque_codec: config.default_rust_opaque_codec,
enable_lifetime: config.enable_lifetime,
type_64bit_int: config.type_64bit_int,
parse_mode,
},
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use crate::utils::crate_name::CrateName;
use crate::utils::namespace::Namespace;
use itertools::Itertools;

#[allow(clippy::too_many_arguments)]
pub(crate) fn parse(
hir_trait_impls: &[HirFlatTraitImpl],
type_parser: &mut TypeParser,
rust_output_path_namespace: Namespace,
default_stream_sink_codec: CodecMode,
default_rust_opaque_codec: RustOpaqueCodecMode,
enable_lifetime: bool,
type_64bit_int: bool,
parse_mode: ParseMode,
) -> anyhow::Result<Vec<MirTraitImpl>> {
let context = TypeParserParsingContext {
Expand All @@ -30,6 +32,7 @@ pub(crate) fn parse(
default_rust_opaque_codec,
owner: None,
enable_lifetime,
type_64bit_int,
parse_mode,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ where
if let Some(src_object) = self.src_objects().get(*name) {
let src_object = (*src_object).clone();

let namespace = &src_object.name.namespace;
let namespaced_name = NamespacedName::new(namespace.clone(), name.to_string());
let namespace = self.parse_namespace(name).unwrap();
let namespaced_name = NamespacedName::new(namespace, name.to_string());

let attrs = FrbAttributes::parse(src_object.src.attrs())?;
let attrs_opaque = override_opaque.or(attrs.opaque());
Expand Down Expand Up @@ -87,6 +87,12 @@ where
Ok(None)
}

fn parse_namespace(&mut self, name: &str) -> Option<Namespace> {
self.src_objects()
.get(name)
.map(|object| object.name.namespace.clone())
}

fn handle_dart_code(&mut self, raw_output: &Option<(MirType, FrbAttributes)>) {
if let Some((ty, attrs)) = &raw_output {
let dart_code = attrs.dart_code();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
EnumOrStructParserEnum(self).parse(path, last_segment, None)
}

pub(crate) fn parse_enum_namespace(&mut self, name: &str) -> Option<Namespace> {
EnumOrStructParserEnum(self).parse_namespace(name)
}

fn parse_enum(
&mut self,
src_enum: &HirFlatEnum,
Expand Down
2 changes: 2 additions & 0 deletions frb_codegen/src/library/codegen/parser/mir/parser/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) mod external_impl;
pub(crate) mod generics;
pub(crate) mod lifetimeable;
pub(crate) mod misc;
mod namespace;
pub(crate) mod optional;
pub(crate) mod path;
pub(crate) mod path_data;
Expand Down Expand Up @@ -156,6 +157,7 @@ pub(crate) struct TypeParserParsingContext {
pub(crate) default_rust_opaque_codec: RustOpaqueCodecMode,
pub(crate) owner: Option<MirFuncOwnerInfo>,
pub(crate) enable_lifetime: bool,
pub(crate) type_64bit_int: bool,
pub(crate) parse_mode: ParseMode,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::codegen::parser::mir::parser::ty::TypeParserWithContext;
use crate::utils::namespace::Namespace;

impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_namespace_by_name(&mut self, raw: &str) -> Option<Namespace> {
self.parse_struct_namespace(raw)
.or_else(|| self.parse_enum_namespace(raw))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ fn parse_primitive_raw(s: &str) -> Option<MirTypePrimitive> {
}

fn transform_primitive(inner: MirTypePrimitive, context: &TypeParserParsingContext) -> MirType {
if context.func_attributes.type_64bit_int()
if context.type_64bit_int
|| context.func_attributes.type_64bit_int()
|| (context.struct_or_enum_attributes.as_ref())
.map(|x| x.type_64bit_int())
.unwrap_or_default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
namespace: Option<Namespace>,
codec: Option<RustOpaqueCodecMode>,
) -> RustOpaqueParserTypeInfo {
let effective_namespace = namespace
.or_else(|| self.parse_namespace_by_name(inner))
.unwrap_or(self.context.initiated_namespace.clone());
self.inner.rust_auto_opaque_parser_info.get_or_insert(
inner.to_owned(),
RustOpaqueParserTypeInfo::new(
namespace.unwrap_or(self.context.initiated_namespace.clone()),
effective_namespace,
codec
.or(self.context.func_attributes.rust_opaque_codec())
.unwrap_or(self.context.default_rust_opaque_codec),
Expand All @@ -77,6 +80,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.with_original_lifetime()))?,
// ty_raw.reason, // this may be more reasonable (but does not affect downstream steps currently)
None,
None,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
EnumOrStructParserStruct(self).parse(path, last_segment, override_opaque)
}

pub(crate) fn parse_struct_namespace(&mut self, name: &str) -> Option<Namespace> {
EnumOrStructParserStruct(self).parse_namespace(name)
}

fn parse_struct(
&mut self,
src_struct: &HirFlatStruct,
Expand Down
8 changes: 7 additions & 1 deletion frb_codegen/src/library/codegen/parser/mir/parser/ty/ty.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::parser::mir::parser::ty::misc::convert_ident_str;
use crate::codegen::parser::mir::parser::ty::{TypeParserParsingContext, TypeParserWithContext};
use crate::utils::syn_utils::ty_to_string;
use anyhow::Context;
use syn::Type;

impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
pub(crate) fn parse_type(&mut self, ty: &Type) -> anyhow::Result<MirType> {
let resolve_ty = self.resolve_alias(ty);
self.parse_type_inner(&resolve_ty)
let ans = self.parse_type_inner(&resolve_ty)?;
log::debug!(
"TypeParserWithContext.parse_type ty={} ans={ans:?}",
ty_to_string(ty)
);
Ok(ans)
}

pub(crate) fn parse_type_with_context(
Expand Down
1 change: 1 addition & 0 deletions frb_codegen/src/library/codegen/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ mod tests {
default_rust_opaque_codec: RustOpaqueCodecMode::Nom,
stop_on_error: true,
enable_lifetime: false,
type_64bit_int: false,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
],
"rust_output_path_namespace": "crate::frb_generated"
},
"stop_on_error": false
"stop_on_error": false,
"type_64bit_int": false
}
},
"polisher": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@
],
"rust_output_path_namespace": "crate::frb_generated"
},
"stop_on_error": false
"stop_on_error": false,
"type_64bit_int": false
}
},
"polisher": {
Expand Down
2 changes: 1 addition & 1 deletion frb_example/dart_minimal/flutter_rust_bridge.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ local: true

# TODO temp
#full_dep: true
enable_lifetime: true
#enable_lifetime: true
#stop_on_error: true
10 changes: 0 additions & 10 deletions frb_example/dart_minimal/lib/src/rust/api/minimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,3 @@ import 'package:flutter_rust_bridge/flutter_rust_bridge_for_generated.dart';

Future<int> minimalAdder({required int a, required int b}) =>
RustLib.instance.api.crateApiMinimalMinimalAdder(a: a, b: b);

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Foo>>
abstract class Foo implements RustOpaqueInterface {
Future<Bar> computeBar();

factory Foo() => RustLib.instance.api.crateApiMinimalFooNew();
}

// Rust type: RustOpaqueMoi<flutter_rust_bridge::for_generated::RustAutoOpaqueInner<Lifetimeable < Bar < 'static > >>>
abstract class Bar implements RustOpaqueInterface {}
Loading

0 comments on commit a8db0b6

Please sign in to comment.