Skip to content

Commit

Permalink
[mtype.d] move numberOfElems and checkRetType to typesem.d
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Oct 3, 2024
1 parent cdd2ad7 commit 74578d1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 57 deletions.
57 changes: 0 additions & 57 deletions compiler/src/dmd/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -1398,33 +1398,6 @@ extern (C++) abstract class Type : ASTNode
return t;
}

/*******************************************
* Compute number of elements for a (possibly multidimensional) static array,
* or 1 for other types.
* Params:
* loc = for error message
* Returns:
* number of elements, uint.max on overflow
*/
final uint numberOfElems(const ref Loc loc)
{
//printf("Type::numberOfElems()\n");
uinteger_t n = 1;
Type tb = this;
while ((tb = tb.toBasetype()).ty == Tsarray)
{
bool overflow = false;
n = mulu(n, (cast(TypeSArray)tb).dim.toUInteger(), overflow);
if (overflow || n >= uint.max)
{
error(loc, "static array `%s` size overflowed to %llu", toChars(), cast(ulong)n);
return uint.max;
}
tb = (cast(TypeSArray)tb).next;
}
return cast(uint)n;
}

/****************************************
* Return the mask that an integral type will
* fit into.
Expand Down Expand Up @@ -2724,36 +2697,6 @@ extern (C++) final class TypeFunction : TypeNext
return newArgs;
}

extern (D) bool checkRetType(const ref Loc loc)
{
Type tb = next.toBasetype();
if (tb.ty == Tfunction)
{
error(loc, "functions cannot return a function");
next = Type.terror;
}
if (tb.ty == Ttuple)
{
error(loc, "functions cannot return a sequence (use `std.typecons.Tuple`)");
next = Type.terror;
}
if (!isRef && (tb.ty == Tstruct || tb.ty == Tsarray))
{
if (auto ts = tb.baseElemOf().isTypeStruct())
{
if (!ts.sym.members)
{
error(loc, "functions cannot return opaque type `%s` by value", tb.toChars());
next = Type.terror;
}
}
}
if (tb.ty == Terror)
return true;
return false;
}


/// Returns: `true` the function is `isInOutQual` or `isInOutParam` ,`false` otherwise.
bool iswild() const pure nothrow @safe @nogc
{
Expand Down
56 changes: 56 additions & 0 deletions compiler/src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -7479,6 +7479,62 @@ MATCH implicitConvToThroughAliasThis(TypeStruct from, Type to)
return MATCH.nomatch;
}

/*******************************************
* Compute number of elements for a (possibly multidimensional) static array,
* or 1 for other types.
* Params:
* t = static array type
* loc = for error message
* Returns:
* number of elements, uint.max on overflow
*/
uint numberOfElems(Type t, const ref Loc loc)
{
//printf("Type::numberOfElems()\n");
uinteger_t n = 1;
Type tb = t;
while ((tb = tb.toBasetype()).ty == Tsarray)
{
bool overflow = false;
n = mulu(n, (cast(TypeSArray)tb).dim.toUInteger(), overflow);
if (overflow || n >= uint.max)
{
error(loc, "static array `%s` size overflowed to %llu", t.toChars(), cast(ulong)n);
return uint.max;
}
tb = (cast(TypeSArray)tb).next;
}
return cast(uint)n;
}

bool checkRetType(TypeFunction tf, const ref Loc loc)
{
Type tb = tf.next.toBasetype();
if (tb.ty == Tfunction)
{
error(loc, "functions cannot return a function");
tf.next = Type.terror;
}
if (tb.ty == Ttuple)
{
error(loc, "functions cannot return a sequence (use `std.typecons.Tuple`)");
tf.next = Type.terror;
}
if (!tf.isRef && (tb.ty == Tstruct || tb.ty == Tsarray))
{
if (auto ts = tb.baseElemOf().isTypeStruct())
{
if (!ts.sym.members)
{
error(loc, "functions cannot return opaque type `%s` by value", tb.toChars());
tf.next = Type.terror;
}
}
}
if (tb.ty == Terror)
return true;
return false;
}


/******************************* Private *****************************************/
Expand Down

0 comments on commit 74578d1

Please sign in to comment.