Skip to content

Commit 50346ab

Browse files
[mtype.d] Devirtualise Type.equals (#21785)
1 parent 2506210 commit 50346ab

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

compiler/src/dmd/frontend.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,7 +2079,7 @@ class Type : public ASTNode
20792079
virtual const char* kind() const;
20802080
Type* copy() const;
20812081
virtual Type* syntaxCopy();
2082-
virtual bool equals(const Type* const t) const;
2082+
bool equals(const Type* const t) const;
20832083
DYNCAST dyncast() const final override;
20842084
size_t getUniqueID() const;
20852085
const char* toChars() const final override;
@@ -4876,7 +4876,6 @@ class TypeTuple final : public Type
48764876
static TypeTuple* create(Type* t1, Type* t2);
48774877
const char* kind() const override;
48784878
TypeTuple* syntaxCopy() override;
4879-
bool equals(const Type* const t) const override;
48804879
void accept(Visitor* v) override;
48814880
};
48824881

compiler/src/dmd/mtype.d

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -424,12 +424,31 @@ extern (C++) abstract class Type : ASTNode
424424
assert(0);
425425
}
426426

427-
bool equals(const Type t) const
427+
final bool equals(const Type t) const
428428
{
429429
//printf("Type::equals(%s, %s)\n", toChars(), t.toChars());
430+
if (this == t)
431+
return true;
432+
if (ty == Ttuple)
433+
{
434+
if (t.ty != Ttuple)
435+
return false;
436+
auto t1 = this.isTypeTuple();
437+
auto t2 = t.isTypeTuple();
438+
if (t1.arguments.length != t2.arguments.length)
439+
return false;
440+
for (size_t i = 0; i < t1.arguments.length; i++)
441+
{
442+
const Parameter arg1 = (*t1.arguments)[i];
443+
const Parameter arg2 = (*t2.arguments)[i];
444+
if (!arg1.type.equals(arg2.type))
445+
return false;
446+
}
447+
return true;
448+
}
430449
// deco strings are unique
431450
// and semantic() has been run
432-
if (this == t || ((t && deco == t.deco) && deco !is null))
451+
if ((t && deco == t.deco) && deco !is null)
433452
{
434453
//printf("deco = '%s', t.deco = '%s'\n", deco, t.deco);
435454
return true;
@@ -3330,28 +3349,6 @@ extern (C++) final class TypeTuple : Type
33303349
return t;
33313350
}
33323351

3333-
override bool equals(const Type t) const
3334-
{
3335-
//printf("TypeTuple::equals(%s, %s)\n", toChars(), t.toChars());
3336-
if (this == t)
3337-
return true;
3338-
if (auto tt = t.isTypeTuple())
3339-
{
3340-
if (arguments.length == tt.arguments.length)
3341-
{
3342-
for (size_t i = 0; i < tt.arguments.length; i++)
3343-
{
3344-
const Parameter arg1 = (*arguments)[i];
3345-
const Parameter arg2 = (*tt.arguments)[i];
3346-
if (!arg1.type.equals(arg2.type))
3347-
return false;
3348-
}
3349-
return true;
3350-
}
3351-
}
3352-
return false;
3353-
}
3354-
33553352
override void accept(Visitor v)
33563353
{
33573354
v.visit(this);

compiler/src/dmd/mtype.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class Type : public ASTNode
218218
virtual const char *kind();
219219
Type *copy() const;
220220
virtual Type *syntaxCopy();
221-
virtual bool equals(const Type * const t) const;
221+
bool equals(const Type * const t) const;
222222
// kludge for template.isType()
223223
DYNCAST dyncast() const override final { return DYNCAST_TYPE; }
224224
size_t getUniqueID() const;
@@ -759,7 +759,6 @@ class TypeTuple final : public Type
759759
static TypeTuple *create(Type *t1, Type *t2);
760760
const char *kind() override;
761761
TypeTuple *syntaxCopy() override;
762-
bool equals(const Type * const t) const override;
763762
void accept(Visitor *v) override { v->visit(this); }
764763
};
765764

0 commit comments

Comments
 (0)