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

Refactor macros information encoding and decoding #2014

Merged
merged 61 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
6d96031
refactor: rm unwrap_frb_attrs_in_doc
fzyzcjy Jun 2, 2024
b727e3b
refactor: split
fzyzcjy Jun 2, 2024
b4b729a
refactor: mv
fzyzcjy Jun 2, 2024
c099cf8
chore: inline
fzyzcjy Jun 2, 2024
f32919c
refactor: split
fzyzcjy Jun 2, 2024
46467a0
feat: change
fzyzcjy Jun 2, 2024
1933ef1
feat: use quote
fzyzcjy Jun 2, 2024
cf56764
feat: more
fzyzcjy Jun 2, 2024
3133841
feat: more
fzyzcjy Jun 2, 2024
dd1990a
feat: better delimeter
fzyzcjy Jun 2, 2024
c93cf55
feat: more
fzyzcjy Jun 2, 2024
0488672
feat: demo
fzyzcjy Jun 2, 2024
27e53c5
refactor: rename
fzyzcjy Jun 2, 2024
24b6a9e
chore: rename
fzyzcjy Jun 2, 2024
d2040a0
chore: mv
fzyzcjy Jun 2, 2024
e59cd52
feat: more
fzyzcjy Jun 2, 2024
3ee9898
fix: err
fzyzcjy Jun 2, 2024
3596adf
feat: more
fzyzcjy Jun 2, 2024
4b926a9
feat: more
fzyzcjy Jun 2, 2024
910f1ee
chore: empty
fzyzcjy Jun 2, 2024
563b65d
Revert "chore: empty"
fzyzcjy Jun 2, 2024
7b21e09
chore: mv
fzyzcjy Jun 2, 2024
f933c33
refactor: mv
fzyzcjy Jun 2, 2024
9fedeb5
refactor: more
fzyzcjy Jun 2, 2024
4147e83
chore: lint
fzyzcjy Jun 2, 2024
c56a03e
refactor: mv
fzyzcjy Jun 2, 2024
cf2df69
refactor: mv
fzyzcjy Jun 2, 2024
3867f5e
Merge branch 'master' into feat/12123
fzyzcjy Jun 3, 2024
ef92196
refactor: split
fzyzcjy Jun 3, 2024
3872871
feat: empty
fzyzcjy Jun 3, 2024
b35a3ca
chore: more
fzyzcjy Jun 3, 2024
74a7f40
chore: rm
fzyzcjy Jun 3, 2024
7d326b2
feat: more
fzyzcjy Jun 3, 2024
070359d
feat: simp
fzyzcjy Jun 3, 2024
28a8cb0
fix: err
fzyzcjy Jun 3, 2024
1c5bf08
fix: err
fzyzcjy Jun 3, 2024
a2ca99b
fix: err
fzyzcjy Jun 3, 2024
5e00c6f
refactor: rm
fzyzcjy Jun 3, 2024
5167149
refactor: rm usage
fzyzcjy Jun 3, 2024
a8230e4
feat: more
fzyzcjy Jun 3, 2024
3da7ae1
feat: more
fzyzcjy Jun 3, 2024
49ec29c
Revert "feat: more"
fzyzcjy Jun 3, 2024
a93cef2
feat: more
fzyzcjy Jun 3, 2024
3eff4c6
Revert "feat: more"
fzyzcjy Jun 3, 2024
0206f5a
Revert "Revert "feat: more""
fzyzcjy Jun 3, 2024
8f809b4
feat: more
fzyzcjy Jun 3, 2024
a768033
refactor: simp
fzyzcjy Jun 3, 2024
a2cf1fd
refactor: more
fzyzcjy Jun 3, 2024
e136989
refactor: more
fzyzcjy Jun 3, 2024
5e1212c
feat: more
fzyzcjy Jun 3, 2024
b4e200d
fix: err
fzyzcjy Jun 3, 2024
b1bb121
chore: rm demo
fzyzcjy Jun 3, 2024
aeed690
chore: lint
fzyzcjy Jun 3, 2024
76592c5
Revert "Auxiliary commit to revert individual files from aeed690b46e2…
fzyzcjy Jun 3, 2024
51a7d5e
chore: lower version
fzyzcjy Jun 3, 2024
bba8d35
chore: more
fzyzcjy Jun 3, 2024
e51cc0d
chore: lint
fzyzcjy Jun 3, 2024
eef892d
chore: lint
fzyzcjy Jun 3, 2024
47062f5
chore: fmt
fzyzcjy Jun 3, 2024
669e87f
chore: dep
fzyzcjy Jun 3, 2024
b5ae2fe
chore: codegen
fzyzcjy Jun 3, 2024
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
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 2 additions & 17 deletions frb_codegen/src/library/codegen/parser/hir/hierarchical/mod.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
use crate::codegen::ir::hir::hierarchical::pack::HirPack;
use crate::codegen::ir::hir::raw::pack::HirRawPack;
use crate::codegen::parser::hir::hierarchical::crates::parse_crate;
use crate::codegen::parser::hir::internal_config::ParserHirInternalConfig;

