Skip to content

Commit

Permalink
moving to use stringifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen Arteca committed Dec 3, 2024
1 parent 4e5d9b7 commit 86863d4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
28 changes: 4 additions & 24 deletions tool/src/kotlin/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ pub(super) struct KotlinFormatter<'tcx> {
docs_url_gen: &'tcx DocsUrlGenerator,
}

const INVALID_RAW_METHOD_NAMES: &[&str] = &[
const INVALID_METHOD_NAMES: &[&str] = &[
"new", "static", "default", "private", "internal", "toString",
];
const DISALLOWED_CORE_TYPES: &[&str] = &["Object", "String"];
const OVERRIDE_METHOD_SIGS: &[&str] = &["fun toString(): String"];

impl<'tcx> KotlinFormatter<'tcx> {
pub fn new(
Expand Down Expand Up @@ -113,37 +112,18 @@ impl<'tcx> KotlinFormatter<'tcx> {
}
}

pub fn fmt_method_name<'a>(
&self,
method: &'a hir::Method,
should_be_overridden: bool,
) -> Cow<'a, str> {
pub fn fmt_method_name<'a>(&self, method: &'a hir::Method) -> Cow<'a, str> {
// TODO(#60): handle other keywords

let name = method.name.as_str().to_lower_camel_case();
let name = method.attrs.rename.apply(name.into());
if INVALID_RAW_METHOD_NAMES.contains(&&*name) && !should_be_overridden {
if INVALID_METHOD_NAMES.contains(&&*name) {
format!("{name}_").into()
} else {
name
}
}

pub fn method_should_be_overridden(
&self,
method: &hir::Method,
params: &str,
return_type: &str,
) -> bool {
let method_sig = format!(
"fun {}({}): {}",
&&*method.name.as_str().to_lower_camel_case(),
params,
return_type
);
OVERRIDE_METHOD_SIGS.contains(&&*method_sig)
}

pub fn fmt_trait_method_name<'a>(&self, method: &'a hir::Callback) -> Cow<'a, str> {
if method.name.is_none() {
panic!("Trait methods need a name");
Expand All @@ -154,7 +134,7 @@ impl<'tcx> KotlinFormatter<'tcx> {
} else {
name.into()
};
if INVALID_RAW_METHOD_NAMES.contains(&&*name) {
if INVALID_METHOD_NAMES.contains(&&*name) {
format!("{name}_").into()
} else {
name
Expand Down
24 changes: 13 additions & 11 deletions tool/src/kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn attr_support() -> BackendAttrSupport {
a.named_constructors = false; // TODO
a.fallible_constructors = false; // TODO
a.accessors = false;
a.stringifiers = false; // TODO
a.stringifiers = true;
a.comparators = false; // TODO
a.iterators = true;
a.iterables = true;
Expand Down Expand Up @@ -1188,18 +1188,18 @@ returnVal.option() ?: return null
panic!("Can only have one iterable method per opaque struct")
}
}
Some(SpecialMethod::Stringifier) => {
if !special_methods.has_stringifier {
special_methods.has_stringifier = true;
format!("override fun toString(): String")
} else {
panic!("Can only have one stringifier method per opaque struct")
}
}
_ => {
let should_be_overridden =
self.formatter
.method_should_be_overridden(method, &params, return_ty.as_ref());
format!(
"{}fun {}({}): {return_ty}",
if should_be_overridden {
"override "
} else {
""
},
self.formatter.fmt_method_name(method, should_be_overridden),
"fun {}({}): {return_ty}",
self.formatter.fmt_method_name(method),
params
)
}
Expand Down Expand Up @@ -1911,6 +1911,7 @@ struct SpecialMethods {
iterator_type: Option<String>,
indexer_type: Option<IndexerType>,
iterable_type: Option<String>,
has_stringifier: bool,
}

struct IndexerType {
Expand All @@ -1931,6 +1932,7 @@ impl SpecialMethodsImpl {
iterator_type,
indexer_type,
iterable_type,
has_stringifier: _,
}: SpecialMethods,
) -> Self {
let interfaces = iterator_type
Expand Down

0 comments on commit 86863d4

Please sign in to comment.