Skip to content

Commit f3b568d

Browse files
[dtemplate.d] move equalsx to templatesem.d (dlang/dmd!21736)
1 parent a57ce45 commit f3b568d

File tree

3 files changed

+82
-79
lines changed

3 files changed

+82
-79
lines changed

dmd/dtemplate.d

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ import dmd.optimize;
7272
import dmd.root.array;
7373
import dmd.common.outbuffer;
7474
import dmd.rootobject;
75-
import dmd.templatesem : getExpression, TemplateInstance_semanticTiargs, arrayObjectMatch;
75+
import dmd.templatesem : getExpression, TemplateInstance_semanticTiargs, equalsx;
7676
import dmd.tokens;
77-
import dmd.typesem : typeSemantic, isBaseOf, resolveNamedArgs;
77+
import dmd.typesem : typeSemantic, isBaseOf;
7878
import dmd.visitor;
7979

8080
//debug = FindExistingInstance; // print debug stats of findExistingInstance
@@ -1725,82 +1725,6 @@ extern (C++) class TemplateInstance : ScopeDsymbol
17251725
return ident;
17261726
}
17271727

1728-
/*************************************
1729-
* Compare proposed template instantiation with existing template instantiation.
1730-
* Note that this is not commutative because of the auto ref check.
1731-
* Params:
1732-
* ti = existing template instantiation
1733-
* Returns:
1734-
* true for match
1735-
*/
1736-
final bool equalsx(TemplateInstance ti)
1737-
{
1738-
//printf("this = %p, ti = %p\n", this, ti);
1739-
assert(tdtypes.length == ti.tdtypes.length);
1740-
1741-
// Nesting must match
1742-
if (enclosing != ti.enclosing)
1743-
{
1744-
//printf("test2 enclosing %s ti.enclosing %s\n", enclosing ? enclosing.toChars() : "", ti.enclosing ? ti.enclosing.toChars() : "");
1745-
return false;
1746-
}
1747-
//printf("parent = %s, ti.parent = %s\n", parent.toPrettyChars(), ti.parent.toPrettyChars());
1748-
1749-
if (!arrayObjectMatch(tdtypes, ti.tdtypes))
1750-
return false;
1751-
1752-
/* Template functions may have different instantiations based on
1753-
* "auto ref" parameters.
1754-
*/
1755-
auto fd = ti.toAlias().isFuncDeclaration();
1756-
if (!fd)
1757-
return true;
1758-
if (fd.errors)
1759-
return true;
1760-
1761-
auto resolvedArgs = fd.type.isTypeFunction().resolveNamedArgs(
1762-
ArgumentList(this.fargs, this.fnames), null);
1763-
1764-
// resolvedArgs can be null when there's an error: fail_compilation/fail14669.d
1765-
// In that case, equalsx returns true to prevent endless template instantiations
1766-
// However, it can also mean the function was explicitly instantiated
1767-
// without function arguments: fail_compilation/fail14669
1768-
// Hence the following check:
1769-
if (this.fargs && !resolvedArgs)
1770-
return true;
1771-
1772-
Expression[] args = resolvedArgs ? (*resolvedArgs)[] : [];
1773-
1774-
auto fparameters = fd.getParameterList();
1775-
size_t nfparams = fparameters.length; // Num function parameters
1776-
for (size_t j = 0; j < nfparams; j++)
1777-
{
1778-
Parameter fparam = fparameters[j];
1779-
if (!(fparam.storageClass & STC.autoref) ) // if "auto ref"
1780-
continue;
1781-
1782-
Expression farg = (j < args.length) ? args[j] : fparam.defaultArg;
1783-
// resolveNamedArgs strips trailing nulls / default params
1784-
// when it doesn't anymore, the ternary can be replaced with:
1785-
// assert(j < resolvedArgs.length);
1786-
if (!farg)
1787-
farg = fparam.defaultArg;
1788-
if (!farg)
1789-
return false;
1790-
if (farg.isLvalue())
1791-
{
1792-
if (!(fparam.storageClass & STC.ref_))
1793-
return false; // auto ref's don't match
1794-
}
1795-
else
1796-
{
1797-
if (fparam.storageClass & STC.ref_)
1798-
return false; // auto ref's don't match
1799-
}
1800-
}
1801-
return true;
1802-
}
1803-
18041728
extern (D) final size_t toHash()
18051729
{
18061730
if (!hash)

dmd/frontend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,6 @@ class TemplateInstance : public ScopeDsymbol
18161816
const char* kind() const override;
18171817
const char* toPrettyCharsHelper() final override;
18181818
Identifier* getIdent() final override;
1819-
bool equalsx(TemplateInstance* ti);
18201819
void accept(Visitor* v) override;
18211820
};
18221821

