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

WIP: Improve overload resolution #11271

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 39 additions & 1 deletion src/dmd/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,50 @@ enum STCFlowThruFunction = ~(STC.auto_ | STC.scope_ | STC.static_ | STC.extern_
STC.TYPECTOR | STC.final_ | STC.tls | STC.gshared | STC.ref_ | STC.return_ | STC.property |
STC.nothrow_ | STC.pure_ | STC.safe | STC.trusted | STC.system); /// for a FuncDeclaration

/* Result of matching arguments to function parameters.
*/
struct MatchResult
{
static immutable noMatch = MatchResult([1, 0, 0]);

int[MATCH.max] counters; // exclude MATCH.exact

ref MatchResult push(MATCH match) return
{
if (match != MATCH.exact)
counters[match]++;
return this;
}

bool isMatch() const { return !counters[MATCH.nomatch]; }

// less means worse
int opCmp(const scope ref MatchResult rhs) const
{
if (!isMatch)
return !rhs.isMatch ? 0 : -1;
if (!rhs.isMatch)
return 1;

// prefer the one with less implicit conversions, then less const conversions
foreach (i; 1 .. counters.length)
{
const lhsConversions = counters[1] + counters[2];
const rhsConversions = rhs.counters[1] + rhs.counters[2];
if (const delta = rhs.counters[i] - counters[i])
return delta > 0 ? 1 : -1;
}

return 0;
}
}

/* Accumulator for successive matches.
*/
struct MatchAccumulator
{
int count; // number of matches found so far
MATCH last = MATCH.nomatch; // match level of lastf
MatchResult last = MatchResult.noMatch; // for lastf
FuncDeclaration lastf; // last matching function we found
FuncDeclaration nextf; // if ambiguous match, this is the "other" function
}
Expand Down
1 change: 1 addition & 0 deletions src/dmd/declaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ class FuncDeclaration : public Declaration
BaseClass *overrideInterface();
bool overloadInsert(Dsymbol *s);
bool inUnittest();
// TODO
MATCH leastAsSpecialized(FuncDeclaration *g);
LabelDsymbol *searchLabel(Identifier *ident);
int getLevel(FuncDeclaration *fd, int intypeof); // lexical nesting level difference
Expand Down
Loading