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

[mtype.d] move numberOfElems and checkRetType to typesem.d #16920

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
1 change: 0 additions & 1 deletion compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1878,7 +1878,6 @@ class Type : public ASTNode
virtual bool hasInvariant();
virtual Type* nextOf();
Type* baseElemOf();
uint32_t numberOfElems(const Loc& loc);
virtual bool needsDestruction();
virtual bool needsCopyOrPostblit();
virtual bool needsNested();
Expand Down
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
Loading