Skip to content

Commit

Permalink
move determineSize to dsymbolsem.d
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Oct 7, 2024
1 parent 57c5b07 commit b9a6ea9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 59 deletions.
60 changes: 1 addition & 59 deletions compiler/src/dmd/aggregate.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import dmd.declaration;
import dmd.dscope;
import dmd.dstruct;
import dmd.dsymbol;
import dmd.dsymbolsem : dsymbolSemantic, determineFields, search;
import dmd.dsymbolsem : dsymbolSemantic, determineFields, search, determineSize;
import dmd.dtemplate;
import dmd.errors;
import dmd.expression;
Expand Down Expand Up @@ -187,64 +187,6 @@ extern (C++) abstract class AggregateDeclaration : ScopeDsymbol
return fields.length - isNested() - (vthis2 !is null);
}

/***************************************
* Collect all instance fields, then determine instance size.
* Returns:
* false if failed to determine the size.
*/
extern (D) final bool determineSize(const ref Loc loc)
{
//printf("AggregateDeclaration::determineSize() %s, sizeok = %d\n", toChars(), sizeok);

// The previous instance size finalizing had:
if (type.ty == Terror || errors)
return false; // failed already
if (sizeok == Sizeok.done)
return true; // succeeded

if (!members)
{
.error(loc, "%s `%s` unknown size", kind, toPrettyChars);
return false;
}

if (_scope)
dsymbolSemantic(this, null);

// Determine the instance size of base class first.
if (auto cd = isClassDeclaration())
{
cd = cd.baseClass;
if (cd && !cd.determineSize(loc))
goto Lfail;
}

// Determine instance fields when sizeok == Sizeok.none
if (!this.determineFields())
goto Lfail;
if (sizeok != Sizeok.done)
finalizeSize();

// this aggregate type has:
if (type.ty == Terror)
return false; // marked as invalid during the finalizing.
if (sizeok == Sizeok.done)
return true; // succeeded to calculate instance size.

Lfail:
// There's unresolvable forward reference.
if (type != Type.terror)
error(loc, "%s `%s` no size because of forward reference", kind, toPrettyChars);
// Don't cache errors from speculative semantic, might be resolvable later.
// https://issues.dlang.org/show_bug.cgi?id=16574
if (!global.gag)
{
type = Type.terror;
errors = true;
}
return false;
}

abstract void finalizeSize();

override final uinteger_t size(const ref Loc loc)
Expand Down
59 changes: 59 additions & 0 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -7647,3 +7647,62 @@ private Expression callScopeDtor(VarDeclaration vd, Scope* sc)
}
return e;
}

/***************************************
* Collect all instance fields, then determine instance size.
* Returns:
* false if failed to determine the size.
*/
bool determineSize(AggregateDeclaration ad, const ref Loc loc)
{
//printf("AggregateDeclaration::determineSize() %s, sizeok = %d\n", toChars(), sizeok);

// The previous instance size finalizing had:
if (ad.type.ty == Terror || ad.errors)
return false; // failed already
if (ad.sizeok == Sizeok.done)
return true; // succeeded

if (!ad.members)
{
.error(loc, "%s `%s` unknown size", ad.kind, ad.toPrettyChars);
return false;
}

if (ad._scope)
dsymbolSemantic(ad, null);

// Determine the instance size of base class first.
if (auto cd = ad.isClassDeclaration())
{
cd = cd.baseClass;
if (cd && !cd.determineSize(loc))
goto Lfail;
}

// Determine instance fields when sizeok == Sizeok.none
if (!ad.determineFields())
goto Lfail;
if (ad.sizeok != Sizeok.done)
finalizeSize();

// this aggregate type has:
if (ad.type.ty == Terror)
return false; // marked as invalid during the finalizing.
if (ad.sizeok == Sizeok.done)
return true; // succeeded to calculate instance size.

Lfail:
// There's unresolvable forward reference.
if (ad.type != Type.terror)
error(loc, "%s `%s` no size because of forward reference", ad.kind, ad.toPrettyChars);
// Don't cache errors from speculative semantic, might be resolvable later.
// https://issues.dlang.org/show_bug.cgi?id=16574
if (!global.gag)
{
ad.type = Type.terror;
ad.errors = true;
}
return false;
}

0 comments on commit b9a6ea9

Please sign in to comment.