Skip to content

Commit

Permalink
API Notes: Add support for having the this parameter anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-carlborg committed Apr 19, 2024
1 parent ae368d3 commit 6aed199
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
10 changes: 5 additions & 5 deletions dstep/translator/ApiNotes.d
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ struct Function

bool isInstanceMethod() =>
context.isPresent &&
arguments.length > 0 &&
(
arguments[0] == "this" ||
arguments[0] == "self"
);
!arguments.save.find!isThis.empty;

auto indexOfThis() => arguments.save.countUntil!isThis;

private:

Expand Down Expand Up @@ -234,3 +232,5 @@ mixin template ToString()
return format!"%s(%-(%s, %))"(typeof(this).stringof, formattedFields);
}
}

bool isThis(string value) => value == "this" || value == "self";
17 changes: 16 additions & 1 deletion dstep/translator/ApiNotesTranslator.d
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,23 @@ struct ApiNotesTranslator
const firstParamType = cursor.func.parameters.first.type;
const thisArg = firstParamType.isPointer ? "&this" : "this";


string[] parameterNames;
parameterNames.reserve(cursor.func.parameters.length);
size_t paramCount;

foreach (param ; cursor.func.parameters)
{
if (paramCount == func.indexOfThis)
parameterNames ~= param.type.isPointer ? "&this" : "this";
else
parameterNames ~= translateIdentifier(param.spelling);

paramCount++;
}

output.subscopeStrong(wrapperResult.extent, wrapperResult.makeString) in {
output.singleLine("return %s(%s, __traits(parameters));", originalName, thisArg);
output.singleLine("return %s(%-(%s, %));", originalName, parameterNames);
};

addOriginalFunction(cursor, output, originalName);
Expand Down
7 changes: 5 additions & 2 deletions dstep/translator/Translator.d
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,11 @@ SourceNode translateFunction (
params ~= Parameter(type, param.spelling);
}

const parameterStart = func.apiNotesFunction.isInstanceMethod.or(false) ? 1 : 0;
params = params[parameterStart .. $];
if (func.apiNotesFunction.isInstanceMethod.or(false))
{
auto index = func.apiNotesFunction.indexOfThis.or(ptrdiff_t(-1));
params = params.remove(index);
}

auto returnType = func.canonicalizeReturnType ?
func.cursor.resultType.canonical : func.cursor.resultType;
Expand Down
39 changes: 33 additions & 6 deletions tests/unit/ApiNotes.d
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,25 @@ YAML";
{
@"when the D name contains a context"
{
@"when the D name contains a 'this'" unittest
@"when the D name of the first argument is 'this'" unittest
{
auto rawFunc = RawFunction(name: "foo", dName: "Foo.bar(this)");
auto rawFunc = RawFunction(name: "foo", dName: "Foo.bar(this:)");
auto func = Function.parse(rawFunc);

assert(func.isInstanceMethod);
}

@"when the D name contains a 'self'" unittest
@"when the D name of the first argument is 'self'" unittest
{
auto rawFunc = RawFunction(name: "foo", dName: "Foo.bar(self)");
auto rawFunc = RawFunction(name: "foo", dName: "Foo.bar(self:)");
auto func = Function.parse(rawFunc);

assert(func.isInstanceMethod);
}

@"when the D name of another argument is 'this'" unittest
{
auto rawFunc = RawFunction(name: "foo", dName: "Foo.bar(foo:this:bar:)");
auto func = Function.parse(rawFunc);

assert(func.isInstanceMethod);
Expand Down Expand Up @@ -232,6 +240,25 @@ YAML";
}
}

@"Function.indexOfThis"
{
@"when the D name contains 'this' or 'self'" unittest
{
auto rawFunc = RawFunction(name: "foo", dName: "Foo.bar(foo:this:bar:)");
auto func = Function.parse(rawFunc);

assert(func.indexOfThis == 1);
}

@"when the D name does not contain 'this' or 'self'" unittest
{
auto rawFunc = RawFunction(name: "foo", dName: "Foo.bar(foo:bar:)");
auto func = Function.parse(rawFunc);

assert(func.indexOfThis == -1);
}
}

@"renaming free function" unittest
{
auto options = Options(apiNotes:
Expand Down Expand Up @@ -276,7 +303,7 @@ struct Bar
{
void foo (int a)
{
return __foo(this, __traits(parameters));
return __foo(this, a);
}
extern (C) private static pragma(mangle, "foo")
Expand Down Expand Up @@ -306,7 +333,7 @@ struct Bar
{
void foo (int a)
{
return __foo(&this, __traits(parameters));
return __foo(&this, a);
}
extern (C) private static pragma(mangle, "foo")
Expand Down

0 comments on commit 6aed199

Please sign in to comment.