Skip to content

Commit

Permalink
s/c_rename/abi_rename
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Feb 6, 2024
1 parent 16befd7 commit e16e740
Show file tree
Hide file tree
Showing 25 changed files with 62 additions and 62 deletions.
38 changes: 19 additions & 19 deletions core/src/ast/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub struct Attrs {
/// in HIR backends,
pub skip_if_unsupported: bool,

/// Renames to apply to the underlying C function. Can be found on methods, impls, and bridge modules, and is inherited.
/// Renames to apply to the underlying C symbol. Can be found on methods, impls, and bridge modules, and is inherited.
///
/// Has no effect on types.
pub c_rename: RenameAttr,
pub abi_rename: RenameAttr,
}

impl Attrs {
Expand All @@ -32,15 +32,15 @@ impl Attrs {
Attr::Cfg(attr) => self.cfg.push(attr),
Attr::DiplomatBackend(attr) => self.attrs.push(attr),
Attr::SkipIfUnsupported => self.skip_if_unsupported = true,
Attr::CRename(rename) => self.c_rename.extend(&rename, AttrExtendMode::Override),
Attr::CRename(rename) => self.abi_rename.extend(&rename, AttrExtendMode::Override),
}
}

/// Merge attributes that should be inherited from the parent
pub(crate) fn merge_parent_attrs(&mut self, other: &Attrs) {
self.cfg.extend(other.cfg.iter().cloned());
self.c_rename
.extend(&other.c_rename, AttrExtendMode::Inherit);
self.abi_rename
.extend(&other.abi_rename, AttrExtendMode::Inherit);
}
pub(crate) fn add_attrs(&mut self, attrs: &[Attribute]) {
for attr in syn_attr_to_ast_attr(attrs) {
Expand Down Expand Up @@ -71,7 +71,7 @@ enum Attr {
fn syn_attr_to_ast_attr(attrs: &[Attribute]) -> impl Iterator<Item = Attr> + '_ {
let cfg_path: syn::Path = syn::parse_str("cfg").unwrap();
let dattr_path: syn::Path = syn::parse_str("diplomat::attr").unwrap();
let crename_attr: syn::Path = syn::parse_str("diplomat::c_rename").unwrap();
let crename_attr: syn::Path = syn::parse_str("diplomat::abi_rename").unwrap();
let skipast: syn::Path = syn::parse_str("diplomat::skip_if_unsupported").unwrap();
attrs.iter().filter_map(move |a| {
if a.path() == &cfg_path {
Expand Down Expand Up @@ -112,8 +112,8 @@ impl Serialize for Attrs {
if self.skip_if_unsupported {
state.serialize_field("skip_if_unsupported", &self.skip_if_unsupported)?;
}
if !self.c_rename.is_empty() {
state.serialize_field("c_rename", &self.c_rename)?;
if !self.abi_rename.is_empty() {
state.serialize_field("abi_rename", &self.abi_rename)?;
}
state.end()
}
Expand Down Expand Up @@ -220,13 +220,13 @@ pub(crate) enum AttrExtendMode {
Override,
}

/// A pattern for use in rename attributes, like `#[diplomat::c_rename]`
/// A pattern for use in rename attributes, like `#[diplomat::abi_rename]`
///
/// This can be parsed from a string, typically something like `icu4x_{0}`.
/// It can have up to one {0} for replacement.
///
/// In the future this may support transformations like to_camel_case, etc,
/// probably specified as a list like `#[diplomat::c_rename("foo{0}", to_camel_case)]`
/// probably specified as a list like `#[diplomat::abi_rename("foo{0}", to_camel_case)]`
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Serialize)]
pub struct RenameAttr {
pattern: Option<RenamePattern>,
Expand Down Expand Up @@ -273,23 +273,23 @@ impl RenameAttr {
}

pub(crate) fn from_meta(meta: &Meta) -> Result<Self, Cow<'static, str>> {
static C_RENAME_ERROR: &str = "#[diplomat::c_rename] must be given a string value";
static ABI_RENAME_ERROR: &str = "#[diplomat::abi_rename] must be given a string value";

match meta {
Meta::Path(..) => Err(C_RENAME_ERROR.into()),
Meta::Path(..) => Err(ABI_RENAME_ERROR.into()),
Meta::NameValue(ref nv) => {
// Support a shortcut `c_rename = "..."`
// Support a shortcut `abi_rename = "..."`
let Expr::Lit(ref lit) = nv.value else {
return Err(C_RENAME_ERROR.into());
return Err(ABI_RENAME_ERROR.into());
};
let Lit::Str(ref lit) = lit.lit else {
return Err(C_RENAME_ERROR.into());
return Err(ABI_RENAME_ERROR.into());
};
Ok(RenameAttr::from_pattern(&lit.value()))
}
// The full syntax to which we'll add more things in the future, `c_rename("")`
// The full syntax to which we'll add more things in the future, `abi_rename("")`
Meta::List(list) => list.parse_args().map_err(|e| {
format!("Failed to parse malformed #[diplomat::c_rename(...)]: {e}").into()
format!("Failed to parse malformed #[diplomat::abi_rename(...)]: {e}").into()
}),
}
}
Expand Down Expand Up @@ -364,10 +364,10 @@ mod tests {

#[test]
fn test_rename() {
let attr: syn::Attribute = syn::parse_quote!(#[diplomat::c_rename = "foobar_{0}"]);
let attr: syn::Attribute = syn::parse_quote!(#[diplomat::abi_rename = "foobar_{0}"]);
let attr = RenameAttr::from_meta(&attr.meta).unwrap();
insta::assert_yaml_snapshot!(attr);
let attr: syn::Attribute = syn::parse_quote!(#[diplomat::c_rename("foobar_{0}")]);
let attr: syn::Attribute = syn::parse_quote!(#[diplomat::abi_rename("foobar_{0}")]);
let attr = RenameAttr::from_meta(&attr.meta).unwrap();
insta::assert_yaml_snapshot!(attr);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/ast/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Method {
let method_ident = &m.sig.ident;
let concat_method_ident = format!("{self_ident}_{method_ident}");
let extern_ident = syn::Ident::new(
&attrs.c_rename.apply(concat_method_ident.into()),
&attrs.abi_rename.apply(concat_method_ident.into()),
m.sig.ident.span(),
);

Expand Down
4 changes: 2 additions & 2 deletions core/src/hir/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub use crate::ast::attrs::RenameAttr;
pub struct Attrs {
pub disable: bool,
pub rename: RenameAttr,
pub c_rename: RenameAttr,
pub abi_rename: RenameAttr,
// more to be added: rename, namespace, etc
}

Expand All @@ -38,7 +38,7 @@ impl Attrs {
) -> Self {
let mut this = Attrs {
// Backends must support this since it applies to the macro/C code.
c_rename: ast.c_rename.clone(),
abi_rename: ast.abi_rename.clone(),
..Default::default()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ TypeContext {
rename: RenameAttr {
pattern: None,
},
c_rename: RenameAttr {
abi_rename: RenameAttr {
pattern: None,
},
},
Expand All @@ -106,7 +106,7 @@ TypeContext {
rename: RenameAttr {
pattern: None,
},
c_rename: RenameAttr {
abi_rename: RenameAttr {
pattern: None,
},
},
Expand Down Expand Up @@ -213,7 +213,7 @@ TypeContext {
rename: RenameAttr {
pattern: None,
},
c_rename: RenameAttr {
abi_rename: RenameAttr {
pattern: None,
},
},
Expand All @@ -224,7 +224,7 @@ TypeContext {
rename: RenameAttr {
pattern: None,
},
c_rename: RenameAttr {
abi_rename: RenameAttr {
pattern: None,
},
},
Expand Down Expand Up @@ -253,7 +253,7 @@ TypeContext {
rename: RenameAttr {
pattern: None,
},
c_rename: RenameAttr {
abi_rename: RenameAttr {
pattern: None,
},
},
Expand Down
2 changes: 1 addition & 1 deletion feature_tests/c/include/AttrOpaque1.h

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

2 changes: 1 addition & 1 deletion feature_tests/c2/include/AttrOpaque1.h

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

2 changes: 1 addition & 1 deletion feature_tests/cpp/docs/source/attrs_ffi.rst

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

2 changes: 1 addition & 1 deletion feature_tests/cpp/include/AttrOpaque1.h

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

6 changes: 3 additions & 3 deletions feature_tests/cpp/include/AttrOpaque1.hpp

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

4 changes: 2 additions & 2 deletions feature_tests/cpp/tests/attrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ int main(int argc, char *argv[]) {
AttrOpaque1 o = AttrOpaque1::new_();
// the cpp2 renames don't apply. However, these must link correctly!!
simple_assert_eq("method should call", o.method(), 77);
simple_assert_eq("method should call", o.crenamed(), 123);
simple_assert_eq("method should call", o.abirenamed(), 123);

// These C names should also resolve
void* renamed = (void*)capi::renamed_in_c_only;
void* renamed = (void*)capi::renamed_on_abi_only;
std::cout<<"Renamed function at "<<renamed<<std::endl;
renamed = (void*)capi::namespace_AttrOpaque1_method;
std::cout<<"Renamed function at "<<renamed<<std::endl;
Expand Down
2 changes: 1 addition & 1 deletion feature_tests/cpp2/include/AttrEnum.d.hpp

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

2 changes: 1 addition & 1 deletion feature_tests/cpp2/include/AttrEnum.hpp

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

2 changes: 1 addition & 1 deletion feature_tests/cpp2/include/AttrOpaque1.h

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

2 changes: 1 addition & 1 deletion feature_tests/cpp2/include/AttrOpaque1Renamed.d.hpp

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

4 changes: 2 additions & 2 deletions feature_tests/cpp2/include/AttrOpaque1Renamed.hpp

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

4 changes: 2 additions & 2 deletions feature_tests/cpp2/tests/attrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
int main(int argc, char *argv[]) {
std::unique_ptr<AttrOpaque1Renamed> r = AttrOpaque1Renamed::totally_not_new();
simple_assert_eq("method should call", r->method_renamed(), 77);
simple_assert_eq("method should call", r->crenamed(), 123);
simple_assert_eq("method should call", r->abirenamed(), 123);

// These C names should also resolve
void* renamed = (void*)capi::renamed_in_c_only;
void* renamed = (void*)capi::renamed_on_abi_only;
std::cout<<"Renamed function at "<<renamed<<std::endl;
renamed = (void*)capi::namespace_AttrOpaque1_method;
std::cout<<"Renamed function at "<<renamed<<std::endl;
Expand Down
8 changes: 4 additions & 4 deletions feature_tests/dart/lib/src/AttrOpaque1.g.dart

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

4 changes: 2 additions & 2 deletions feature_tests/dotnet/Lib/Generated/AttrOpaque1.cs

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

4 changes: 2 additions & 2 deletions feature_tests/dotnet/Lib/Generated/RawAttrOpaque1.cs

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

2 changes: 1 addition & 1 deletion feature_tests/js/api/AttrOpaque1.d.ts

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

4 changes: 2 additions & 2 deletions feature_tests/js/api/AttrOpaque1.mjs

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

2 changes: 1 addition & 1 deletion feature_tests/js/docs/source/attrs_ffi.rst

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

Loading

0 comments on commit e16e740

Please sign in to comment.