pub(crate) mod crates;
pub(crate) mod function;
pub(crate) mod item_type;
pub(crate) mod mirror_ident;
pub(crate) mod module;
mod pub_use;
pub(crate) mod pack;
pub(crate) mod struct_or_enum;
mod third_party_override_transformer;
pub(crate) mod visibility;

pub(crate) fn parse(
config: &ParserHirInternalConfig,
hir_raw: &HirRawPack,
) -> anyhow::Result<HirPack> {
let pack = parse_raw(config, hir_raw)?;
let pack = third_party_override_transformer::transform(pack)?;
Ok(pack)
}

fn parse_raw(config: &ParserHirInternalConfig, hir_raw: &HirRawPack) -> anyhow::Result<HirPack> {
let crates = hir_raw
.crates
.iter()
.map(|c| parse_crate(config, &c.syn_file, &c.name))
.collect::<anyhow::Result<Vec<_>>>()?
.into_iter()
.collect();
Ok(HirPack { crates })
pack::parse_pack(config, hir_raw)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
mod pub_use_transformer;
mod syn_item;

use crate::codegen::ir::hir::hierarchical::module::{HirModule, HirModuleContent, HirModuleMeta};
use crate::codegen::parser::hir::hierarchical::function::parse_generalized_functions;
use crate::codegen::parser::hir::internal_config::ParserHirInternalConfig;

pub(crate) fn parse_module(
items: &[syn::Item],
meta: HirModuleMeta,
config: &ParserHirInternalConfig,
) -> anyhow::Result<HirModule> {
let module = parse_raw(items, meta, config)?;
let module = pub_use_transformer::transform(module, items)?;
Ok(module)
}

fn parse_raw(
items: &[syn::Item],
meta: HirModuleMeta,
config: &ParserHirInternalConfig,
) -> anyhow::Result<HirModule> {
let mut scope = HirModuleContent {
functions: parse_generalized_functions(items, &meta.namespace)?,
..HirModuleContent::default()
};

for item in items.iter() {
syn_item::parse_syn_item(item, &mut scope, config, &meta.namespace, &meta.parent_vis)?;
}

Ok(HirModule {
meta,
content: scope,
raw: (items.iter())
.filter(|item| !matches!(item, syn::Item::Mod(_)))
.map(|item| quote::quote!(#item).to_string())
.collect(),
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use crate::codegen::ir::hir::hierarchical::module::HirModule;
use crate::utils::namespace::Namespace;
use itertools::Itertools;

pub(crate) fn transform_module_by_pub_use(
mut module: HirModule,
items: &[syn::Item],
) -> anyhow::Result<HirModule> {
pub(crate) fn transform(mut module: HirModule, items: &[syn::Item]) -> anyhow::Result<HirModule> {
let pub_use_names = parse_pub_use_from_items(items);
for pub_use_name in pub_use_names {
transform_module_by_pub_use_single(&mut module, &pub_use_name)?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,16 @@
use crate::codegen::ir::hir::hierarchical::module::{
HirModule, HirModuleContent, HirModuleMeta, HirVisibility,
};
use crate::codegen::parser::hir::hierarchical::function::parse_generalized_functions;
use crate::codegen::parser::hir::hierarchical::item_type::parse_syn_item_type;
use crate::codegen::parser::hir::hierarchical::pub_use::transform_module_by_pub_use;
use crate::codegen::parser::hir::hierarchical::module::parse_module;
use crate::codegen::parser::hir::hierarchical::struct_or_enum::{
parse_syn_item_enum, parse_syn_item_struct,
};
use crate::codegen::parser::hir::internal_config::ParserHirInternalConfig;
use crate::utils::namespace::Namespace;
use syn::ItemMod;

pub(crate) fn parse_module(
items: &[syn::Item],
meta: HirModuleMeta,
config: &ParserHirInternalConfig,
) -> anyhow::Result<HirModule> {
let module = parse_module_raw(items, meta, config)?;
let module = transform_module_by_pub_use(module, items)?;
Ok(module)
}

fn parse_module_raw(
items: &[syn::Item],
meta: HirModuleMeta,
config: &ParserHirInternalConfig,
) -> anyhow::Result<HirModule> {
let mut scope = HirModuleContent {
functions: parse_generalized_functions(items, &meta.namespace)?,
..HirModuleContent::default()
};

for item in items.iter() {
parse_syn_item(item, &mut scope, config, &meta.namespace, &meta.parent_vis)?;
}

Ok(HirModule {
meta,
content: scope,
raw: (items.iter())
.filter(|item| !matches!(item, syn::Item::Mod(_)))
.map(|item| quote::quote!(#item).to_string())
.collect(),
})
}

fn parse_syn_item(
pub(super) fn parse_syn_item(
item: &syn::Item,
scope: &mut HirModuleContent,
config: &ParserHirInternalConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::codegen::ir::hir::hierarchical::pack::HirPack;
use crate::codegen::ir::hir::raw::pack::HirRawPack;
use crate::codegen::parser::hir::hierarchical::crates::parse_crate;
use crate::codegen::parser::hir::internal_config::ParserHirInternalConfig;

mod third_party_override_transformer;

pub(crate) fn parse_pack(
config: &ParserHirInternalConfig,
hir_raw: &HirRawPack,
) -> anyhow::Result<HirPack> {
let pack = parse_raw(config, hir_raw)?;
let pack = third_party_override_transformer::transform(pack)?;
Ok(pack)
}

fn parse_raw(config: &ParserHirInternalConfig, hir_raw: &HirRawPack) -> anyhow::Result<HirPack> {
let crates = hir_raw
.crates
.iter()
.map(|c| parse_crate(config, &c.syn_file, &c.name))
.collect::<anyhow::Result<Vec<_>>>()?
.into_iter()
.collect();
Ok(HirPack { crates })
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use crate::codegen::ir::mir::ty::MirType;
use crate::codegen::parser::mir::attribute_parser::FrbAttributes;
use crate::codegen::parser::mir::function_parser::structs::ParseFunctionOutput;
use crate::codegen::parser::mir::type_parser::misc::parse_comments;
use crate::codegen::parser::mir::type_parser::{
external_impl, TypeParser, TypeParserParsingContext,
};
use crate::codegen::parser::mir::type_parser::{TypeParser, TypeParserParsingContext};
use crate::library::codegen::ir::mir::ty::MirTypeTrait;
use crate::utils::namespace::{Namespace, NamespacedName};
use anyhow::{bail, Context};
Expand Down Expand Up @@ -203,9 +201,10 @@ impl<'a, 'b> FunctionParser<'a, 'b> {
return Ok(None);
};

let owner_ty_name = external_impl::parse_name_or_original(
&(self_ty_path.path.segments.first().unwrap().ident).to_string(),
)?;
// let owner_ty_name = external_impl::parse_name_or_original(
// &(self_ty_path.path.segments.first().unwrap().ident).to_string(),
// )?;
let owner_ty_name = (self_ty_path.path.segments.first().unwrap().ident).to_string();
let syn_ty: Type = parse_str(&owner_ty_name)?;
Ok(Some(self.type_parser.parse_type(&syn_ty, context)?))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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;
Expand Down Expand Up @@ -34,14 +33,14 @@ where
override_opaque: Option<bool>,
) -> anyhow::Result<Option<(MirType, FrbAttributes)>> {
let (name, _) = last_segment;
let name = external_impl::parse_name_or_original(name)?;
// let name = external_impl::parse_name_or_original(name)?;

if let Some(src_object) = self.src_objects().get(&name) {
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());
let namespaced_name = NamespacedName::new(namespace.clone(), name.to_string());

let attrs = FrbAttributes::parse(src_object.src.attrs())?;
let attrs_opaque = override_opaque.or(attrs.opaque());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
use anyhow::Result;
use syn::{parse_str, visit_mut, visit_mut::VisitMut, Path, Type};

pub(crate) fn parse_type(mut ty: Type) -> Result<Type> {
struct Visitor;
impl VisitMut for Visitor {
fn visit_path_mut(&mut self, node: &mut Path) {
if node.segments.len() == 1 {
let ident = &node.segments[0].ident;
if let Some(reconstructed_name) = parse_name(&ident.to_string()).unwrap() {
// println!("hi {node:?} {reconstructed_name}");
*node = parse_str(&reconstructed_name).unwrap();
}
}

visit_mut::visit_path_mut(self, node);
}
}
Visitor.visit_type_mut(&mut ty);
Ok(ty)
}

pub(crate) fn parse_name_or_original(raw_name: &str) -> Result<String> {
Ok(parse_name(raw_name)?.unwrap_or_else(|| raw_name.to_string()))
}

fn parse_name(raw_name: &str) -> Result<Option<String>> {
const DUMMY_STRUCT_PREFIX: &str = "__external_impl__";
Ok(
if let Some(stripped_name) = raw_name.strip_prefix(DUMMY_STRUCT_PREFIX) {
Some(String::from_utf8(hex::decode(stripped_name)?)?)
} else {
None
},
)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_external_impl_dummy_struct_name() {
assert_eq!(parse_name("One<Two,Three>").unwrap(), None);
assert_eq!(
parse_name("__external_impl__4f6e65203c2054776f2c205468726565203e").unwrap(),
Some("One < Two, Three >".to_owned()),
);
}
}
// use anyhow::Result;
// use syn::{parse_str, visit_mut, visit_mut::VisitMut, Path, Type};
//
// pub(crate) fn parse_type(mut ty: Type) -> Result<Type> {
// struct Visitor;
// impl VisitMut for Visitor {
// fn visit_path_mut(&mut self, node: &mut Path) {
// if node.segments.len() == 1 {
// let ident = &node.segments[0].ident;
// if let Some(reconstructed_name) = parse_name(&ident.to_string()).unwrap() {
// // println!("hi {node:?} {reconstructed_name}");
// *node = parse_str(&reconstructed_name).unwrap();
// }
// }
//
// visit_mut::visit_path_mut(self, node);
// }
// }
// Visitor.visit_type_mut(&mut ty);
// Ok(ty)
// }
//
// pub(crate) fn parse_name_or_original(raw_name: &str) -> Result<String> {
// Ok(parse_name(raw_name)?.unwrap_or_else(|| raw_name.to_string()))
// }
//
// fn parse_name(raw_name: &str) -> Result<Option<String>> {
// const DUMMY_STRUCT_PREFIX: &str = "__external_impl__";
// Ok(
// if let Some(stripped_name) = raw_name.strip_prefix(DUMMY_STRUCT_PREFIX) {
// Some(String::from_utf8(hex::decode(stripped_name)?)?)
// } else {
// None
// },
// )
// }
//
// #[cfg(test)]
// mod tests {
// use super::*;
//
// #[test]
// fn test_parse_external_impl_dummy_struct_name() {
// assert_eq!(parse_name("One<Two,Three>").unwrap(), None);
// assert_eq!(
// parse_name("__external_impl__4f6e65203c2054776f2c205468726565203e").unwrap(),
// Some("One < Two, Three >".to_owned()),
// );
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::codegen::ir::mir::ty::rust_opaque::{
MirRustOpaqueInner, MirTypeRustOpaque, RustOpaqueCodecMode,
};
use crate::codegen::ir::mir::ty::{MirType, MirTypeTrait};
use crate::codegen::parser::mir::type_parser::external_impl;
use crate::codegen::parser::mir::type_parser::path_data::extract_path_data;
use crate::codegen::parser::mir::type_parser::rust_opaque::{
GeneralizedRustOpaqueParserInfo, RustOpaqueParserTypeInfo,
Expand Down Expand Up @@ -42,7 +41,7 @@ impl<'a, 'b, 'c> TypeParserWithContext<'a, 'b, 'c> {
namespace: Option<Namespace>,
codec: Option<RustOpaqueCodecMode>,
) -> Result<(MirRustAutoOpaqueRaw, MirTypeRustOpaque)> {
let inner = external_impl::parse_type(inner)?;
// let inner = external_impl::parse_type(inner)?;

let inner_str = inner.to_token_stream().to_string();
let info = self.get_or_insert_rust_auto_opaque_info(&inner_str, namespace, codec);
Expand Down
Loading
Loading