Skip to content

Commit

Permalink
Merge pull request #2044 from fzyzcjy/feat/12154
Browse files Browse the repository at this point in the history
Refactor MIR (mid-level intermediate representation) parser
  • Loading branch information
fzyzcjy authored Jun 7, 2024
2 parents 08799b1 + 3e7c9b6 commit 76cdb39
Show file tree
Hide file tree
Showing 61 changed files with 579 additions and 460 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::codegen::generator::api_dart::spec_generator::misc::{
generate_dart_comments, generate_dart_maybe_implements_exception,
};
use crate::codegen::ir::mir::field::MirField;
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirVariant, MirVariantKind};
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirEnumVariant, MirVariantKind};
use crate::codegen::ir::mir::ty::structure::MirStruct;
use crate::library::codegen::generator::api_dart::spec_generator::base::*;
use crate::library::codegen::generator::api_dart::spec_generator::info::ApiDartGeneratorInfoTrait;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {
})
}

fn generate_mode_complex_variant(&self, variant: &MirVariant) -> String {
fn generate_mode_complex_variant(&self, variant: &MirEnumVariant) -> String {
let args = match &variant.kind {
MirVariantKind::Value => "".to_owned(),
MirVariantKind::Struct(st) => {
Expand Down Expand Up @@ -128,7 +128,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {
format!("{{ {} }}", fields.join(""))
}

fn generate_implements_exception(&self, variant: &MirVariant) -> &str {
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.raw == BACKTRACE_IDENT));
if self.mir.is_exception && has_backtrace {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::codegen::generator::api_dart::spec_generator::class::ApiDartGeneratedClass;
use crate::codegen::generator::api_dart::spec_generator::misc::generate_dart_comments;
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirVariant};
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirEnumVariant};
use crate::library::codegen::generator::api_dart::spec_generator::base::*;
use crate::utils::basic_code::DartBasicHeaderCode;
use crate::utils::dart_keywords::make_string_keyword_safe;
Expand Down Expand Up @@ -39,7 +39,7 @@ impl<'a> EnumRefApiDartGenerator<'a> {
})
}

fn generate_mode_simple_variant(&self, variant: &MirVariant) -> String {
fn generate_mode_simple_variant(&self, variant: &MirEnumVariant) -> String {
let variant_name = if self.context.config.dart_enums_style {
make_string_keyword_safe(variant.name.dart_style())
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ fn generate_maybe_impls(
)
}

#[allow(clippy::single_match)]
fn get_candidate_safe_idents_for_matching(ty: &MirType) -> Vec<String> {
let mut ans = vec![ty.safe_ident()];
match ty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ impl<'a> ApiDartGeneratorInfoTrait for DelegateApiDartGenerator<'a> {
MirTypeDelegate::BigPrimitive(_) => "BigInt".to_owned(),
MirTypeDelegate::RustAutoOpaqueExplicit(mir) => {
ApiDartGenerator::new(mir.inner.clone(), self.context).dart_api_type()
}
MirTypeDelegate::DynTrait(mir) => mir.name.name.clone(),
} // MirTypeDelegate::DynTrait(mir) => mir.trait_def_name.name.clone(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
}
MirTypeDelegate::BigPrimitive(_) => "self.toString()".to_owned(),
MirTypeDelegate::RustAutoOpaqueExplicit(_ir) => "self".to_owned(),
MirTypeDelegate::DynTrait(_ir) => lang.throw_unimplemented(""), // TODO
// MirTypeDelegate::DynTrait(_ir) => lang.throw_unimplemented(""), // TODO
},
Lang::RustLang(_) => match &self.mir {
MirTypeDelegate::Array(_) => {
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
}
},
MirTypeDelegate::Uuid => "self.as_bytes().to_vec()".to_owned(),
MirTypeDelegate::StreamSink(_) | MirTypeDelegate::DynTrait(_) => {
MirTypeDelegate::StreamSink(_) /*| MirTypeDelegate::DynTrait(_)*/ => {
return Some(lang.throw_unimplemented(""))
}
MirTypeDelegate::BigPrimitive(_) => "self.to_string()".to_owned(),
Expand Down Expand Up @@ -146,7 +146,7 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
}
MirTypeDelegate::BigPrimitive(_) => "BigInt.parse(inner)".to_owned(),
MirTypeDelegate::RustAutoOpaqueExplicit(_ir) => "inner".to_owned(),
MirTypeDelegate::DynTrait(_) => return Some(lang.throw_unimplemented("")),
// MirTypeDelegate::DynTrait(_) => return Some(lang.throw_unimplemented("")),
}
}
Lang::RustLang(_) => match &self.mir {
Expand Down Expand Up @@ -188,8 +188,7 @@ impl<'a> CodecSseTyTrait for DelegateCodecSseTy<'a> {
MirTypeDelegate::RustAutoOpaqueExplicit(_ir) => {
"flutter_rust_bridge::for_generated::rust_auto_opaque_explicit_decode(inner)"
.to_owned()
}
MirTypeDelegate::DynTrait(_ir) => lang.throw_unimplemented(""), // TODO
} // MirTypeDelegate::DynTrait(_ir) => lang.throw_unimplemented(""), // TODO
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::codegen::generator::codec::sse::ty::structure::GeneralizedStructGenerator;
use crate::codegen::generator::codec::sse::ty::*;
use crate::codegen::generator::misc::StructOrRecord;
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirVariant, MirVariantKind};
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirEnumVariant, MirVariantKind};
use crate::library::codegen::generator::codec::sse::lang::LangTrait;
use crate::utils::namespace::NamespacedName;
use itertools::Itertools;
Expand Down Expand Up @@ -62,7 +62,7 @@ impl<'a> CodecSseTyTrait for EnumRefCodecSseTy<'a> {
}

