Skip to content

Commit

Permalink
Extract mtype.Type.constConv and move it to typesem.d (#16675)
Browse files Browse the repository at this point in the history
  • Loading branch information
RazvanN7 authored Jul 9, 2024
1 parent 222d095 commit 40ea009
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 158 deletions.
12 changes: 12 additions & 0 deletions compiler/src/dmd/cxxfrontend.d
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,18 @@ uinteger_t size(Type type, const ref Loc loc)
return dmd.typesem.size(type, loc);
}

MATCH implicitConvTo(Type from, Type to)
{
import dmd.dcast;
return dmd.dcast.implicitConvTo(from, to);
}

MATCH constConv(Type from, Type to)
{
import dmd.typesem;
return dmd.typesem.constConv(from, to);
}

/***********************************************************
* typinf.d
*/
Expand Down
10 changes: 0 additions & 10 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,6 @@ class Type : public ASTNode
virtual Type* makeSharedWildConst();
virtual Type* makeMutable();
Type* toBasetype();
virtual MATCH constConv(Type* to);
virtual uint8_t deduceWild(Type* t, bool isRef);
virtual ClassDeclaration* isClassHandle();
virtual structalign_t alignment();
Expand Down Expand Up @@ -4195,7 +4194,6 @@ class TypeNext : public Type
Type* makeSharedWild() final override;
Type* makeSharedWildConst() final override;
Type* makeMutable() final override;
MATCH constConv(Type* to) override;
uint8_t deduceWild(Type* t, bool isRef) final override;
void transitive();
void accept(Visitor* v) override;
Expand All @@ -4217,7 +4215,6 @@ class TypeAArray final : public TypeArray
TypeAArray* syntaxCopy() override;
bool isZeroInit(const Loc& loc) override;
bool isBoolean() override;
MATCH constConv(Type* to) override;
void accept(Visitor* v) override;
};

Expand Down Expand Up @@ -4261,7 +4258,6 @@ class TypeClass final : public Type
const char* kind() const override;
TypeClass* syntaxCopy() override;
ClassDeclaration* isClassHandle() override;
MATCH constConv(Type* to) override;
uint8_t deduceWild(Type* t, bool isRef) override;
bool isZeroInit(const Loc& loc) override;
bool isscope() override;
Expand Down Expand Up @@ -4314,7 +4310,6 @@ class TypeEnum final : public Type
bool needsDestruction() override;
bool needsCopyOrPostblit() override;
bool needsNested() override;
MATCH constConv(Type* to) override;
bool isZeroInit(const Loc& loc) override;
bool hasVoidInitPointers() override;
bool hasUnsafeBitpatterns() override;
Expand Down Expand Up @@ -4446,7 +4441,6 @@ class TypeFunction final : public TypeNext
TypeFunction* syntaxCopy() override;
bool hasLazyParameters();
bool isDstyleVariadic() const;
MATCH constConv(Type* to) override;
bool iswild() const;
void accept(Visitor* v) override;
};
Expand Down Expand Up @@ -4496,7 +4490,6 @@ class TypeNoreturn final : public Type
public:
const char* kind() const override;
TypeNoreturn* syntaxCopy() override;
MATCH constConv(Type* to) override;
bool isBoolean() override;
uint32_t alignsize() override;
void accept(Visitor* v) override;
Expand All @@ -4517,7 +4510,6 @@ class TypePointer final : public TypeNext
static TypePointer* create(Type* t);
const char* kind() const override;
TypePointer* syntaxCopy() override;
MATCH constConv(Type* to) override;
bool isscalar() override;
bool isZeroInit(const Loc& loc) override;
void accept(Visitor* v) override;
Expand Down Expand Up @@ -4551,7 +4543,6 @@ class TypeSArray final : public TypeArray
bool isString() override;
bool isZeroInit(const Loc& loc) override;
structalign_t alignment() override;
MATCH constConv(Type* to) override;
Expression* defaultInitLiteral(const Loc& loc) override;
bool hasUnsafeBitpatterns() override;
bool hasVoidInitPointers() override;
Expand Down Expand Up @@ -4593,7 +4584,6 @@ class TypeStruct final : public Type
bool hasVoidInitPointers() override;
bool hasUnsafeBitpatterns() override;
bool hasInvariant() override;
MATCH constConv(Type* to) override;
uint8_t deduceWild(Type* t, bool isRef) override;
void accept(Visitor* v) override;
};
Expand Down
139 changes: 1 addition & 138 deletions compiler/src/dmd/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -1282,24 +1282,6 @@ extern (C++) abstract class Type : ASTNode
return ((te = isTypeEnum()) !is null) ? te.toBasetype2() : this;
}

/*******************************
* Determine if converting 'this' to 'to' is an identity operation,
* a conversion to const operation, or the types aren't the same.
* Returns:
* MATCH.exact 'this' == 'to'
* MATCH.constant 'to' is const
* MATCH.nomatch conversion to mutable or invariant
*/
MATCH constConv(Type to)
{
//printf("Type::constConv(this = %s, to = %s)\n", toChars(), to.toChars());
if (equals(to))
return MATCH.exact;
if (ty == to.ty && MODimplicitConv(mod, to.mod))
return MATCH.constant;
return MATCH.nomatch;
}

