Skip to content

Commit

Permalink
Extract addStorageClass method from Type AST nodes and move it into t…
Browse files Browse the repository at this point in the history
…ypesem.d (#16191)
  • Loading branch information
RazvanN7 committed Feb 15, 2024
1 parent 1878df0 commit a952831
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 84 deletions.
6 changes: 6 additions & 0 deletions compiler/src/dmd/cxxfrontend.d
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,12 @@ Type addMod(Type type, MOD mod)
return dmd.typesem.addMod(type, mod);
}

Type addStorageClass(Type type, StorageClass stc)
{
import dmd.typesem;
return dmd.typesem.addStorageClass(type, stc);
}

Type pointerTo(Type type)
{
import dmd.typesem;
Expand Down
3 changes: 0 additions & 3 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,6 @@ class Type : public ASTNode
bool isSharedWild() const;
bool isNaked() const;
Type* nullAttributes() const;
virtual Type* addStorageClass(StorageClass stc);
Type* arrayOf();
Type* sarrayOf(dinteger_t dim);
bool hasDeprecatedAliasThis();
Expand Down Expand Up @@ -4364,7 +4363,6 @@ class TypeDelegate final : public TypeNext
static TypeDelegate* create(TypeFunction* t);
const char* kind() const override;
TypeDelegate* syntaxCopy() override;
Type* addStorageClass(StorageClass stc) override;
uinteger_t size(const Loc& loc) override;
uint32_t alignsize() override;
MATCH implicitConvTo(Type* to) override;
Expand Down Expand Up @@ -4521,7 +4519,6 @@ class TypeFunction final : public TypeNext
TypeFunction* syntaxCopy() override;
bool hasLazyParameters();
bool isDstyleVariadic() const;
Type* addStorageClass(StorageClass stc) override;
Type* substWildTo(uint32_t __param_0_) override;
MATCH constConv(Type* to) override;
bool iswild() const;
Expand Down
78 changes: 0 additions & 78 deletions compiler/src/dmd/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -1186,28 +1186,6 @@ extern (C++) abstract class Type : ASTNode
return t;
}

/************************************
* Add storage class modifiers to type.
*/
Type addStorageClass(StorageClass stc)
{
/* Just translate to MOD bits and let addMod() do the work
*/
MOD mod = 0;
if (stc & STC.immutable_)
mod = MODFlags.immutable_;
else
{
if (stc & (STC.const_ | STC.in_))
mod |= MODFlags.const_;
if (stc & STC.wild)
mod |= MODFlags.wild;
if (stc & STC.shared_)
mod |= MODFlags.shared_;
}
return this.addMod(mod);
}

final Type arrayOf()
{
if (ty == Terror)
Expand Down Expand Up @@ -3419,56 +3397,6 @@ extern (C++) final class TypeFunction : TypeNext
return linkage == LINK.d && parameterList.varargs == VarArg.variadic;
}

override Type addStorageClass(StorageClass stc)
{
//printf("addStorageClass(%llx) %d\n", stc, (stc & STC.scope_) != 0);
TypeFunction t = Type.addStorageClass(stc).toTypeFunction();
if ((stc & STC.pure_ && !t.purity) ||
(stc & STC.nothrow_ && !t.isnothrow) ||
(stc & STC.nogc && !t.isnogc) ||
(stc & STC.scope_ && !t.isScopeQual) ||
(stc & STC.safe && t.trust < TRUST.trusted))
{
// Klunky to change these
auto tf = new TypeFunction(t.parameterList, t.next, t.linkage, 0);
tf.mod = t.mod;
tf.fargs = fargs;
tf.purity = t.purity;
tf.isnothrow = t.isnothrow;
tf.isnogc = t.isnogc;
tf.isproperty = t.isproperty;
tf.isref = t.isref;
tf.isreturn = t.isreturn;
tf.isreturnscope = t.isreturnscope;
tf.isScopeQual = t.isScopeQual;
tf.isreturninferred = t.isreturninferred;
tf.isscopeinferred = t.isscopeinferred;
tf.trust = t.trust;
tf.isInOutParam = t.isInOutParam;
tf.isInOutQual = t.isInOutQual;
tf.isctor = t.isctor;

if (stc & STC.pure_)
tf.purity = PURE.fwdref;
if (stc & STC.nothrow_)
tf.isnothrow = true;
if (stc & STC.nogc)
tf.isnogc = true;
if (stc & STC.safe)
tf.trust = TRUST.safe;
if (stc & STC.scope_)
{
tf.isScopeQual = true;
if (stc & STC.scopeinferred)
tf.isscopeinferred = true;
}

tf.deco = tf.merge().deco;
t = tf;
}
return t;
}

override Type substWildTo(uint)
{
if (!iswild && !(mod & MODFlags.wild))
Expand Down Expand Up @@ -3770,12 +3698,6 @@ extern (C++) final class TypeDelegate : TypeNext
return result;
}