dmd/templatesem.d

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,86 @@ bool arrayObjectMatch(ref Objects oa1, ref Objects oa2)
342342
return true;
343343
}
344344

345+
346+
/*************************************
347+
* Compare proposed template instantiation with existing template instantiation.
348+
* Note that this is not commutative because of the auto ref check.
349+
* Params:
350+
* ti1 = proposed template instantiation
351+
* ti2 = existing template instantiation
352+
* Returns:
353+
* true for match
354+
*/
355+
bool equalsx(TemplateInstance ti1, TemplateInstance ti2)
356+
{
357+
//printf("this = %p, ti2 = %p\n", this, ti2);
358+
assert(ti1.tdtypes.length == ti2.tdtypes.length);
359+
360+
// Nesting must match
361+
if (ti1.enclosing != ti2.enclosing)
362+
{
363+
//printf("test2 enclosing %s ti2.enclosing %s\n",
364+
// enclosing ? enclosing.toChars() : "", ti2.enclosing ? ti.enclosing.toChars() : "");
365+
return false;
366+
}
367+
//printf("parent = %s, ti2.parent = %s\n", parent.toPrettyChars(), ti2.parent.toPrettyChars());
368+
369+
if (!arrayObjectMatch(ti1.tdtypes, ti2.tdtypes))
370+
return false;
371+
372+
/* Template functions may have different instantiations based on
373+
* "auto ref" parameters.
374+
*/
375+
auto fd = ti2.toAlias().isFuncDeclaration();
376+
if (!fd)
377+
return true;
378+
if (fd.errors)
379+
return true;
380+
381+
auto resolvedArgs = fd.type.isTypeFunction().resolveNamedArgs(
382+
ArgumentList(ti1.fargs, ti1.fnames), null);
383+
384+
// resolvedArgs can be null when there's an error: fail_compilation/fail14669.d
385+
// In that case, equalsx returns true to prevent endless template instantiations
386+
// However, it can also mean the function was explicitly instantiated
387+
// without function arguments: fail_compilation/fail14669
388+
// Hence the following check:
389+
if (ti1.fargs && !resolvedArgs)
390+
return true;
391+
392+
Expression[] args = resolvedArgs ? (*resolvedArgs)[] : [];
393+
394+
auto fparameters = fd.getParameterList();
395+
size_t nfparams = fparameters.length; // Num function parameters
396+
for (size_t j = 0; j < nfparams; j++)
397+
{
398+
Parameter fparam = fparameters[j];
399+
if (!(fparam.storageClass & STC.autoref) ) // if "auto ref"
400+
continue;
401+
402+
Expression farg = (j < args.length) ? args[j] : fparam.defaultArg;
403+
// resolveNamedArgs strips trailing nulls / default params
404+
// when it doesn't anymore, the ternary can be replaced with:
405+
// assert(j < resolvedArgs.length);
406+
if (!farg)
407+
farg = fparam.defaultArg;
408+
if (!farg)
409+
return false;
410+
if (farg.isLvalue())
411+
{
412+
if (!(fparam.storageClass & STC.ref_))
413+
return false; // auto ref's don't match
414+
}
415+
else
416+
{
417+
if (fparam.storageClass & STC.ref_)
418+
return false; // auto ref's don't match
419+
}
420+
}
421+
return true;
422+
}
423+
424+
345425
/*******************************************
346426
* Match to a particular TemplateParameter.
347427
* Input:

0 commit comments

Comments
 (0)