fn generate_decode_variant(
variant: &MirVariant,
variant: &MirEnumVariant,
enum_name: &NamespacedName,
lang: &Lang,
context: CodecSseTyContext,
Expand Down Expand Up @@ -95,7 +95,7 @@ pub(crate) fn generate_enum_encode_rust_general(
lang: &Lang,
src: &MirEnum,
self_ref: &str,
generate_branch: impl Fn(usize, &MirVariant) -> String,
generate_branch: impl Fn(usize, &MirEnumVariant) -> String,
) -> String {
let enum_name_str = src.name.style(lang);
let enum_sep = enum_sep(lang);
Expand All @@ -118,7 +118,7 @@ pub(crate) fn generate_enum_encode_rust_general(
)
}

fn pattern_match_enum_variant(lang: &Lang, variant: &MirVariant) -> String {
fn pattern_match_enum_variant(lang: &Lang, variant: &MirEnumVariant) -> String {
match &variant.kind {
MirVariantKind::Value => match lang {
Lang::DartLang(_) => "()".to_owned(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<'a> WireDartCodecCstGeneratorEncoderTrait for DelegateWireDartCodecCstGener
// ))),
MirTypeDelegate::Backtrace
| MirTypeDelegate::AnyhowException
| MirTypeDelegate::DynTrait(_) => {
/*| MirTypeDelegate::DynTrait(_)*/ => {
Acc::distribute(Some("throw UnimplementedError();".to_string()))
}
MirTypeDelegate::Map(_) => Acc::distribute(Some(format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::codegen::generator::misc::target::Target;
use crate::codegen::generator::wire::dart::spec_generator::codec::cst::base::*;
use crate::codegen::generator::wire::dart::spec_generator::codec::cst::encoder::misc::dart_wire_type_from_rust_wire_type_or_web;
use crate::codegen::generator::wire::dart::spec_generator::codec::cst::encoder::ty::WireDartCodecCstGeneratorEncoderTrait;
use crate::codegen::ir::mir::ty::enumeration::{MirVariant, MirVariantKind};
use crate::codegen::ir::mir::ty::enumeration::{MirEnumVariant, MirVariantKind};
use crate::library::codegen::ir::mir::ty::MirTypeTrait;
use itertools::Itertools;

Expand Down Expand Up @@ -43,7 +43,11 @@ impl<'a> WireDartCodecCstGeneratorEncoderTrait for EnumRefWireDartCodecCstGenera
}

impl<'a> EnumRefWireDartCodecCstGenerator<'a> {
fn generate_api_fill_to_wire_body_variant(&self, index: usize, variant: &MirVariant) -> String {
fn generate_api_fill_to_wire_body_variant(
&self,
index: usize,
variant: &MirEnumVariant,
) -> String {
let wrapper_name = &variant.wrapper_name;
let variant_name = &variant.name;

Expand Down Expand Up @@ -85,7 +89,7 @@ impl<'a> EnumRefWireDartCodecCstGenerator<'a> {
}
}

fn generate_encode_body_variant(index: usize, variant: &MirVariant) -> String {
fn generate_encode_body_variant(index: usize, variant: &MirEnumVariant) -> String {
let fields = match &variant.kind {
MirVariantKind::Value => vec![],
MirVariantKind::Struct(st) => (st.fields)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl<'a> WireDartCodecDcoGeneratorDecoderTrait for DelegateWireDartCodecDcoGener
"return Set.from(dco_decode_{}(raw));",
self.mir.get_delegate().safe_ident(),
),
MirTypeDelegate::StreamSink(_) | MirTypeDelegate::DynTrait(_) => "throw UnimplementedError();".to_owned(),
MirTypeDelegate::StreamSink(_) /*| MirTypeDelegate::DynTrait(_)*/ => "throw UnimplementedError();".to_owned(),
MirTypeDelegate::BigPrimitive(_) => {
"return BigInt.parse(raw);".to_owned()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for DelegateWireRustCodecCstGener
// "let multiple: Vec<u8> = self.cst_decode(); flutter_rust_bridge::for_generated::decode_uuids(multiple)".into(),
// ),
// ),
MirTypeDelegate::Backtrace | MirTypeDelegate::AnyhowException | MirTypeDelegate::DynTrait(_) => Acc::new(|target| match target {
MirTypeDelegate::Backtrace | MirTypeDelegate::AnyhowException /*| MirTypeDelegate::DynTrait(_)*/ => Acc::new(|target| match target {
TargetOrCommon::Common => None,
TargetOrCommon::Io | TargetOrCommon::Web => Some("unimplemented!()".into()),
}),
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<'a> WireRustCodecCstGeneratorDecoderTrait for DelegateWireRustCodecCstGener
"self.unchecked_into::<flutter_rust_bridge::for_generated::js_sys::Uint8Array>().to_vec().into_boxed_slice().cst_decode()"
.into()
}
MirTypeDelegate::Backtrace | MirTypeDelegate::AnyhowException | MirTypeDelegate::DynTrait(_) => "unimplemented!()".into(),
MirTypeDelegate::Backtrace | MirTypeDelegate::AnyhowException /*| MirTypeDelegate::DynTrait(_)*/ => "unimplemented!()".into(),
MirTypeDelegate::Array(array) => generate_decode_array(array)
.into(),
MirTypeDelegate::Map(mir) => generate_decode_map(mir).into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::codegen::generator::wire::rust::spec_generator::output_code::WireRust
use crate::codegen::generator::wire::rust::spec_generator::codec::cst::base::*;
use crate::codegen::generator::wire::rust::spec_generator::codec::cst::decoder::ty::WireRustCodecCstGeneratorDecoderTrait;
use crate::codegen::ir::mir::ident::MirIdent;
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirEnumMode, MirVariant, MirVariantKind};
use crate::codegen::ir::mir::ty::enumeration::{MirEnum, MirEnumMode, MirEnumVariant, MirVariantKind};
use crate::codegen::ir::mir::ty::structure::MirStruct;
use itertools::Itertools;
use crate::codegen::generator::wire::rust::spec_generator::codec::cst::decoder::impl_new_with_nullptr::generate_impl_new_with_nullptr_code_block;
Expand Down Expand Up @@ -153,7 +153,7 @@ fn generate_impl_cst_decode_body_variant(
enu: &MirEnum,
target: TargetOrCommon,
idx: usize,
variant: &MirVariant,
variant: &MirEnumVariant,
) -> String {
match &variant.kind {
MirVariantKind::Value => {
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 @@ -3,7 +3,7 @@ use crate::codegen::ir::hir::misc::generation_source::HirGenerationSource;
use crate::codegen::ir::hir::misc::item_fn::GeneralizedItemFn;
use crate::codegen::ir::hir::misc::serializers::serialize_generalized_item_fn;
use crate::codegen::ir::hir::misc::serializers::serialize_syn;
use crate::codegen::parser::mir::attribute_parser::FrbAttributes;
use crate::codegen::parser::mir::parser::attribute::FrbAttributes;
use crate::utils::namespace::{Namespace, NamespacedName};
use crate::utils::syn_utils::ty_to_string;
use serde::Serialize;
Expand Down
44 changes: 31 additions & 13 deletions frb_codegen/src/library/codegen/ir/mir/ty/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::codegen::ir::mir::ty::rust_auto_opaque_implicit::MirRustAutoOpaqueRaw
use crate::codegen::ir::mir::ty::rust_opaque::MirTypeRustOpaque;
use crate::codegen::ir::mir::ty::{MirContext, MirType, MirTypeTrait};
use crate::utils::namespace::Namespace;
use crate::utils::namespace::NamespacedName;

crate::mir! {
/// types that delegate to another type
Expand All @@ -30,7 +29,7 @@ pub enum MirTypeDelegate {
StreamSink(MirTypeDelegateStreamSink),
BigPrimitive(MirTypeDelegateBigPrimitive),
RustAutoOpaqueExplicit(MirTypeDelegateRustAutoOpaqueExplicit),
DynTrait(MirTypeDelegateDynTrait),
// DynTrait(MirTypeDelegateDynTrait),
}

pub struct MirTypeDelegateArray {
Expand Down Expand Up @@ -84,10 +83,9 @@ pub struct MirTypeDelegateRustAutoOpaqueExplicit {
pub raw: MirRustAutoOpaqueRaw,
}

pub struct MirTypeDelegateDynTrait {
pub name: NamespacedName,
pub inner: MirTypeEnumRef,
}
// pub struct MirTypeDelegateDynTrait {
// pub trait_def_name: NamespacedName,
// }
}

impl MirTypeTrait for MirTypeDelegate {
Expand Down Expand Up @@ -132,10 +130,7 @@ impl MirTypeTrait for MirTypeDelegate {
MirTypeDelegate::BigPrimitive(mir) => mir.to_string(),
MirTypeDelegate::RustAutoOpaqueExplicit(mir) => {
format!("AutoExplicit_{}", mir.inner.safe_ident())
}
MirTypeDelegate::DynTrait(mir) => {
format!("DynTrait_{}", mir.inner.safe_ident())
}
} // MirTypeDelegate::DynTrait(mir) => mir.safe_ident(),
}
}

Expand Down Expand Up @@ -197,8 +192,7 @@ impl MirTypeTrait for MirTypeDelegate {
},
MirTypeDelegate::RustAutoOpaqueExplicit(mir) => {
format!("RustAutoOpaque{}<{}>", mir.inner.codec, mir.raw.string)
}
MirTypeDelegate::DynTrait(mir) => format!("dyn <{}>", mir.name.name),
} // MirTypeDelegate::DynTrait(mir) => format!("dyn <{}>", mir.trait_def_name.name),
}
}

Expand Down Expand Up @@ -266,7 +260,7 @@ impl MirTypeDelegate {
MirTypeDelegate::StreamSink(_) => MirType::Delegate(MirTypeDelegate::String),
MirTypeDelegate::BigPrimitive(_) => MirType::Delegate(MirTypeDelegate::String),
MirTypeDelegate::RustAutoOpaqueExplicit(mir) => MirType::RustOpaque(mir.inner.clone()),
MirTypeDelegate::DynTrait(mir) => MirType::EnumRef(mir.inner.clone()),
// MirTypeDelegate::DynTrait(mir) => mir.inner(),
}
}
}
Expand Down Expand Up @@ -307,3 +301,27 @@ impl MirTypeDelegateArray {
}
}
}

// impl MirTypeDelegateDynTrait {
// pub fn inner(&self) -> MirType {
// MirType::EnumRef(self.inner_raw())
// }
//
// pub fn inner_raw(&self) -> MirTypeEnumRef {
// MirTypeEnumRef {
// ident: MirEnumIdent(NamespacedName::new(
// self.trait_def_name.namespace.clone(),
// self.inner_enum_name(),
// )),
// is_exception: false,
// }
// }
//
// pub(crate) fn inner_enum_name(&self) -> String {
// format!("{}DynImplEnum", self.trait_def_name.name)
// }
//
// pub(crate) fn safe_ident(&self) -> String {
// format!("DynTrait_{}", self.trait_def_name.name)
// }
// }
6 changes: 3 additions & 3 deletions frb_codegen/src/library/codegen/ir/mir/ty/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct MirEnum {
pub name: NamespacedName,
pub wrapper_name: Option<String>,
pub comments: Vec<MirComment>,
pub variants: Vec<MirVariant>,
pub variants: Vec<MirEnumVariant>,
pub mode: MirEnumMode,
pub ignore: bool,
}
Expand All @@ -31,7 +31,7 @@ pub enum MirEnumMode {
Complex,
}

pub struct MirVariant {
pub struct MirEnumVariant {
pub name: MirIdent,
pub wrapper_name: MirIdent,
pub comments: Vec<MirComment>,
Expand Down Expand Up @@ -85,7 +85,7 @@ impl MirTypeTrait for MirTypeEnumRef {
}

impl MirEnum {
pub fn variants(&self) -> &[MirVariant] {
pub fn variants(&self) -> &[MirEnumVariant] {
&self.variants
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::codegen::parser::mir::attribute_parser::FrbAttributes;
use crate::codegen::parser::mir::parser::attribute::FrbAttributes;
use anyhow::Context;
use itertools::Itertools;
use proc_macro2::Ident;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::codegen::ir::hir::flat::pack::HirFlatPack;
use crate::codegen::ir::hir::flat::type_alias::HirFlatTypeAlias;
use crate::codegen::parser::mir::type_parser::misc::convert_ident_str;
use crate::codegen::parser::mir::parser::ty::misc::convert_ident_str;
use itertools::Itertools;
use std::collections::HashMap;
use syn::Type;
Expand Down
38 changes: 0 additions & 38 deletions frb_codegen/src/library/codegen/parser/mir/file_reader.rs

This file was deleted.

Loading

0 comments on commit 76cdb39

Please sign in to comment.