override Type addStorageClass(StorageClass stc)
{
TypeDelegate t = cast(TypeDelegate)Type.addStorageClass(stc);
return t;
}

override uinteger_t size(const ref Loc loc)
{
return target.ptrsize * 2;
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dmd/mtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ class Type : public ASTNode
bool isSharedWild() const { return (mod & (MODshared | MODwild)) == (MODshared | MODwild); }
bool isNaked() const { return mod == 0; }
Type *nullAttributes() const;
virtual Type *addStorageClass(StorageClass stc);
Type *arrayOf();
Type *sarrayOf(dinteger_t dim);
bool hasDeprecatedAliasThis();
Expand Down Expand Up @@ -580,7 +579,6 @@ class TypeFunction final : public TypeNext
TypeFunction *syntaxCopy() override;
bool hasLazyParameters();
bool isDstyleVariadic() const;
Type *addStorageClass(StorageClass stc) override;

Type *substWildTo(unsigned mod) override;
MATCH constConv(Type *to) override;
Expand Down Expand Up @@ -624,7 +622,6 @@ class TypeDelegate final : public TypeNext
static TypeDelegate *create(TypeFunction *t);
const char *kind() override;
TypeDelegate *syntaxCopy() override;
Type *addStorageClass(StorageClass stc) override;
uinteger_t size(const Loc &loc) override;
unsigned alignsize() override;
MATCH implicitConvTo(Type *to) override;
Expand Down Expand Up @@ -907,4 +904,5 @@ namespace dmd
Type *sharedWildConstOf(Type *type);
Type *castMod(Type *type, MOD mod);
Type *addMod(Type *type, MOD mod);
Type *addStorageClass(Type *type, StorageClass stc);
}
88 changes: 88 additions & 0 deletions compiler/src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -5644,6 +5644,94 @@ Dsymbol toDsymbol(Type type, Scope* sc)
}
}

/************************************
* Add storage class modifiers to type.
*/
Type addStorageClass(Type type, StorageClass stc)
{
Type visitType(Type t)
{
/* Just translate to MOD bits and let addMod() do the work
*/
MOD mod = 0;
if (stc & STC.immutable_)
mod = MODFlags.immutable_;
else
{
if (stc & (STC.const_ | STC.in_))
mod |= MODFlags.const_;
if (stc & STC.wild)
mod |= MODFlags.wild;
if (stc & STC.shared_)
mod |= MODFlags.shared_;
}
return t.addMod(mod);
}

Type visitFunction(TypeFunction tf_src)
{
//printf("addStorageClass(%llx) %d\n", stc, (stc & STC.scope_) != 0);
TypeFunction t = visitType(tf_src).toTypeFunction();
if ((stc & STC.pure_ && !t.purity) ||
(stc & STC.nothrow_ && !t.isnothrow) ||
(stc & STC.nogc && !t.isnogc) ||
(stc & STC.scope_ && !t.isScopeQual) ||
(stc & STC.safe && t.trust < TRUST.trusted))
{
// Klunky to change these
auto tf = new TypeFunction(t.parameterList, t.next, t.linkage, 0);
tf.mod = t.mod;
tf.fargs = tf_src.fargs;
tf.purity = t.purity;
tf.isnothrow = t.isnothrow;
tf.isnogc = t.isnogc;
tf.isproperty = t.isproperty;
tf.isref = t.isref;
tf.isreturn = t.isreturn;
tf.isreturnscope = t.isreturnscope;
tf.isScopeQual = t.isScopeQual;
tf.isreturninferred = t.isreturninferred;
tf.isscopeinferred = t.isscopeinferred;
tf.trust = t.trust;
tf.isInOutParam = t.isInOutParam;
tf.isInOutQual = t.isInOutQual;
tf.isctor = t.isctor;

if (stc & STC.pure_)
tf.purity = PURE.fwdref;
if (stc & STC.nothrow_)
tf.isnothrow = true;
if (stc & STC.nogc)
tf.isnogc = true;
if (stc & STC.safe)
tf.trust = TRUST.safe;
if (stc & STC.scope_)
{
tf.isScopeQual = true;
if (stc & STC.scopeinferred)
tf.isscopeinferred = true;
}

tf.deco = tf.merge().deco;
t = tf;
}
return t;
}

Type visitDelegate(TypeDelegate tdg)
{
TypeDelegate t = visitType(tdg).isTypeDelegate();
return t;
}

switch(type.ty)
{
default: return visitType(type);
case Tfunction: return visitFunction(type.isTypeFunction());
case Tdelegate: return visitDelegate(type.isTypeDelegate());
}
}

/**********************************************
* Extract complex type from core.stdc.config
* Params:
Expand Down

0 comments on commit a952831

Please sign in to comment.