Skip to content

Commit

Permalink
API Notes: Add support for converting free functions to static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-carlborg committed Mar 9, 2024
1 parent 342a2b7 commit 9de9a81
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [Issue 288](https://github.com/jacob-carlborg/dstep/issues/288): Missing `__u/int128` support
* Add support for renaming global functions using API notes
* Add support for converting free functions to instance methods using API notes
* Add support for converting free functions to static methods using API notes

### Bugs Fixed

Expand Down
20 changes: 20 additions & 0 deletions dstep/translator/Translator.d
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,26 @@ class Translator
});
}

else if (func.isStaticMethod.or(false))
{
auto context = func.flatMap!(f => f.context).or("");

this.context.addAnnotatedMember(context, (Output output) {
auto function_ = Function(
cursor: cursor.func,
name: name,
mangledName: none!string, // handle below
apiNotesFunction: func,
isStatic: true
);

auto result = translateFunction(this.context, function_);
output.singleLine(`extern (C) pragma(mangle, "%s")`, cursor.mangling);
output.adaptiveSourceNode(result);
output.append(";");
});
}

else
{
auto function_ = Function(
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/ApiNotes.d
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,28 @@ struct Bar
D", options, annotatedFile: "Bar.d");
}
}

@"free function to static method" unittest
{
auto options = Options(apiNotes:
q"YAML
Functions:
- Name: foo
DName: Bar.foo(a:)
YAML"
);

assertTranslatesAnnotated(
q"C
struct Bar {};
void foo(int a);
C",
q"D
struct Bar
{
extern (C) pragma(mangle, "foo")
static void foo (int a);
}
D", options, annotatedFile: "Bar.d");
}

0 comments on commit 9de9a81

Please sign in to comment.