/***************************************
* Compute MOD bits matching `this` argument type to wild parameter type.
* Params:
Expand Down Expand Up @@ -1866,35 +1848,6 @@ extern (C++) abstract class TypeNext : Type
return t;
}

override MATCH constConv(Type to)
{
//printf("TypeNext::constConv from = %s, to = %s\n", toChars(), to.toChars());
if (equals(to))
return MATCH.exact;

if (!(ty == to.ty && MODimplicitConv(mod, to.mod)))
return MATCH.nomatch;

Type tn = to.nextOf();
if (!(tn && next.ty == tn.ty))
return MATCH.nomatch;

MATCH m;
if (to.isConst()) // whole tail const conversion
{
// Recursive shared level check
m = next.constConv(tn);
if (m == MATCH.exact)
m = MATCH.constant;
}
else
{
//printf("\tnext => %s, to.next => %s\n", next.toChars(), tn.toChars());
m = next.equals(tn) ? MATCH.constant : MATCH.nomatch;
}
return m;
}

override final MOD deduceWild(Type t, bool isRef)
{
if (ty == Tfunction)
Expand Down Expand Up @@ -2337,16 +2290,6 @@ extern (C++) final class TypeSArray : TypeArray
return next.alignment();
}

override MATCH constConv(Type to)
{
if (auto tsa = to.isTypeSArray())
{
if (!dim.equals(tsa.dim))
return MATCH.nomatch;
}
return TypeNext.constConv(to);
}

override Expression defaultInitLiteral(const ref Loc loc)
{
static if (LOGDEFAULTINIT)
Expand Down Expand Up @@ -2506,18 +2449,6 @@ extern (C++) final class TypeAArray : TypeArray
return true;
}

override MATCH constConv(Type to)
{
if (auto taa = to.isTypeAArray())
{
MATCH mindex = index.constConv(taa.index);
MATCH mkey = next.constConv(taa.next);
// Pick the worst match
return mkey < mindex ? mkey : mindex;
}
return Type.constConv(to);
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -2554,18 +2485,6 @@ extern (C++) final class TypePointer : TypeNext
return result;
}

override MATCH constConv(Type to)
{
if (next.ty == Tfunction)
{
if (to.nextOf() && next.equals((cast(TypeNext)to).next))
return Type.constConv(to);
else
return MATCH.nomatch;
}
return TypeNext.constConv(to);
}

override bool isscalar()
{
return true;
Expand Down Expand Up @@ -2868,16 +2787,6 @@ extern (C++) final class TypeFunction : TypeNext
return newArgs;
}

/** Extends TypeNext.constConv by also checking for matching attributes **/
override MATCH constConv(Type to)
{
// Attributes need to match exactly, otherwise it's an implicit conversion
if (this.ty != to.ty || !this.attributesEqual(cast(TypeFunction) to))
return MATCH.nomatch;

return super.constConv(to);
}

extern (D) bool checkRetType(const ref Loc loc)
{
Type tb = next.toBasetype();
Expand Down Expand Up @@ -3555,15 +3464,6 @@ extern (C++) final class TypeStruct : Type
return MATCH.nomatch;
}

override MATCH constConv(Type to)
{
if (equals(to))
return MATCH.exact;
if (ty == to.ty && sym == (cast(TypeStruct)to).sym && MODimplicitConv(mod, to.mod))
return MATCH.constant;
return MATCH.nomatch;
}

override MOD deduceWild(Type t, bool isRef)
{
if (ty == t.ty && sym == (cast(TypeStruct)t).sym)
Expand Down Expand Up @@ -3690,15 +3590,6 @@ extern (C++) final class TypeEnum : Type
return memType().needsNested();
}

override MATCH constConv(Type to)
{
if (equals(to))
return MATCH.exact;
if (ty == to.ty && sym == (cast(TypeEnum)to).sym && MODimplicitConv(mod, to.mod))
return MATCH.constant;
return MATCH.nomatch;
}

extern (D) Type toBasetype2()
{
if (!sym.members && !sym.memtype)
Expand Down Expand Up @@ -3770,7 +3661,7 @@ extern (C++) final class TypeClass : Type
extern (D) MATCH implicitConvToWithoutAliasThis(Type to)
{
ClassDeclaration cdto = to.isClassHandle();
MATCH m = constConv(to);
MATCH m = constConv(this, to);
if (m > MATCH.nomatch)
return m;

Expand All @@ -3797,28 +3688,6 @@ extern (C++) final class TypeClass : Type
return m;
}

override MATCH constConv(Type to)
{
if (equals(to))
return MATCH.exact;
if (ty == to.ty && sym == (cast(TypeClass)to).sym && MODimplicitConv(mod, to.mod))
return MATCH.constant;

/* Conversion derived to const(base)
*/
int offset = 0;
if (to.isBaseOf(this, &offset) && offset == 0 && MODimplicitConv(mod, to.mod))
{
// Disallow:
// derived to base
// inout(derived) to inout(base)
if (!to.isMutable() && !to.isWild())
return MATCH.convert;
}

return MATCH.nomatch;
}

override MOD deduceWild(Type t, bool isRef)
{
ClassDeclaration cd = t.isClassHandle();
Expand Down Expand Up @@ -4088,12 +3957,6 @@ extern (C++) final class TypeNoreturn : Type
return this;
}

override MATCH constConv(Type to)
{
// Either another noreturn or conversion to any type
return this.implicitConvTo(to);
}

override bool isBoolean()
{
return true; // bottom type can be implicitly converted to any other type
Expand Down
Loading

0 comments on commit 40ea009

Please sign in to comment.